react-native-background-timer icon indicating copy to clipboard operation
react-native-background-timer copied to clipboard

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

Open shensven opened this issue 3 years ago • 18 comments

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

shensven avatar Oct 07 '21 08:10 shensven

+1

ronaldaraujo avatar Oct 20 '21 13:10 ronaldaraujo

#72 jest.mock("react-native-background-timer", () => {});

s956142 avatar Oct 25 '21 11:10 s956142

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');

princefishthrower avatar Oct 28 '21 09:10 princefishthrower

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

ghost avatar Jun 21 '22 10:06 ghost

This might also be a solution: https://github.com/react-native-device-info/react-native-device-info/issues/1360#issuecomment-1004822298

mizutani256 avatar Jun 24 '22 18:06 mizutani256

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');

@princefishthrower

this helped a lot.=, Thanks man :)

ivelinov94 avatar Jul 31 '22 10:07 ivelinov94

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');

please where do I find this setup file? as all the answers seem vague

AdamuAbba avatar Oct 08 '22 16:10 AdamuAbba

@AdamuAbba if you have Jest installed in your project you will find this file in the root of your project. The name of the file is jest.setup.js. If you are using VsCode, you can press Ctrl+P / Command+P and search by file name. Open that file and inside just paste the line mentioned by @princefishthrower

NanyThery avatar Oct 20 '22 08:10 NanyThery

@AdamuAbba if you have Jest installed in your project you will find this file in the root of your project. The name of the file is jest.setup.js. If you are using VsCode, you can press Ctrl+P / Command+P and search by file name. Open that file and inside just paste the line mentioned by @princefishthrower

OMG, this is the best reply so far, so direct and straight to the point. thanks, you're a lifesaver 🔥

AdamuAbba avatar Oct 20 '22 09:10 AdamuAbba

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');

@AdamuAbba I get jest is not defined if I add this in my jest.setup.js file

Balthazar33 avatar Oct 26 '22 07:10 Balthazar33

@Balthazar33 did you try importing jest like so at the top of your jest.setup.js file ?

import {jest} from '@jest/globals'

in an ideal environment, this should not be necessary tho for global modules ..... i think.

AdamuAbba avatar Oct 26 '22 10:10 AdamuAbba

jest.setup.js

after adding the above line I am getting this error : TypeError: _reactNative.NativeEventEmitter is not a constructor

shariqahmedAirasia avatar Feb 13 '23 08:02 shariqahmedAirasia

jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');

jest.setup.json file doesn't exist

TomJerry56 avatar Jun 27 '23 09:06 TomJerry56

You have to create it

El El mar, 27 jun 2023 a las 11:35, TomJerry56 @.***> escribió:

jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');

jest.setup.json file doesn't exist

— Reply to this email directly, view it on GitHub https://github.com/ocetnik/react-native-background-timer/issues/367#issuecomment-1609139455, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALKLCYPGUURQLCEGIEBDBQ3XNKSN7ANCNFSM5FQWR2BQ . You are receiving this because you commented.Message ID: @.***>

-- Nadine Thêry

NadineThery.com | Linkedin https://www.linkedin.com/in/nadinethery/ | Twitter http://www.twitter.com/nanythery |

NanyThery avatar Jun 27 '23 09:06 NanyThery

You have to create it El El mar, 27 jun 2023 a las 11:35, TomJerry56 @.> escribió: jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter'); jest.setup.json file doesn't exist — Reply to this email directly, view it on GitHub <#367 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALKLCYPGUURQLCEGIEBDBQ3XNKSN7ANCNFSM5FQWR2BQ . You are receiving this because you commented.Message ID: @.> -- Nadine Thêry NadineThery.com | Linkedin https://www.linkedin.com/in/nadinethery/ | Twitter http://www.twitter.com/nanythery |

getting same error, Do I need add any other changes in tsconfig related to jest.setup.js?

TomJerry56 avatar Jun 27 '23 09:06 TomJerry56

jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter'); seems to work for me if I put it in the single test file, but not in jest.setup.json (I checked that the file is correctly loaded, and tried both with and without import { jest } from '@jest/globals'; I am curious as to why one works and not the other.

smarchesini-DQ avatar Jun 30 '23 12:06 smarchesini-DQ

these two lines in 'jest.setup.js' fixed the issue for the same error in react-native-device-info

import mockRNDeviceInfo from "react-native-device-info/jest/react-native-device-info-mock"
jest.mock("react-native-device-info", () => mockRNDeviceInfo)

maybe it's a similar issue

davidecarpini avatar Jul 13 '23 16:07 davidecarpini

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');

Great, this line wasn't mandatory on my case, but helped me to identify the correct problem, because show the real problem. Thank you very much!

anaarezo avatar Nov 17 '23 13:11 anaarezo