Cannot get Jest & React Native working

风格不统一 提交于 2020-02-24 12:14:06

问题


Trying to get Jest v12.1.1 working with React Native v0.26.2 I'm getting this error when run npm test: Error: Cannot find module 'ErrorUtils' from 'env.js'

Here's my package.json. I'm trying to get this working with the default react-native init AwesomeProject starter.

Is there something missing from my package.json? (are here any lines I don't need?)

EXAMPLE 1:

package.json

{
  "name": "AwesomeProject",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start"
  },
  "dependencies": {
    "react": "^15.0.2",
    "react-native": "^0.26.2"
  },
  "devDependencies": {
    "babel-jest": "^9.0.0",
    "babel-preset-es2015": "*",
    "babel-preset-react": "*",
    "jest-cli": "^12.1.1"
  },
  "scripts": {
    "test": "jest"
  },
  "jest": {   
    "scriptPreprocessor": "node_modules/react-native/jestSupport/preprocessor.js",
    "setupEnvScriptFile": "node_modules/react-native/jestSupport/env.js",        
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/android/",
      "/ios/",
      "/.idea/"
    ],    
    "testFileExtensions": [
      "js"
    ],
    "unmockedModulePathPatterns": [
      "<rootDir>/node_modules/react/",
      "<rootDir>/node_modules/react-dom/",
      "<rootDir>/node_modules/react-addons-test-utils/"
    ],    
    "verbose": true
  }
}

Here's a test script.

index.android-test.js

// __tests__/index.android-test.js

'use strict';

jest
  .disableAutomock()
  .setMock('AwesomeProject', {})
  .setMock('React', {Component: class {}});

describe('AwesomeProject', () => {

  it('test something', () => {
    const testJunk = true;

    expect(testJunk).toEqual(true);
  });

});

EXAMPLE 2:

I get a different error when I add "haste" to package.json and remove scriptPreprocessor & setupEnvScriptFile. Error: Cannot find module 'AwesomeProject' from 'index.android-test.js'

I have no clue why I get farther ahead with the following. My previous Example 1 is following: https://facebook.github.io/jest/docs/tutorial-react.html#content And Example 2 with 'haste` is following: https://github.com/fbsamples/f8app/blob/master/package.json

Really confused about why Ex 1 doesn't work.

revised package.json

{
  "name": "AwesomeProject",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start"
  },
  "dependencies": {
    "react": "^15.0.2",
    "react-native": "^0.26.2"
  },
  "devDependencies": {
    "babel-jest": "^9.0.0",
    "babel-preset-es2015": "*",
    "babel-preset-react": "*",
    "jest-cli": "^12.1.1"
  },
  "scripts": {
    "test": "jest"
  },
  "jest": {   
    "haste": {
      "defaultPlatform": "android",
      "platforms": [
        "ios",
        "android"
      ],
      "providesModuleNodeModules": [
        "react-native"
      ]
    },    
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/android/",
      "/ios/",
      "/.idea/"
    ],    
    "testFileExtensions": [
      "js"
    ],
    "unmockedModulePathPatterns": [
      "<rootDir>/node_modules/react/",
      "<rootDir>/node_modules/react-dom/",
      "<rootDir>/node_modules/react-addons-test-utils/"
    ],    
    "verbose": true
  }
}

来源:https://stackoverflow.com/questions/37474160/cannot-get-jest-react-native-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!