【发布时间】:2018-02-24 22:24:22
【问题描述】:
我试图在这个 jest tutorial 之后在 react-native 中开始单元测试。但是当我运行npm test 时,我得到了错误Cannot find module 'react/lib/ReactCurrentOwner' from 'ReactFiberTreeReflection.js'
我的组件是最简单的
// /SimpleComp.js
import React, { Component } from 'react';
import {
View,
} from 'react-native';
export default class SimpleComp extends Component {
constructor(props){
super(props);
this.state = {}
}
render() {
return (
<View></View>
);
}
}
这是我的测试文件
// /__tests__/SimpleComp-test.js
import 'react-native';
import React from 'react';
import SimpleComp from '../SimpleComp';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
test('renders correctly', () => {
const tree = renderer.create(
<SimpleComp />
).toJSON();
expect(tree).toMatchSnapshot();
});
完整的测试输出
FAIL __tests__/SimpleComp-test.js
● Test suite failed to run
Cannot find module 'react/lib/ReactCurrentOwner' from 'ReactFiberTreeReflection.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:169:17)
at Object.<anonymous> (node_modules/react-test-renderer/lib/ReactFiberTreeReflection.js:15:25)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.42s
Ran all test suites.
npm ERR! Test failed. See above for more details.
附加信息
版本
反应原生:
react-native-cli: 2.0.1
react-native: 0.45.1
纱线:v0.27.5
npm: 3.10.10
节点:v6.11.1
包.json
{
"name": "PrimeiraMesa2",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
"flow": "flow"
},
"dependencies": {
"axios": "^0.16.2",
"jest-haste-map": "^20.0.4",
"moment": "^2.18.1",
"native-base": "2.2.0",
"prop-types": "^15.5.10",
"react": "16.0.0-alpha.12",
"react-devtools-core": "^2.3.3",
"react-native": "0.45.1",
"react-native-check-box": "^1.0.4",
"react-native-communications": "^2.2.1",
"react-native-credit-card-input": "^0.3.3",
"react-native-fbsdk": "^0.6.0",
"react-native-google-places-autocomplete": "^1.2.12",
"react-native-gps-state": "git+https://github.com/neuberoliveira/react-native-gps-state.git",
"react-native-image-picker": "^0.26.3",
"react-native-keyboard-aware-scroll-view": "^0.2.9",
"react-native-linear-gradient": "react-native-community/react-native-linear-gradient",
"react-native-masked-text": "^1.5.1",
"react-native-modal": "^2.4.0",
"react-native-multiple-select": "^0.2.1",
"react-native-navigation": "^1.1.134",
"react-native-onesignal": "^3.0.5",
"react-native-push-notification": "^3.0.0",
"react-native-scrollable-tab-view": "^0.6.7",
"react-native-star-rating": "^1.0.7",
"react-native-swiper": "^1.5.4",
"react-native-textinput-effects": "git+https://github.com/multisolution/react-native-textinput-effects",
"react-native-vector-icons": "^4.0.1",
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.2.0",
"tcomb-form-native": "^0.6.9",
"underscore": "^1.8.3"
},
"devDependencies": {
"react-test-renderer": "16.0.0-alpha.6",
"babel-preset-react-native": "1.9.1",
"babel-jest": "19.0.0",
"eslint": "^4.4.1",
"jest": "19.0.2",
},
"jest": {
"preset": "react-native"
}
}
【问题讨论】:
-
我假设你已经运行了
npm install,对吧?如果有,请尝试删除node_modules文件夹并重新运行npm install。 -
已经做了几次,同样的结果
-
尝试在 GitHub 上引用 this issue。可能有一些有用的信息。
-
也试过这个问题,主要是
import *的东西(不要使用expo),同样的结果
标签: reactjs react-native jestjs