jest: Cannot find module 'ReactNativeDefaultInjection' from 'setup.js'

 FAIL  __tests__/App-test.js
  ● Test suite failed to run

    Cannot find module 'ReactNativeDefaultInjection' from 'setup.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:142:17)

Test Summary
 › Ran all tests.
 › 1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 0.242s)
npm ERR! Test failed.  See above for more details.

here is my config of jest

  "jest": {
    "preset": "jest-react-native",
    "preprocessorIgnorePatterns": [
      "node_modules/(?!react-native|jpush-react-native)"
    ],
    "haste": {
      "defaultPlatform": "ios",
      "platforms": [
        "ios",
        "android"
      ]
    },
    "modulePathIgnorePatterns": [
      "react",
      "react-native"
    ],
    "moduleFileExtensions": [
      "js",
      "jsx"
    ],
    "collectCoverage": false,
    "verbose": true,
    "testPathIgnorePatterns": [
      "/node_modules/",
      ".+/(resources)/.+",
      ".+/(autoMockOff.js)"
    ]
  },
  "devDependencies": {
    "babel-jest": "^15.0.0",
    "babel-plugin-module-resolver": "^2.0.0",
    "babel-polyfill": "^6.16.0",
    "babel-preset-react-native": "^1.9.0",
    "babel-preset-react-native-stage-0": "^1.0.1",
    "jest": "^15.1.1",
    "jest-react-native": "^15.0.0",
    "react-test-renderer": "^15.3.2",
    "remote-redux-devtools": "^0.4.7"
  }

any idea?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 42 (14 by maintainers)

Commits related to this issue

Most upvoted comments

We had this issue to using jest@17.0.2, in our case was not installing the right version of react for react-native

We had this in our package.json

{
    "react": "^15.3.2",
    "react-native": "^0.37.0",
}

and doing a npm install gave us this output

react-native@0.37.0 requires a peer of react@~15.3.1 but none was installed.

This was due react@^15.3.2 resolve to react@15.4.0 due how npm semver works.

Setting the react version to react@~15.3.2 solved the issue. Change your package.json to look like this

{
    "react": "~15.3.2",
    "react-native": "^0.37.0",
}

@JeroenNelen If you are using react-native prior to 0.38, use jest-react-native locked to version 17.0.3 or lower. If you are using 0.38, use react-native as preset or install 17.1.0 of jest-react-native which will continue to work (and forward) to react-native for a while.

This is happening to me with

    "react": "~15.4.0-rc.4",
    "react-native": "~0.38.0",

    "babel-eslint": "~6.1.2",
    "babel-jest": "~17.0.2",
    "babel-preset-react-native": "~1.9.0",
    "eslint": "~3.4.0",
    "eslint-plugin-react": "~6.2.0",
    "jest": "~17.0.3",
    "jest-react-native": "~17.0.3",
    "react-test-renderer": "~15.4.0"

but only on my CircleCI VM… locally everything runs fine.

Oh, remove the haste config from your Jest configuration. The jest-react-native preset brings this in automatically.

Yeah, we moved to more explicit mocks and StatusBar might simply not be mocked properly. See https://github.com/facebook/react-native/blob/master/jest/setup.js#L69 – you can send a PR to RN to fix this and meanwhile you can probably add this mock manually a file that you add to setupFiles.

Sorry for triple comment. This is not a Jest issue, I guess It’s React issue. React and Haste *

let me explain a bit require('React') is actually handled by Haste loader, by @providesModule React directive.

Where it’s located: Source code: src/isomorphic/React.js https://github.com/facebook/react/blob/c78464f8ea9a5b00ec80252d20a71a1482210e57/src/isomorphic/React.js#L9 Transpiled version: node_modules/react/lib/React.js

I’ve installed some last versions one-by-one, and this directive was suddendly disappear from transpiled code, throwing error from Haste, which can’t resolve React to node_modules/react.

Versions and directive existence:

Version Has Haste directive
15.3.2 Yes
15.4.0-rc.1 Yes
15.4.0-rc.2 Yes
15.4.0-rc.3 No
15.4.0-rc.4 No
15.4.0 No
15.4.1 No

What was happen with transpiled source at 15.4.0-rc.2 -> 15.4.0-rc.3 version bump? I dunno…

So. For now, I have react-native 0.38 installed, and

npm WARN react-native@0.38.0 requires a peer of react@~15.4.0-rc.4 but none was installed.

And react@~15.4.0-rc.4 don’t hase Haste directive -> tests are broken…

What to do?

  1. Rollback react to 15.4.0-rc.2 or 15.3.2 and wait for fix
  2. See Red CI servers and wait for fix…
  • actually, react-native packager forms bundle successfully… and Jest can’t find react module O_O very strange…

Oh man, i figured this out. We need to install react as dependency or devDependency. 🎉 Cheers !

I did some more research and if I put my test in __ tests __ folder, everything works fine. However, when I move them to /test/jest, and add this param:

"testPathDirs": ["./test/jest"]

Then it starts failing with ReactNativeDefaultInjection error.

I’m having the exact same issue. Here’s my package.json:

"scripts": {
    "start": "react-native start",
    "test": "jest"
  },
  "jest": {
    "preset": "jest-react-native",
    "testPathDirs": ["./test/jest"]
  },
  "devDependencies": {
    "babel-core": "^6.14.0",
    "babel-preset-es2015": "^6.13.2",
    "babel-preset-react": "^6.11.1",
    "babel-preset-react-native": "^1.9.0",
    "babel-preset-stage-2": "^6.13.0",
    "babel-jest": "^15.0.0",
    "babelify": "^7.3.0",
    "browserify": "^13.1.0",
    "chai": "^3.5.0",
    "enzyme": "^2.4.1",
    "gsap": "^1.19.0",
    "gsap-react-plugin": "^1.0.2",
    "gulp": "^3.9.1",
    "gulp-browserify": "^0.5.1",
    "gulp-minify": "0.0.14",
    "gulp-sourcemap": "^1.0.1",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-uglify": "^2.0.0",
    "gulp-watchify": "^0.6.0",
    "jasmine": "^2.4.1",
    "jasmine-jquery": "^2.1.1",
    "jsdoc": "^3.4.0",
    "karma": "^1.3.0",
    "karma-babel-preprocessor": "^6.0.1",
    "karma-browserify": "^5.1.0",
    "karma-chrome-launcher": "^1.0.1",
    "karma-firefox-launcher": "^1.0.0",
    "karma-html-reporter": "^0.2.7",
    "karma-jasmine": "^1.0.2",
    "karma-jasmine-html-reporter": "^0.2.0",
    "karma-jasmine-jquery": "^0.1.1",
    "karma-nyan-reporter": "^0.2.4",
    "karma-phantomjs-launcher": "^1.0.1",
    "karma-safari-launcher": "^1.0.0",
    "mocha": "^3.0.2",
    "pump": "^1.0.1",
    "radium": "^0.18.1",
    "react": "^15.3.2",
    "react-dom": "^15.3.1",
    "react-native-accordion": "^1.0.1",
    "react-native-collapsible": "^0.7.0",
    "react-native-mock": "^0.2.6",
    "sinon": "^1.17.6",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0",
    "watchify": "^3.7.0",
    "jest": "16.0.1",
    "jest-react-native": "^15.0.0",
    "react-test-renderer": "^15.3.2"
  }

Any idea why it’s not working?

@JeroenNelen this comment should fix your issue: https://github.com/facebook/jest/issues/2208#issuecomment-264733133. If you’re feeling generous, you can contribute by making a PR similar to this one that fixes the issue for everyone else, too.

I think this page is useful for you.

https://github.com/facebook/jest/issues https://github.com/facebook/jest/issues/2208

I have not tested yet. I succeed when I used ‘react-native 0.37’ and ‘jest 17.0.3’

“react”: “15.3.1”, “react-native”: “0.37.0”,

“babel-jest”: “^17.0.2”, “babel-preset-react-native”: “^1.9.0”, “jest”: “17.0.3”, “jest-react-native”: “17.0.3”, “react-test-renderer”: “15.3.0”,

and jest preset is ‘jest-react-native’

But, If you use react-native >= 0.38, you should fix ‘jest-react-native’ –> ‘react-native’

2016-12-05 18:52 GMT+09:00 JeroenNelen notifications@github.com:

So I am on the latest versions of React and React-native and the following versions of jest:

However, I’m bumping into the Invariant Violation as well, mostly due to the react-native-experimental-navigation it seems (I’m using react-native-router-flux):

Invariant Violation: Native module cannot be null.

  at invariant (node_modules/fbjs/lib/invariant.js:38:15)
  at Linking.NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:32:1)
  at new Linking (node_modules/react-native/Libraries/Linking/Linking.js:119:141)
  at Object.<anonymous> (node_modules/react-native/Libraries/Linking/Linking.js:191:16)
  at Object.Linking (node_modules/react-native/Libraries/react-native/react-native.js:91:22)
  at Object.<anonymous> (node_modules/react-native-experimental-navigation/NavigationRootContainer.js:15:36)

When trying to mock: jest.mock(‘react-native-experimental-navigation’, () => ‘Mocked’);

It still throws the error. @MechanicKim https://github.com/MechanicKim , I also have imports like that, to import the container or component that I want to test, so I don’t see how I can remove those imports and still test the code?

Anyone any suggestion or idea?

Kind regards,

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/facebook/jest/issues/1840#issuecomment-264810632, or mute the thread https://github.com/notifications/unsubscribe-auth/ALlkF7fKftElpPNyLMgQLpJR6PuCTKThks5rE97RgaJpZM4KL8lY .

Hey, @cpojer, after updating I’m getting an error that seems to be related to mocking of native modules. Do I need to manually mock it?

In one of my files I have:

export const STATUS_BAR_HEIGHT = (StatusBar.currentHeight || 0);

When I run the tests it gives this error, which seems to be because StatusBarManager is undefined (here)

TypeError: Cannot read property 'HEIGHT' of undefined      
  at Object.<anonymous> (node_modules/react-native/Libraries/Components/StatusBar/StatusBar.js:395:1823)
  at Object.StatusBar (node_modules/react-native/Libraries/react-native/react-native.js:55:24)

this issue happened to me after upgrading react-native to 0.38 and react to 15.4.1

it was fine using react-native 0.33 and react ~15.3.0

@carera installing react-dom doesn’t help