21

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

});
Brian Adams
  • 43,011
  • 9
  • 113
  • 111
Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
  • 2
    could you post your test file? – Jason Gaare Sep 11 '17 at 20:58
  • @JasonGaare I've posted it but it's just an import of `react-native`. – Kyle Decot Sep 11 '17 at 21:10
  • Your test file is incomplete. `import 'react-native'` from what? And also, wouldnt it be `import { stuff } from 'react-native'` – Max Baldwin Sep 11 '17 at 21:19
  • 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 Sep 11 '17 at 22:16
  • @AluanHaddad in javascript and React that is not true. Unless you have some docs that say otherwise. Jest is a javascript testing framework for React, so importing like that isn't going to work. – Max Baldwin Sep 12 '17 at 14:38
  • 2
    It most certainly works that way @MaxBaldwin. See [here](https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/local-cli/templates/HelloWorld/__tests__/App.js#L1) for an example in react native's repo. – Kyle Decot Sep 12 '17 at 15:59
  • 1
    @KyleDecot I guess you are right. You learn something new everyday – Max Baldwin Sep 13 '17 at 21:09
  • Do you also `import React from 'react'` in your test? See breaking changes after v0.26: https://github.com/facebook/react-native/releases/tag/v0.26.0. – Tom Davies Sep 14 '17 at 15:05
  • @TomDavies I have tried adding this but since this line comes after the import of react native (same in example from Facebook that I previously linked to) it makes no difference. – Kyle Decot Sep 18 '17 at 11:30
  • try import * from 'react-native' – DrEarnest Sep 20 '17 at 11:44
  • 2
    Not sure if this is the issue, but there should be a `preset: "react-native"` part in your jest config, like stated here https://facebook.github.io/jest/docs/en/tutorial-react-native.html – LYu Sep 20 '17 at 23:40
  • What version of `react-native` do you have installed? – Jack Sep 30 '17 at 14:42
  • Have you tried to import `react` before `react-native`? The link to your issue is not obvious, but it may be worth clearing this out. – RaphaMex Dec 20 '17 at 03:06

2 Answers2

2

Your Jest configuration is missing React Native preset:

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

It's available by default in this form since react-native@0.38.0.

Michał Pierzchała
  • 1,762
  • 1
  • 15
  • 20
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
    }
  },
AJcodez
  • 31,780
  • 20
  • 84
  • 118