使用Jest进行测试时,node_modules / react-native / Libraries / Modal / Modal.js出错

时间:2017-10-03 02:57:04

标签: javascript reactjs react-native jest

我正在使用Jest进行简单的快照测试,只是出现此错误:enter image description here

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();
});

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

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

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

进入此错误,这使我的测试无法运行

Error Page

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

  1. 再次删除node_modules文件夹
  2. 升级了我的package.json文件,其密钥更改如下所示:
  3. ``` {

    "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 所有测试都是绿色的