Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Native, Invariant Violation: Native module cannot be null. #6382

Closed
ronnaf opened this issue Jun 2, 2018 · 7 comments
Closed

React Native, Invariant Violation: Native module cannot be null. #6382

ronnaf opened this issue Jun 2, 2018 · 7 comments

Comments

@ronnaf
Copy link

ronnaf commented Jun 2, 2018

🐛 Bug Report

hey, hi! i have a similar issue (i think) here: #2208, but this issue is quite old, and i have followed the solutions there, but none worked for me.

at first, i encountered this error for running jest:

/home/nrion/Desktop/mobile-ui/PriceInsight_app/node_modules/react-native-onesignal/index.js:4
    import { NativeModules, NativeEventEmitter, NetInfo, Platform } from 'react-native';
    ^^^^^^
    
    SyntaxError: Unexpected token import

    > 1 | import OneSignal from 'react-native-onesignal';
      2 | 
      3 | export const STORE_TIMEFRAME = 'STORE_TIMEFRAME';
      4 | export const STORE_TIMEFRAMES = 'STORE_TIMEFRAMES';
      
      at ScriptTransformer._transformAndBuildScript (../../../../../usr/lib/node_modules/jest-cli/node_modules/jest-runtime/build/script_transformer.js:316:17)
      at Object.<anonymous> (src/actions/subscription.js:1:387)
      at Object.<anonymous> (src/reducers/subscription.js:1:402)

so i went ahead and added react-native-onesignal to package.json as such:

"transformIgnorePatterns": [
      "node_modules/(?!(react-native-onesignal|react-native-login|react-native-elements)/)"
    ]

so that solved my problem. but here comes another problem:

 Invariant Violation: Native module cannot be null.

    > 1 | import OneSignal from 'react-native-onesignal';
      2 | 
      3 | export const STORE_TIMEFRAME = 'STORE_TIMEFRAME';
      4 | export const STORE_TIMEFRAMES = 'STORE_TIMEFRAMES';
      
      at invariant (node_modules/fbjs/lib/invariant.js:42:15)
      at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:37:31)
      at Object.<anonymous> (node_modules/react-native-onesignal/index.js:9:33)

i tried mocking OneSignal, but this is what happens when I do that:

 Cannot find module 'OneSignal' from 'Main.test.js'

    > 1 | import React from 'react'
      2 | import { shallow } from '../../jest-setup'
      3 | import configureStore from 'redux-mock-store'
      4 | import OneSignal from 'react-native-onesignal'
      
      at Resolver.resolveModule (../../../../../usr/lib/node_modules/jest-cli/node_modules/jest-resolve/build/index.js:169:17)
      at Object.<anonymous> (src/__tests__/Main.test.js:1:108)

To Reproduce

Steps to reproduce the behavior:

  1. npm install react-native-onesignal
  2. run jest

Expected behavior

test passes, writes a snapshot

@thymikee
Copy link
Collaborator

thymikee commented Jun 2, 2018

How do you mock OneSignal? Can you provide a minimal repro?

@ronnaf
Copy link
Author

ronnaf commented Jun 2, 2018

@thymikee on the test file:

jest.mock('OneSignal', () => {
  return {
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    inFocusDisplaying: jest.fn()
  }
})

@thymikee
Copy link
Collaborator

thymikee commented Jun 2, 2018

Oh, you need to mock the react-native-onesignal, not OneSignal. Mocking is for modules and OneSignal is not a module - it's a variable name.

jest.mock('react-native-onesignal', () => {
  return {
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    inFocusDisplaying: jest.fn()
  }
})

should do the trick

@ronnaf
Copy link
Author

ronnaf commented Jun 4, 2018

@thymikee wow, i'm so stupid. thanks for this!

@ronnaf ronnaf closed this as completed Jun 4, 2018
@Pallavisoba
Copy link

Pallavisoba commented Feb 13, 2020

Hi, I'm a having an issue while running my app "Invariant Violation: Native module cannot be null"

Can someone please help for that
Thanks

@felansu
Copy link

felansu commented Oct 9, 2020

+1 @Pallavisoba

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants