0
我試圖在這個jest tutorial的react-native中開始單元測試。但是當我運行npm test
我得到的錯誤Cannot find module 'react/lib/ReactCurrentOwner' from 'ReactFiberTreeReflection.js'
從'ReactFiberTreeReflection.js'找不到模塊'react/lib/ReactCurrentOwner'
我的分量是最簡單越好
// /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.
個aditional的信息
版本
反應天然:
react-native-cli: 2.0.1
react-native: 0.45.1
紗:v0.27.5
NPM:3.10.10
節點:v6.11.1
Package.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](https://github.com/dalcib/jest-expo-ts/issues/1)。可以有一些有用的信息。 –