2016-09-17 75 views
8

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

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模块。

+0

好吧,所以我似乎是正确的,要解决这个问题,我想我需要写一个驱动程序包的茉莉花和运行jest当流星正在运行...我会用摩卡反而:( –

回答

10

您可以轻松地存根使用 “moduleNameMapper” 流星模块在开玩笑配置文件:

"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)。

+0

这是多德这是老问题,但我喜欢你的答案,并要测试ASAP然后批准! –

+0

欢呼的人:)我遇到了同样的问题,并发现这一点。以为我会分享我的解决方案。 – chmanie

+0

这是流星的博客文章[真实世界单元测试与流星和Jest](https://blog.meteor.com/real-world-unit-tests-with-meteor-and-jest-3d557e84e84a)解释了如何用jest进行测试。这是指回到这个答案;-) –