Jest: Invariant Violation: `new NativeEventEmitter()` requires a non-null argument.

See original GitHub issue

The following error will be reported if jest is not configured

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

    Invariant Violation: `new NativeEventEmitter()` requires a non-null argument.

       6 | import {IconButton, TouchableRipple} from 'react-native-paper';
       7 | import Ionicons from 'react-native-vector-icons/Ionicons';
    >  8 | import BackgroundTimer from 'react-native-background-timer';
         | ^
       9 | import dayjs from 'dayjs';
      10 | import relativeTime from 'dayjs/plugin/relativeTime';
      11 | import 'dayjs/locale/zh-cn';

      at invariant (node_modules/invariant/invariant.js:40:15)
      at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:44:7)
      at Object.<anonymous> (node_modules/react-native-background-timer/index.js:10:17)
      at Object.<anonymous> (src/screens/Home.tsx:8:1)
      at Object.<anonymous> (App.tsx:6:1)
      at Object.<anonymous> (__tests__/App-test.tsx:7:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        4.29 s, estimated 7 s
Ran all test suites.
error Command failed with exit code 1.

So maybe we should configure jest manually, like:

// jest.setup.js
jest.doMock('react-native-background-timer', () => {
  return {
    stopBackgroundTimer: jest.fn(),
    runBackgroundTimer: jest.fn(),
  };
});

This allows the unit tests to pass with flying colors

repo here: https://github.com/shensven/ReadHubn/tree/5806faf354fbd178827aff9985efe4dea906c24d

test here: https://github.com/shensven/ReadHubn/runs/3824410010?check_suite_focus=true

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11

github_iconTop GitHub Comments

92reactions
princefishthrowercommented, Oct 28, 2021

The suggested solution didn’t work for me.

While this error appears to come from react-native-background-timer, the true culprit, if you look carefully, is react-native/Libraries/EventEmitter/NativeEventEmitter, I solved this by adding the following to my jest.setup.js :

jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');
2reactions
gweatherstoncommented, Jun 21, 2022

i seem to be getting another error after doing this? i’m currently trying to mock deviceInfo module like so:

jest.mock('react-native-device-info', () => ({
  ...jest.requireActual('react-native-device-info'),
  getBuildNumber: jest.fn(() => 99),
}));

i got this error : Invariant Violation: new NativeEventEmitter() requires a non-null argument.

After adding the jest mock for NativeEventEmitter i get this error:

 react-native-device-info: NativeModule.RNDeviceInfo is null. To fix this issue try these steps:
      • For react-native <= 0.59: Run `react-native link react-native-device-info` in the project root.
      • Rebuild and re-run the app.
      • If you are using CocoaPods on iOS, run `pod install` in the `ios` directory and then rebuild and re-run the app. You may also need to re-open Xcode to get the new pods.
      If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-device-info/react-native-device-info
Read more comments on GitHub >

github_iconTop Results From Across the Web

Invariant Violation: `new NativeEventEmitter()` requires a non ...
I tried to run default test in react-native using jest and I found this problem: Test suite ...
Read more >
`new nativeeventemitter()` requires a non-null argument. - You ...
I tried to run default test in react-native using jest and I found this problem: Test suite failed to run Invariant Violation: `new...
Read more >
new NativeEventEmitter() requires a non-null argument-React ...
Coding example for the question Invariant Violation: new NativeEventEmitter() requires a non-null argument-React Native.
Read more >
NativeEventEmitter() Broken After Update From RN 0.64.2 To ...
constructor() { this.lightning = NativeModules. ... Invariant Violation: `new NativeEventEmitter()` requires a non-null argument.
Read more >
ERROR Error: Requiring module “node_modules/react-native ...
ERROR Invariant Violation: `new NativeEventEmitter()` requires a non-null argument. Solution: cd into the /ios directory cd ios. Once you are in this directory, ......
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