Cannot get Jest & React Native working

2019-07-17 02:28发布

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
  }
}

0条回答
登录 后发表回答