我正在尝试使用jest(v20.0.0
)w /我的React Native应用程序(v0.42.0
)但是当我运行yarn jest
时出现以下错误:
yarn jest v0.27.5
$ jest
FAIL __tests__/routing/router-test.js
● Test suite failed to run
Cannot find module 'ReactNative' from 'react-native.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
at Object.<anonymous> (node_modules/react-native/Libraries/react-native/react-native.js:188:25)
以下是jest
package.json
部分
"jest": {
"testPathIgnorePatterns": [
"/node_modules/"
],
"transformIgnorePatterns": [
"node_modules/(?!react-native|react-native-geocoding)/"
],
"globals": {
"__DEV__": false
},
"collectCoverage": false
},
这是失败的测试文件(我删除了import
以外的所有内容,但错误仍然存在。)
import 'react-native';
import React from 'react';
describe('Router', () => {
});
答案 0 :(得分:2)
您的Jest配置缺少React Native预设:
"jest": {
"preset": "react-native"
}
默认情况下,此格式自react-native@0.38.0
以来可用。
答案 1 :(得分:0)
这是一个适合我的配置。
"devDependencies": {
"babel-jest": "21.0.2",
"babel-plugin-module-resolver": "2.7.1",
"babel-preset-es2015": "6.24.1",
"babel-preset-react-native": "1.9.1",
"jest": "21.0.2"
},
"jest": {
"preset": "react-native",
"automock": false,
"testMatch": [
"<rootDir>/source/**/__tests__/*.js"
],
"moduleFileExtensions": [
"js",
"jsx"
],
"moduleDirectories": [
"node_modules",
"<rootDir>/source"
],
"globals": {
"__DEV__": true
}
},