2015-11-27 48 views
4

我有一個非常簡單的測試玩笑問題 - ReactElement.js:無法模擬元數據

/** @jsx React.DOM */ 
var store = require('../simpleStore'); 
var TestUtils = require('react/addons').addons.TestUtils; 

describe("my tests",function(){ 
    it('simple test',function(){ 
    var x=10, 
     y=20; 
    expect(x).not.toBe(y); 
    }) 
}) 

測試工作得很好,只要我不要求TestUtils。 我添加任何node_module引用的那一刻,我開始看到下面的錯誤

Using Jest CLI v0.8.0, jasmine1 
FAIL build/stores/__tests__/simple-test.js 

● Runtime Error 

Error: ../stores/__tests__/simple-test.js: 
../../node_modules/react/addons.js: 
../../node_modules/react/lib/ReactWithAddons.js: 
../../node_modules/react/lib/LinkedStateMixin.js: 
../../node_modules/react/lib/ReactLink.js: 
../../node_modules/react/lib/React.js: 
../../node_modules/react/lib/ReactDOM.js: 
../../node_modules/react/lib/ReactDOMTextComponent.js: 
../../node_modules/react/lib/ReactComponentBrowserEnvironment.js: 
../../node_modules/react/lib/ReactDOMIDOperations.js: 
../../node_modules/react/lib/ReactMount.js: 
../../node_modules/react/lib/ReactElement.js: Failed to get mock metadata: 
../../node_modules/react/lib/canDefineProperty.js 
npm ERR! Test failed. See above for more details. 

我敢肯定,很多人都在用笑話,我失去了一些東西silly..appreaciate任何想法來解決這個問題?

回答

5

嘗試禁用嘲諷的反應 - 你JSET configuraction中的package.json:

"unmockedModulePathPatterns": [ 
    "<rootDir>/node_modules/react", 
    "<rootDir>/node_modules/fbjs" 
] 
+0

不幸的是我仍然看到錯誤,即使有這樣的配置:' 「unmockedModulePathPatterns」: 「/node_modules /反應」, 「/node_modules /反應-DOM」, 「/node_modules /反應-插件 - 測試 - utils的」, 「/node_modules/FBJS」 ],' – Andy

+1

我後放棄了玩笑有多個測試問題反應母語。您可能有興趣查看酶(http://airbnb.io/enzyme/)。您可以使用Mocha和Chai進行測試,而且大部分都是無痛的。 – kyrisu

+0

感謝您的提示!我現在最終只使用了Jasmine和React TestUtils,但Enzyme看起來很棒! – Andy