reactjs - 无法从 'react/lib/ReactCurrentOwner' 找到模块 'ReactFiberTreeReflection.js'

标签 reactjs react-native jestjs

我尝试按照此 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 :

react-native-cli: 2.0.1
react-native: 0.45.1

yarn :v0.27.5

npm:2010 年 10 月 3 日

节点: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"
    }
}

最佳答案

解决我自己的问题。

我没有创建项目,我在中间得到了项目,所以我安装了新版本的react-native 0.45,与我的项目使用的相同,并且我发现包中的jest版本.json devDependency 不匹配。

这是我的项目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

再次运行npm test,测试按预期通过。

关于reactjs - 无法从 'react/lib/ReactCurrentOwner' 找到模块 'ReactFiberTreeReflection.js',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46244442/

相关文章:

javascript - 如何将函数的 "this"绑定(bind)到类(React 组件)

reactjs - 在 native react 中重置子不透明度

react-native - 如何模拟包含 React Native 模块功能的全局变量

javascript - 将事件对象传递给 enzyme .simulate

javascript - 如何测试商店单例?

reactjs - 如何(同时)更改 react 中状态的多个属性?

javascript - React - 处理输入的变化以及通过数组迭代呈现的状态

javascript - React Native - 如何使用 map 数据渲染行?

ios - React Native iOS 构建失败无法解析构建文件

javascript - 为什么在 JavaScript 中任何数字除以 -Infinity 时都会得到 -0?