找不到来自'react-native.js'w / Jest的模块'ReactNative'

时间:2017-09-11 19:36:20

标签: javascript reactjs react-native jestjs

我正在尝试使用jest(v20.0.0)w /我的React Native应用程序(v0.42.0)但是当我运行yarn jest时出现以下错误:

yarn jest v0.27.5
$ jest
 FAIL  __tests__/routing/router-test.js
  ● Test suite failed to run

    Cannot find module 'ReactNative' from 'react-native.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
      at Object.<anonymous> (node_modules/react-native/Libraries/react-native/react-native.js:188:25)

以下是jest

package.json部分
  "jest": {
    "testPathIgnorePatterns": [
      "/node_modules/"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!react-native|react-native-geocoding)/"
    ],
    "globals": {
      "__DEV__": false
    },
    "collectCoverage": false
  },

更新#1

这是失败的测试文件(我删除了import以外的所有内容,但错误仍然存​​在。)

import 'react-native';
import React from 'react';

describe('Router', () => {

});

2 个答案:

答案 0 :(得分:2)

您的Jest配置缺少React Native预设:

"jest": {
  "preset": "react-native"
}

默认情况下,此格式自react-native@0.38.0以来可用。

答案 1 :(得分:0)

这是一个适合我的配置。

  "devDependencies": {
    "babel-jest": "21.0.2",
    "babel-plugin-module-resolver": "2.7.1",
    "babel-preset-es2015": "6.24.1",
    "babel-preset-react-native": "1.9.1",
    "jest": "21.0.2"
  },
  "jest": {
    "preset": "react-native",
    "automock": false,
    "testMatch": [
      "<rootDir>/source/**/__tests__/*.js"
    ],
    "moduleFileExtensions": [
      "js",
      "jsx"
    ],
    "moduleDirectories": [
      "node_modules",
      "<rootDir>/source"
    ],
    "globals": {
      "__DEV__": true
    }
  },