javascript - Cannot find module 'ReactNative' from 'react-native.js' w Jest - Stack Overflow

I'm attempting to use jest (v20.0.0) w my React Native application (v0.42.0) however when I run y

I'm attempting to use jest (v20.0.0) w/ my React Native application (v0.42.0) however when I run yarn jest I get the following error:

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)

Here is the jest portion of my package.json

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

Update #1

Here's the failing test file (I stripped out everything except the import and the error persists).

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

describe('Router', () => {

});

I'm attempting to use jest (v20.0.0) w/ my React Native application (v0.42.0) however when I run yarn jest I get the following error:

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)

Here is the jest portion of my package.json

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

Update #1

Here's the failing test file (I stripped out everything except the import and the error persists).

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

describe('Router', () => {

});
Share Improve this question edited Aug 7, 2018 at 18:03 Brian Adams 45.9k12 gold badges122 silver badges115 bronze badges asked Sep 11, 2017 at 19:36 Kyle DecotKyle Decot 20.9k40 gold badges149 silver badges269 bronze badges 13
  • 2 could you post your test file? – Jason Gaare Commented Sep 11, 2017 at 20:58
  • 2 @MaxBaldwin that's a valid import form. It's purpose is to import a module which has side effects for those side effects alone. There is no from clause used in that case. – Aluan Haddad Commented Sep 11, 2017 at 22:16
  • 2 It most certainly works that way @MaxBaldwin. See here for an example in react native's repo. – Kyle Decot Commented Sep 12, 2017 at 15:59
  • 1 @KyleDecot I guess you are right. You learn something new everyday – Max Baldwin Commented Sep 13, 2017 at 21:09
  • 2 Not sure if this is the issue, but there should be a preset: "react-native" part in your jest config, like stated here facebook.github.io/jest/docs/en/tutorial-react-native.html – LYu Commented Sep 20, 2017 at 23:40
 |  Show 8 more ments

2 Answers 2

Reset to default 2

Your Jest configuration is missing React Native preset:

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

It's available by default in this form since [email protected].

Here is a config that works for me.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744863355a4597833.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信