Kyle Decot
Kyle Decot

Reputation: 20815

Cannot find module 'ReactNative' from 'react-native.js' w/ Jest

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', () => {

});

Upvotes: 21

Views: 1919

Answers (2)

Michał Pierzchała
Michał Pierzchała

Reputation: 1850

Your Jest configuration is missing React Native preset:

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

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

Upvotes: 2

AJcodez
AJcodez

Reputation: 34166

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

Upvotes: 0

Related Questions