2016-11-29 96 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