【发布时间】:2018-02-20 03:19:39
【问题描述】:
我正在尝试将 jest (v20.0.0) 与我的 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)
这是我的package.json 的jest 部分
"jest": {
"testPathIgnorePatterns": [
"/node_modules/"
],
"transformIgnorePatterns": [
"node_modules/(?!react-native|react-native-geocoding)/"
],
"globals": {
"__DEV__": false
},
"collectCoverage": false
},
更新 #1
这是失败的测试文件(我删除了除 import 之外的所有内容,并且错误仍然存在)。
import 'react-native';
import React from 'react';
describe('Router', () => {
});
【问题讨论】:
-
你能发布你的测试文件吗?
-
@MaxBaldwin 这是一个有效的导入表单。其目的是导入一个仅对这些副作用有副作用的模块。在这种情况下没有使用
from子句。 -
它肯定会以@MaxBaldwin 的方式工作。请参阅 here 以获取 react native 的 repo 中的示例。
-
@KyleDecot 我猜你是对的。你每天都能学到新东西
-
不确定这是否是问题所在,但您的 jest 配置中应该有一个
preset: "react-native"部分,就像这里所说的 facebook.github.io/jest/docs/en/tutorial-react-native.html
标签: javascript reactjs react-native jestjs