question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest not properly mocking library

See original GitHub issue

When 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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
evollucommented, Oct 1, 2017

will update code fixed in 9.7.2

1reaction
evollucommented, Oct 1, 2017

try replace this like https://github.com/evollu/react-native-fcm/blob/master/index.js#L3 with const EventEmitter = new NativeEventEmitter(NativeModules.RNFIRMessaging || {});

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found