2016-11-29 99 views
0

我將Jest Testing框架添加到我的React Native項目中。我收到以下錯誤:Jest配置

Failed to get mock metadata: /Users/me/Documents/Development/project/node_modules/global/window.js 

我的測試文件看起來像這樣:

import 'react-native' 
import React from 'react' 
import { MyComponent } from '../components/MyComponent' 

import renderer from 'react-test-renderer' 

it('renders correctly',() => { 
    const tree = renderer.create(<MyComponent />).toJSON() 
    expect(tree).toMatchSnapshot() 
}) 

而且還玩笑配置在我的package.json:

"jest": { 
    "preset": "jest-react-native", 
    "testPathIgnorePatterns": ["/node_modules/", "/example/", "/lib/"], 
    "testRegex": "(/tests/.*|\\.(test|spec))\\.(js|jsx)$", 
    "automock": "true", 
    "unmockedModulePathPatterns": [ "lodash" ], 
    "transformIgnorePatterns": [ 
     "node_modules/([email protected]/ex-navigation", 
     ")" 
    ] 
    } 

我有一個按照提示的錯誤建議查看http://facebook.github.io/jest/docs/manual-mocks.html#content。在你的package.json

回答

0

"automock": "false"(與automocking笑話反應的母語預設isn't supported

1

我覺得有什麼錯在你的package.json開玩笑配置。

下面是一個示例笑話配置片段:

"jest": { 
    "preset": "react-native", 
    "cacheDirectory": "./cache", 
    "coveragePathIgnorePatterns": [ 
     "./app/utils/vendor" 
    ], 
    "coverageThreshold": { 
     "global": { 
     "statements": 80 
     } 
    }, 
    "transformIgnorePatterns": [ 
     "/node_modules/(?!react-native|react-clone-referenced-element|react-navigation)" 
    ] 
    } 

預設:預設爲模仿一陣營原生應用的環境中的節點的環境。由於它不加載任何DOM或瀏覽器API,因此它極大地提高了Jest的啓動時間。

cacheDirectory:它可以幫助您極大地提高測試速度。它通過創建編譯模塊的緩存來實現,以便下次在運行測試時不必編譯node_modules。

coveragePathIgnorePatterns:定義想要跳過覆蓋報告的文件。

coverageThreshold:定義所有要通過的測試的閾值限制。如果覆蓋範圍小於定義的限制,則測試將失敗。這有助於我們在各個時間點保持良好的覆蓋面。

transformIgnorePatterns:我們通過所有需要轉換的NPM模塊。這些模塊基本上是ES6/7模塊。 PS:我寫了一篇關於如何爲反應原生項目設置笑話的博客。這裏是網址:http://rahulgaba.com/react-native/2017/05/19/Jest-test-suite-with-superpowers.html