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

"Invariant Violation: Native module cannot be null" when running tests after upgrade from 0.38 to 0.39.1 #11585

Closed
jcollum opened this issue Dec 21, 2016 · 12 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@jcollum
Copy link

jcollum commented Dec 21, 2016

Originally reported here: jestjs/jest#2382 (comment)

Description

https://github.com/jcollum/kishar-nine/tree/02-routing -- RN 0.38

https://github.com/jcollum/kishar-nine/tree/03-redux-counter -- RN 0.39.1

After upgrading to 0.39.1 the tests fail:

$ npm test

> [email protected] test /Users/collumj/research/kishar-nine
> jest

 FAIL  test/index.android.js
  ● Test suite failed to run

    Invariant Violation: Native module cannot be null.
      
      at invariant (node_modules/fbjs/lib/invariant.js:38:15)
      at Linking.NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:32:1)
      at new Linking (node_modules/react-native/Libraries/Linking/Linking.js:119:141)
      at Object.<anonymous> (node_modules/react-native/Libraries/Linking/Linking.js:191:16)
      at Object.get Linking [as Linking] (node_modules/react-native/Libraries/react-native/react-native.js:91:22)
      at Object.<anonymous> (node_modules/react-native-experimental-navigation/NavigationRootContainer.js:15:36)

Could the use of `react-native-experimental-navigation` be the issue? It's in the Router code I think. 

Reproduction

Clone the repo.

$ rm -rf node_modules/ && git checkout 02-routing && npm install --silent && npm test

Tests pass.

rm -rf node_modules/ && git checkout 03-redux-counter && npm install --silent && npm test

Tests fail with above error message.

Additional Information

$ versions
npm 3.10.8
node v6.9.1
OS:
ProductName:	Mac OS X
ProductVersion:	10.11.6
BuildVersion:	15G31

$ cat node_modules/react-native/package.json|grep version
  "version": "0.39.2"

$ react-native --version
react-native-cli: 1.2.0
react-native: 0.39.2


@vinnyoodles
Copy link

I'm getting the same error, this is my package.json

{
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest",
    "ios": "react-native run-ios && react-native log-ios",
    "android": "react-native run-android && react-native log-android",
    "link": "react-native link"
  },
  "dependencies": {
    "react": "15.4.1",
    "react-native": "^0.39.2",
    "react-native-cli": "^2.0.1",
    "react-native-fbsdk": "^0.4.0",
    "react-native-maps": "^0.12.2",
    "react-redux": "^5.0.1",
    "redux": "^3.6.0",
    "redux-thunk": "^2.1.0"
  },
  "devDependencies": {
    "babel-jest": "17.0.2",
    "babel-preset-react-native": "1.9.0",
    "jest": "17.0.3",
    "react-test-renderer": "15.4.1"
  },
  "jest": {
    "preset": "react-native"
  }
}

@brentvatne
Copy link
Collaborator

this error results from creating a NativeEventEmitter on a module that hasn't been mocked

@jcollum
Copy link
Author

jcollum commented Dec 25, 2016

I don't know what to do with that information. I've searched for NativeEventEmitter in all the js in my node_modules folder and there are 141 occurences -- all in the reactnative library.

@bufke
Copy link

bufke commented Dec 27, 2016

I'm getting this too on a very new RN project with jest. Even a trivial test like add two numbers fails. Guessing one of my dependencies includes react-native-experimental-navigation as a dependency. I'm not using it directly.

@michaelAndersonCampingWorld
Copy link
Contributor

michaelAndersonCampingWorld commented Dec 27, 2016

I get this as well. I have several components that reference Linking module in componentDidMount and componentDidUnmount, but the only one that fails is the one where I'm adding/removing event listeners. I am also requiring react-native-mock/mock in the jest setup.

@jcollum
Copy link
Author

jcollum commented Dec 27, 2016

react-native-experimental-navigation

I know mine is, react-native-router-flux (source). Should we report this over there instead?

@MrToph
Copy link

MrToph commented Dec 28, 2016

Same error here using NetInfo

@bufke
Copy link

bufke commented Dec 28, 2016

This fixed it for me (though obviously is a hacky workaround)

@jcollum
Copy link
Author

jcollum commented Dec 28, 2016

I'll give that a shot thanks @bufke

@jcollum
Copy link
Author

jcollum commented Jan 2, 2017

@bufke Looks like it worked. But I don't know why, which would be great. Thanks much! Full solution:

  1. create setup.js in /test (I despise Jest's __ notation 👎, I want test stuff to be in one directory )
  2. add this to setup.js:
jest.mock('Linking', () => {
  return {
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    openURL: jest.fn(),
    canOpenURL: jest.fn(),
    getInitialURL: jest.fn(),
  }
})
  1. Set your jest node in package.json to this:
"jest": {
        "preset": "react-native",
        "testRegex": "./test/[^setup].*.js$",
        "mocksPattern": "./test/mocks/.*.js$",
        "transformIgnorePatterns": ["node_modules/(?!react-native|native-base|react-clone-referenced-element)"],
        "setupFiles": [
            "./test/setup.js"
        ]
    },

@jcollum jcollum closed this as completed Jan 2, 2017
bdougie added a commit to americool/vndr_ideas that referenced this issue Jan 4, 2017
bdougie added a commit to americool/vndr_ideas that referenced this issue Jan 4, 2017
americool pushed a commit to americool/vndr_ideas that referenced this issue Jan 5, 2017
* test setup

* adds first test

* fixes Linking error with react-native-router-flux

facebook/react-native#11585

* move folders
@linhnh123
Copy link

@jcollum I followed 3 step of you, but it still not working.
This fixed it for me:

jest.mock('react-native', () => ({
    NetInfo: {
        addEventListener: jest.fn(),
        fetch: () => {
            return {
                done: jest.fn()
            }
        }
    },
    NativeModules: {
        RNPasscodeStatus: {
            supported: jest.fn(),
            status: jest.fn(),
            get: jest.fn()
        }
    },
    Dimensions: {
        get: () => ({
            width: jest.fn(),
            height: jest.fn()
        })
    },
}));

@SupriyaKalghatgi
Copy link

Adding setup.js worked initially and I did nothing later, it failed again.
Can anyone help me on this?

@facebook facebook locked as resolved and limited conversation to collaborators May 24, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

9 participants