Originally reported here: https://github.com/facebook/jest/issues/2382#issuecomment-268331120
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.
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.
$ 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
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"
}
}
this error results from creating a NativeEventEmitter on a module that hasn't been mocked
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.
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.
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.
react-native-experimental-navigation
I know mine is, react-native-router-flux (source). Should we report this over there instead?
Same error here using NetInfo
This fixed it for me (though obviously is a hacky workaround)
I'll give that a shot thanks @bufke
@bufke Looks like it worked. But I don't know why, which would be great. Thanks much! Full solution:
/test (I despise Jest's __ notation 👎, I want test stuff to be in one directory ) jest.mock('Linking', () => {
return {
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
openURL: jest.fn(),
canOpenURL: jest.fn(),
getInitialURL: jest.fn(),
}
})
"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 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()
})
},
}));
Adding setup.js worked initially and I did nothing later, it failed again.
Can anyone help me on this?
Most helpful comment
This fixed it for me (though obviously is a hacky workaround)