我正在向我的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配置:
"jest": {
"preset": "jest-react-native",
"testPathIgnorePatterns": ["/node_modules/", "/example/", "/lib/"],
"testRegex": "(/tests/.*|\\.(test|spec))\\.(js|jsx)$",
"automock": "true",
"unmockedModulePathPatterns": [ "lodash" ],
"transformIgnorePatterns": [
"node_modules/(?!@exponent/ex-navigation",
")"
]
}我按照错误提示中的建议查看了一下http://facebook.github.io/jest/docs/manual-mocks.html#content。
发布于 2017-09-17 17:11:25
我认为package.json中的jest配置有问题。
下面是一个示例jest配置片段:
"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)"
]
}preset :该预设是一个模拟React Native应用程序环境的节点环境。因为它不加载任何DOM或浏览器API,所以极大地改善了Jest的启动时间。
cacheDirectory:它可以帮助你极大地提高测试速度。它通过创建已编译模块的缓存来实现这一点,以便下次不必在运行测试时编译node_modules。
coveragePathIgnorePatterns:为覆盖率报告定义了要跳过的文件。
coverageThreshold:定义了所有测试要通过的阈值限制。如果覆盖率小于定义的限制,则测试将失败。这帮助我们在所有时间点都保持了良好的覆盖率。
transformIgnorePatterns:我们在这里传递了所有需要转译的NPM模块。这些模块基本上都是ES6/7模块。
PS:我已经写了一篇关于如何为react-native项目设置jest的博客。这是网址:http://rahulgaba.com/react-native/2017/05/19/Jest-test-suite-with-superpowers.html
发布于 2016-12-06 05:04:54
在package.json中设置"automock": "false" (使用jest-react-native预置isn't supported自动锁定)
https://stackoverflow.com/questions/40873125
复制相似问题