我使用以下package.json(根据http://facebook.github.io/jest/docs/tutorial-react-native.html#content):
{
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
},
"dependencies": {
"react": "^15.3.1",
"react-native": "0.31.0",
},
"devDependencies": {
"babel-jest": "^14.1.0",
"babel-preset-react-native": "^1.9.0",
"jest": "^14.1.0",
"jest-cli": "^13.1.0",
"jest-react-native": "^14.1.3",
"react-test-renderer": "^15.3.1"
},
"jest": {
"globals": {
"__DEV__": true,
"__RCTProfileIsProfiling": false
},
"preset": "jest-react-native"
}
}
但我收到错误:
Error: Unknown config option "preset" with value "jest-react-native". This is either a typing error or another user mistake and fixing it will remove this message.
Using Jest CLI v13.2.3, jasmine2, babel-jest
FAIL __tests__/AuthorRequest-test.js (0s)
● Runtime Error
- Error: Cannot find module 'throwOnWrongReactAPI' from 'react-native.js'
at Resolver.resolveModule (node_modules/jest-cli/node_modules/jest-resolve/build/index.js:197:17)
at eval (node_modules/react-native/Libraries/react-native/react-native.js:180:26)
at Object.<anonymous> (node_modules/react-native/Libraries/react-native/react-native.js:189:4)
1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 2.238s)
npm ERR! Test failed. See above for more details.
我的.babelrc文件包含:
{
"presets": ["react-native"]
}
答案 0 :(得分:0)
我觉得你使用的是错误的Jest版本。你有:
"jest": "^14.1.0",
"jest-cli": "^13.1.0"
但似乎您使用13.2.3
安装了npm -g
:
Using Jest CLI v13.2.3, jasmine2, babel-jest
首先,我认为您可以删除jest-cli
并使用jest 14.1.0
。
然后您可以更新test
脚本,如:
"scripts": {
"test": "./node_modules/jest/bin/jest.js"
}
通过这种方式,您可以确保运行项目的本地Jest副本,因此现在应该说:
Using Jest CLI v14.1.0, jasmine2, babel-jest
这样做并遵循您发布的官方docs,它应该是您需要的一切(由于您没有发布测试代码,因此无法确定)。