【问题标题】:Meteor react jest testing流星反应开玩笑测试
【发布时间】:2017-01-25 16:54:43
【问题描述】:

简单的玩笑测试只是为了检查反应组件是否可以渲染并且它失败了,因为我导入了

import { Meteor } from 'meteor/meteor'

完整的错误是...

 PASS  imports/__partials/Navigation/__tests__/Navigation.jest.js
 PASS  imports/__layouts/AuthLayout/__tests__/AuthLayout.jest.js
 FAIL  imports/features/oAuth/ui/LoginLayout/__tests__/LoginLayout.jest.js
  ● Test suite failed to run

    Cannot find module 'meteor/meteor' from 'index.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:142:17)
      at Object.<anonymous> (imports/features/oAuth/ui/LoginLayout/index.js:2:41)
      at Object.<anonymous> (imports/features/oAuth/ui/LoginLayout/__tests__/LoginLayout.jest.js:4:40)

 PASS  imports/staticTests/quickCheckboxTest/__tests__/CheckboxWithLabel.jest.js
 PASS  imports/staticTests/quickLinkTest/__tests__/Link.react.jest.js

我将假设它是因为流星没有构建,因此 meteor/meteor 不存在,任何帮助使其工作的帮助将不胜感激。 :)

编辑...

我的假设是正确的,这基本上是因为流星没有构建 npm 模块。

【问题讨论】:

  • 好吧,看来我是对的,为了解决这个问题,我想我需要为 jasmine 编写一个驱动程序包,并在流星运行时运行 jest ......我将使用 mocha 代替: (

标签: javascript testing meteor ecmascript-6 jestjs


【解决方案1】:

您可以在您的 jest 配置文件中使用“moduleNameMapper”轻松存根 Meteor 模块:

"moduleNameMapper": {
  "^meteor/(.*)": "<rootDir>/meteorMocks.js"
}

在meteorMocks.js 中:

export const Meteor = {
  call: () => null,
  // ... more stuff you'd like to mock on the Meteor object
};

那你就可以了

import { Meteor } from 'meteor/meteor';

在您的测试文件中。

只需对您需要模拟的所有模块执行相同操作(例如 TrackerReactiveVar)。

【讨论】:

  • 老兄这是个老问题,但我喜欢你的回答,尽快去测试然后批准!
  • 干杯人 :) 我遇到了同样的问题并找到了这个。想我会分享我的解决方案。
  • 这里是流星Real-World Unit Tests with Meteor and Jest 的博客文章,解释了如何使用 jest 进行测试。它指的是这个答案;-)
  • 你有没有使用全局变量在不导入的情况下做同样的事情的模式?它似乎不适用于特别是从 3rd 方包加载的代码。但它适用于下划线,很奇怪。
  • 还有@Michael_Scharf 我知道你刚刚发布了这个,但是你最近遇到了“core-js”的问题吗?基本上 Meteor 出于某种原因强制 core-js v2,而 Jest/Babel 现在使用 v3(请参阅 github.com/VulcanJS/Vulcan/issues/2270 了解我们在这方面的进展)
猜你喜欢
  • 2020-03-02
  • 2017-12-19
  • 2018-07-22
  • 2019-11-22
  • 1970-01-01
  • 2018-01-10
  • 1970-01-01
  • 2023-02-09
  • 2014-09-18
相关资源
最近更新 更多