流星反应开玩笑测试

时间:2016-09-17 15:55:00

标签: javascript testing meteor ecmascript-6 jestjs

简单的jest测试只是为了检查react组件是否可以呈现并且因为我导入

而失败

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不存在,任何帮助使其工作将不胜感激。 :)

编辑...

我的假设是正确的,这主要是因为meteor没有构建npm模块。

1 个答案:

答案 0 :(得分:16)

您可以使用&#34; moduleNameMapper&#34;轻松存根Meteor模块。在您的jest配置文件中:

"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)执行相同操作。