-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jest React Native - Invariant Violation: Native module cannot be null. #2208
Comments
Set the preset to "react-native" and remove |
@cpojer I did it but I got same error.
2016-12-02 22:14 GMT+09:00 Christoph Pojer <[email protected]>:
… Closed #2208 <#2208>.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#2208 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ALlkF0cZy0p2Dnf-cHN7XkLXpu01K2Caks5rEBm_gaJpZM4LCgV1>
.
|
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 The problematic line is package.json:
@cpojer Is there any way to mock out react native Linking globally without duplicating the setup.js file? |
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. |
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 :
|
@cliedeman Worked me for me thanks! However I feel like RN or Jest needs to figure it out. :) |
I think it's more the job of the react-native preset to figure it out, rather than jest itself |
We merged this preset back into react-native; please send mocks and PRs directly there. |
I'll give it a shot when i'll have some time. |
the same thing happened to me, had to
|
Cleaner solution using __mocks__
|
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: facebook/react-native#11585) Any update on this? I was using this workaround: But since updating all my packages to the latest versions today, Jest keeps giving this error: Any clue why it can no longer find the file? It hasn't moved or anything. Kind regards, |
@JeroenNelen I've managed to workaround this with prepending
|
In Jest 18.1, doing |
Should there be a solution to mock custom components directly in jest ? A flow-typed like repository for example. |
That might be cool! |
I have a similar issue. I have a saga that is importing: import Camera from 'react-native-camera'; This throws the error:
I tried to define a Camera mock in jest.mock('Camera', () => {
return {
Aspect: {}
}
}); Which is required in
I also get the Invariant error for other libraries. For example, Pusher. I get the jest.mock('Pusher', () => {
return {
addEventListener: jest.fn(),
removeEventListener: jest.fn()
}
}); This is my jest config in package.json: "jest": {
"preset": "react-native",
"setupFiles": ["./js/jest/setup.js"],
"transformIgnorePatterns": [
"node_modules/(?!react-native||@shoutem)"
]
}, React version: 15.4.2 |
I got it working, but had to add the following to my jest.mock('PushNotificationIOS', () => ({
addEventListener: jest.fn(),
requestPermissions: jest.fn(),
}));
jest.mock('react-native', () => ({
NetInfo: {
addEventListener: jest.fn(),
fetch: () => {
return {
done: jest.fn()
}
}
},
NativeModules: {
RNPasscodeStatus: {
supported: jest.fn(),
status: jest.fn(),
get: jest.fn()
}
},
Dimensions: {
get: () => ({
width: jest.fn(),
height: jest.fn()
})
},
}));
jest.mock('react-native-camera', () => {
}); Is this overkill? Also, I need to explicitly call |
Hmmm, I've had to amend these slightly. I'm using
Because |
For anyone having issues with react-native-router-flux, I had this problem today, and went with this solution #2208 (comment) Quick and painless. |
@andrewhl Were you able to successfully mock pusher? Im having trouble getting my tests fixed because of the |
@kyangy I think in the end I didn't need to. The library that was causing problems was jest.mock('react-native-push-notification', () => {
return {
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
requestPermissions: jest.fn(),
configure: jest.fn()
}
}); Have you tried creating a jest/__mocks__/pusher.js file that looks something like: module.exports = {
// whatever methods or properties are being accessed via the pusher object
} 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. |
Has any one had the same issue with react-native-callkit ?
I've tried the solutions above but I gather I'm not mocking the right functions. Does any one have any ideas on what exactly should be mocked? |
…g-mobile into issue/167-merge-refresh-android * 'master' of https://github.com/wordpress-mobile/gutenberg-mobile: (35 commits) Point to latest from RecyclerViewList Point to latest react-native-aztec Adding yarn.lock field for `turbo-combine-reducers`. Added turbo-combine-reducers dependency to fix failed builds Update subrepo ref to point to master again Update subrepo ref No need for change of default Jest platform Update subrepo ref Update subrepo ref to point to master again Update subrepo ref Update subrepo ref Update subrepo ref Update subrepo ref Add symlink in symlinked-packages-in-parent for notices Add symlink for notices package to fix the failing CI tests Update subrepo by merging from master Add support for nextpage block Fix lint warnings Mocking `react-native-gutenberg-bridge` to make iOS tests pass. From: jestjs/jest#2208 (comment) rn-gutenberg-bridge: Adding dumb iOS implementation to avoid errors. ...
…g-mobile into issue/167-merge-blocks-different-types * 'master' of https://github.com/wordpress-mobile/gutenberg-mobile: (27 commits) Point to latest from RecyclerViewList Point to latest react-native-aztec Adding yarn.lock field for `turbo-combine-reducers`. Added turbo-combine-reducers dependency to fix failed builds Update subrepo ref to point to master again Update subrepo ref No need for change of default Jest platform Update subrepo ref Update subrepo ref Fix lint warnings Mocking `react-native-gutenberg-bridge` to make iOS tests pass. From: jestjs/jest#2208 (comment) rn-gutenberg-bridge: Adding dumb iOS implementation to avoid errors. Revert "Update gutenberg subproject ref" Add react-native-hr library to add styled line+text elements easily Update gutenberg subproject ref Match wpandroid's Android API levels Remove plugin from Babel to make wpandroid release build work Fix wpandroid checkstyle issues Remove dep, Matro will find the package automagically Fix indentation ... # Conflicts: # gutenberg
…g-mobile into port-quote-block-step-1 * 'master' of https://github.com/wordpress-mobile/gutenberg-mobile: (27 commits) Point to latest from RecyclerViewList Point to latest react-native-aztec Adding yarn.lock field for `turbo-combine-reducers`. Added turbo-combine-reducers dependency to fix failed builds Update subrepo ref to point to master again Update subrepo ref No need for change of default Jest platform Update subrepo ref Update subrepo ref Fix lint warnings Mocking `react-native-gutenberg-bridge` to make iOS tests pass. From: jestjs/jest#2208 (comment) rn-gutenberg-bridge: Adding dumb iOS implementation to avoid errors. Revert "Update gutenberg subproject ref" Add react-native-hr library to add styled line+text elements easily Update gutenberg subproject ref Match wpandroid's Android API levels Remove plugin from Babel to make wpandroid release build work Fix wpandroid checkstyle issues Remove dep, Matro will find the package automagically Fix indentation ... # Conflicts: # gutenberg
getting same error message after implementing react-native-gps-state module into the project.can someone please suggest how to mock one. |
jest.mock('react-native-gps-state', () => {
return {
nativeMethodToMock: jest.fn(),
anotherNativeMethod: jest.fn()
}
}) |
came across this when using react-navigation-drawer. Created mocks/react-navigation-drawer.js with
But then the error gets replaced with createDrawerNavigator is not a function. Am I doing something wrong? |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
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.
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
andnpm test
.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
The text was updated successfully, but these errors were encountered: