2016-09-17 76 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進行測試。這是指回到這個答案;-) –