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

Viewed 4473

I tried to run default test in react-native using jest and I found this problem:

Test suite failed to run

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

  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-clipboard/clipboard/dist/NativeClipboard.js:15:20)
  at Object.<anonymous> (node_modules/@react-native-clipboard/clipboard/dist/Clipboard.js:24:38)

I completly dont know how to solve this problem.

Ive tried to add the following line:

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

to jest.setup.js

or

mocks/native_event_emmiter.js

but neither solution works.

Anyone know how to solve this ?

2 Answers

Have you add setupFiles in your jest.config.js?

This worked for me:

//jest.config.js

setupFiles: ['<rootDir>/jest.setup.js'],
//jest.setup.js

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

In my case I have added a package in package.json and forget to add the config
in ios podfile you can either remove the package from package.json or add the config for ios that helped me. The run npm i

Related