jest: Jest React Native - Invariant Violation: Native module cannot be null.
Do you want to request a feature or report a bug? bug
What is the current behavior? When I run ‘npm run jest’, I got an error like this. 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.get Linking [as 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)
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can npm install
and npm test
.
- cd react-native-router-flux
- npm install
- cd Example
- npm install
- npm run jest
https://github.com/MechanicKim/react-native-router-flux
What is the expected behavior? Pass the jest test and update snapshot
Run Jest again with --debug
and provide the full configuration it prints. Please mention your node and npm version and operating system.
Darwin 16.1.0
node v7.2.0
npm v3.10.9
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 11
- Comments: 27 (4 by maintainers)
Commits related to this issue
- Fix jest tests with mobx See https://github.com/mobxjs/mobx-react/issues/186 and https://github.com/facebook/jest/issues/2208#issuecomment-264733133 — committed to saimonmoore/JamOn by saimonmoore 7 years ago
- fix jest test [https://github.com/facebook/jest/issues/2208] — committed to corymayer/react-native-router-flux by corymayer 7 years ago
- fix jest test [https://github.com/facebook/jest/issues/2208] — committed to corymayer/react-native-router-flux by corymayer 7 years ago
- Mocking `react-native-gutenberg-bridge` to make iOS tests pass. From: https://github.com/facebook/jest/issues/2208#issuecomment-264733133 — committed to wordpress-mobile/gutenberg-mobile by etoledom 6 years ago
- Merge branch 'master' of https://github.com/wordpress-mobile/gutenberg-mobile into issue/167-merge-refresh-android * 'master' of https://github.com/wordpress-mobile/gutenberg-mobile: (35 commits) P... — committed to wordpress-mobile/gutenberg-mobile by daniloercoli 6 years ago
- Merge branch 'master' of https://github.com/wordpress-mobile/gutenberg-mobile into issue/167-merge-blocks-different-types * 'master' of https://github.com/wordpress-mobile/gutenberg-mobile: (27 commi... — committed to wordpress-mobile/gutenberg-mobile by daniloercoli 6 years ago
- Merge branch 'master' of https://github.com/wordpress-mobile/gutenberg-mobile into port-quote-block-step-1 * 'master' of https://github.com/wordpress-mobile/gutenberg-mobile: (27 commits) Point to ... — committed to wordpress-mobile/gutenberg-mobile by daniloercoli 6 years ago
Figured it out
Created a file jest/setup.js with the following content
Add changed my package.json
Although there has been some change behaviour because previously this mock was not required.
Set the preset to “react-native” and remove
jest-react-native
. It isn’t needed any more with 0.38!Cleaner solution using __mocks__ Create __mocks__ directory next to __tests__ and place react-native.js with
the same thing happened to me, had to
Hmmm, I’ve had to amend these slightly. I’m using
react-native-push-notification
version2.2.1
.Because
requestPermissions
andgetInitialNotification
seem to return a promise now. Would love this to be stable.I am also getting this error.
Steps to reproduce:
The current jest setup.js does not mock out Linking.
I was able to get past the error by adding
After
AsyncLocalStorage {...},
The problematic line is
const Linking = require('react-native').Linking;
from the react-native-experimental-navigation fork that react-native-router-flux uses.package.json:
@cpojer Is there any way to mock out react native Linking globally without duplicating the setup.js file?
@kyangy I think in the end I didn’t need to. The library that was causing problems was
react-native-push-notification
, which I mocked as follows:Have you tried creating a jest/__mocks__/pusher.js file that looks something like:
I was able to narrow down exactly what libraries and methods needed to be mocked by systematically removing all imports from the component/file under test, and commenting out a lot of code, and reintroducing things one at a time. I mocked whatever caused the test to squawk.
In Jest 18.1, doing
./jest-setup.js
will be resolved from the location of package.json.We merged this preset back into react-native; please send mocks and PRs directly there.
I’ve got this problem as well on RN 38 with Jest 17.0.3. The problem is coming from NetInfo and react-native-router-flux. This is how i Implemented this from your solution :
@JeroenNelen I’ve managed to workaround this with prepending
<rootDir>
:Edited; I had to refer to the file via “./jest/setup.js” since updating, before the “jest/setup.js” was working without any issues (I found the solution here: https://github.com/facebook/react-native/issues/11585)
Any update on this? I was using this workaround:
"jest": { "preset": "react-native", "setupFiles": [ "jest/setup.js" ], "collectCoverage": true, "verbose": true },
But since updating all my packages to the latest versions today, Jest keeps giving this error:
Error: Jest: Module "jest/setup.js" in the "setupFiles" option was not found.
Any clue why it can no longer find the file? It hasn’t moved or anything.
Kind regards,
I have a similar issue. I have a saga that is importing:
This throws the error:
I tried to define a Camera mock in
./js/jest/setup.js
:Which is required in
package.json
setupFiles
. All my tests start throwing the error:I also get the Invariant error for other libraries. For example, Pusher. I get the
Cannot find module
warning if I try to mock Pusher:This is my jest config in package.json:
React version: 15.4.2 React-native version: ^0.40.0 Jest version: ^19.0.0
I think it’s more the job of the react-native preset to figure it out, rather than jest itself