Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

});
like image 956
Kyle Decot Avatar asked Sep 11 '17 19:09

Kyle Decot


People also ask

Can not find module in react?

To solve the "Cannot find module react or its corresponding type declarations" error, install the module and its type definitions by running the commands npm install react and npm i --save-dev @types/react . Copied! Now you should be able to import the react library with the following line of code.


1 Answers

Your Jest configuration is missing React Native preset:

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

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

like image 164
Michał Pierzchała Avatar answered Oct 02 '22 16:10

Michał Pierzchała