我尝试在本地jest tutorial的反应原生中开始单元测试。但是当我运行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
node:v6.11.1
{
"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"
}
}
答案 0 :(得分:1)
解决我自己的问题。
我没有创建项目,我把项目放在中间,所以我安装了一个新版本的react-native 0.45,我的项目正在使用,我发现{{1}中的开玩笑版本是不匹配的。
以下是我的项目package.json devDependencies
devDependencies
这里是全新安装的"devDependencies": {
"react-test-renderer": "16.0.0-alpha.6",
"babel-preset-react-native": "1.9.1",
"babel-jest": "19.0.0",
"jest": "19.0.2",
},
devDependencies
似乎项目已更新。
之后我再次安装所有内容"devDependencies": {
"react-test-renderer": "16.0.0-alpha.12",
"babel-preset-react-native": "4.0.0",
"babel-jest": "21.0.2",
"jest": "21.1.0"
},
按预期再次运行rm -rf node_modules && yarn install
测试通道。