Jest not properly mocking library
See original GitHub issueWhen trying to run tests on Jest for my app which uses react-native-fcm I get error:
Invariant Violation: Native module cannot be null.
at invariant (node_modules/fbjs/lib/invariant.js:44:15)
at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:32:1)
at Object.<anonymous> (node_modules/react-native-fcm/index.js:3:18)
at Object.<anonymous> (app/components/MyComponent.js:6:21)
(MyComponent.js:6
is import FCM from 'react-native-fcm';
)
Version info:
react-native-fcm@9.6.2
react-native@0.44.3
This problem has been earlier reported, but closed as https://github.com/evollu/react-native-fcm/issues/435
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Manual Mocks - Jest
In order to mock properly, Jest needs jest.mock('moduleName') to be in the same scope as the require/import statement.
Read more >Mocking a module with Jest not working properly
I was able to make it work this way: import * as AWS from 'aws-sdk'; import { uploadFile } from '.'; const mockPutObject...
Read more >jest.mock factory doesn't work inside a test #2582 - GitHub
So jest.mock is being hoisted to the function scope, that's why it won't work with require s (and definitely not import s ...
Read more >Mock a Library in Jest and Typescript | by Anthony Ng - Medium
It is crucial to be notified when the endpoint was not working. However, it is not the concern of the unit test. Our...
Read more >JavaScript testing #10. Advanced mocking with Jest and ...
If the React Testing Library does not find the element during that time, it throws an error, and our test fails. Mocking a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
will update code fixed in 9.7.2
try replace this like https://github.com/evollu/react-native-fcm/blob/master/index.js#L3 with
const EventEmitter = new NativeEventEmitter(NativeModules.RNFIRMessaging || {});