2

试图让 Jest v12.1.1 与 React Native v0.26.2 一起工作我在运行时收到此错误npm test Error: Cannot find module 'ErrorUtils' from 'env.js'

这是我的package.json。我正在尝试使用默认react-native init AwesomeProject启动器来实现它。

我的 package.json 中是否缺少某些内容?(这里有我不需要的行吗?)

示例 1:

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

这是一个测试脚本。

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

});

示例 2:

当我将“haste”添加到package.json并删除scriptPreprocessor&时,我得到了一个不同的错误setupEnvScriptFile Error: Cannot find module 'AwesomeProject' from 'index.android-test.js'

我不知道为什么我会在以下方面走得更远。我之前的示例 1 如下: https ://facebook.github.io/jest/docs/tutorial-react.html#content 和带有“haste”的示例 2 如下: https ://github.com/fbsamples/f8app/ blob/master/package.json

真的很困惑为什么 Ex 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": {   
    "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
  }
}
4

0 回答 0