【问题标题】:Error in node_modules/react-native/Libraries/Modal/Modal.js while testing with Jest使用 Jest 测试时 node_modules/react-native/Libraries/Modal/Modal.js 出错
【发布时间】:2017-10-03 02:57:03
【问题描述】:

我正在使用 Jest 进行简单的快照测试,但出现此错误:

home.test.js

import React from 'react';
import Home from './index';
import renderer from 'react-test-renderer';

it('renders home', ()=> {
  const view = renderer.create(
    <Home></Home>
  ).toJSON();
  expect(view).toMatchSnapshot();
});

不幸的是,我不知道这里有什么问题。我认为测试写得很好。

任何帮助都会很棒。

【问题讨论】:

    标签: javascript reactjs react-native jestjs


    【解决方案1】:

    我如何解决使测试无法运行的错误

    遇到了这个错误,导致我的测试无法运行

    Error Page

    经过几个小时的挫折,我两次删除了 node_modules 文件夹并重新安装并通过 Stackoverflow 和 Google 搜索,但没有任何解决方案,我决定创建一个新的 CRNA 项目,看看它是否有同样的问题。 当我发现新的 repo 中不存在问题时,我使用以下步骤使其工作:

    1. 再次删除 node_modules 文件夹
    2. 升级了我的 package.json 文件,主要变化如下:

    ``` {

    "devDependencies": {
            "react-native-scripts": "1.5.0",
            "jest-expo": "^21.0.2",
            "react-test-renderer": "16.0.0-alpha.12"
        },
    
    
    "jest": {
            "preset": "jest-expo",
            "transformIgnorePatterns": [
                    "node_modules/(?!react-native|expo|react-navigation|native-base-shoutem-theme|@shoutem/theme|@shoutem/animation|@shoutem/ui|tcomb-form-native)"
             ]
        },
    
    
    "dependencies": {
                    "react-native": “0.48.4”,
            "expo": "^21.0.2",
            "react": "16.0.0-alpha.12"
        },
    

    3. The core difference was in upgrading react-native from 0.48.0 to 0.48.4; react-native-scripts from 1.2.1 to 1.5.0 and including the transformIgnorePatterns under the jest option. NOTE: The portion from *native-base* in the transformIgnorePatterns was included because I used NativeBase in the project. 4. I then added a .babelrc with the following details:

    {
            "presets": ["babel-preset-expo"],
            "env": {
                    "development": {
                        "plugins": ["transform-react-jsx-source"]
                 }
             }
        }
    

    ``` 5. 现在运行测试给我结果:

    Passing Tests 所有测试都是绿色的

    【讨论】:

    • CRNA 是什么意思?
    • CRNA: create-react-native-app.
    猜你喜欢
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 2022-01-01
    • 2016-07-22
    • 2018-03-17
    • 2020-06-01
    • 2021-09-20
    • 2020-10-26
    相关资源
    最近更新 更多