20

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

});
13
  • 2
    could you post your test file? 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. 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. Commented Sep 12, 2017 at 15:59
  • 1
    @KyleDecot I guess you are right. You learn something new everyday 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 Commented Sep 20, 2017 at 23:40

2 Answers 2

2

Your Jest configuration is missing React Native preset:

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.