jest: Cannot find module 'ReactNativeDefaultInjection' from 'setup.js'
FAIL __tests__/App-test.js
● Test suite failed to run
Cannot find module 'ReactNativeDefaultInjection' from 'setup.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:142:17)
Test Summary
› Ran all tests.
› 1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 0.242s)
npm ERR! Test failed. See above for more details.
here is my config of jest
"jest": {
"preset": "jest-react-native",
"preprocessorIgnorePatterns": [
"node_modules/(?!react-native|jpush-react-native)"
],
"haste": {
"defaultPlatform": "ios",
"platforms": [
"ios",
"android"
]
},
"modulePathIgnorePatterns": [
"react",
"react-native"
],
"moduleFileExtensions": [
"js",
"jsx"
],
"collectCoverage": false,
"verbose": true,
"testPathIgnorePatterns": [
"/node_modules/",
".+/(resources)/.+",
".+/(autoMockOff.js)"
]
},
"devDependencies": {
"babel-jest": "^15.0.0",
"babel-plugin-module-resolver": "^2.0.0",
"babel-polyfill": "^6.16.0",
"babel-preset-react-native": "^1.9.0",
"babel-preset-react-native-stage-0": "^1.0.1",
"jest": "^15.1.1",
"jest-react-native": "^15.0.0",
"react-test-renderer": "^15.3.2",
"remote-redux-devtools": "^0.4.7"
}
any idea?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 5
- Comments: 42 (14 by maintainers)
We had this issue to using
jest@17.0.2
, in our case was not installing the right version ofreact
forreact-native
We had this in our
package.json
and doing a
npm install
gave us this outputThis was due
react@^15.3.2
resolve toreact@15.4.0
due how npm semver works.Setting the react version to
react@~15.3.2
solved the issue. Change yourpackage.json
to look like this@JeroenNelen If you are using react-native prior to 0.38, use
jest-react-native
locked to version 17.0.3 or lower. If you are using 0.38, usereact-native
as preset or install17.1.0
of jest-react-native which will continue to work (and forward) to react-native for a while.This is happening to me with
but only on my CircleCI VM… locally everything runs fine.
Oh, remove the
haste
config from your Jest configuration. The jest-react-native preset brings this in automatically.Yeah, we moved to more explicit mocks and StatusBar might simply not be mocked properly. See https://github.com/facebook/react-native/blob/master/jest/setup.js#L69 – you can send a PR to RN to fix this and meanwhile you can probably add this mock manually a file that you add to
setupFiles
.Sorry for triple comment. This is not a Jest issue, I guess It’s React issue. React and Haste *
let me explain a bit
require('React')
is actually handled by Haste loader, by@providesModule React
directive.Where it’s located: Source code:
src/isomorphic/React.js
https://github.com/facebook/react/blob/c78464f8ea9a5b00ec80252d20a71a1482210e57/src/isomorphic/React.js#L9 Transpiled version:node_modules/react/lib/React.js
I’ve installed some last versions one-by-one, and this directive was suddendly disappear from transpiled code, throwing error from Haste, which can’t resolve
React
tonode_modules/react
.Versions and directive existence:
What was happen with transpiled source at
15.4.0-rc.2
->15.4.0-rc.3
version bump? I dunno…So. For now, I have react-native
0.38
installed, andAnd
react@~15.4.0-rc.4
don’t hase Haste directive -> tests are broken…What to do?
15.4.0-rc.2
or15.3.2
and wait for fixOh man, i figured this out. We need to install react as dependency or devDependency. 🎉 Cheers !
I did some more research and if I put my test in __ tests __ folder, everything works fine. However, when I move them to /test/jest, and add this param:
"testPathDirs": ["./test/jest"]
Then it starts failing with ReactNativeDefaultInjection error.
I’m having the exact same issue. Here’s my package.json:
Any idea why it’s not working?
@JeroenNelen this comment should fix your issue: https://github.com/facebook/jest/issues/2208#issuecomment-264733133. If you’re feeling generous, you can contribute by making a PR similar to this one that fixes the issue for everyone else, too.
I think this page is useful for you.
https://github.com/facebook/jest/issues https://github.com/facebook/jest/issues/2208
I have not tested yet. I succeed when I used ‘react-native 0.37’ and ‘jest 17.0.3’
“react”: “15.3.1”, “react-native”: “0.37.0”,
“babel-jest”: “^17.0.2”, “babel-preset-react-native”: “^1.9.0”, “jest”: “17.0.3”, “jest-react-native”: “17.0.3”, “react-test-renderer”: “15.3.0”,
and jest preset is ‘jest-react-native’
But, If you use react-native >= 0.38, you should fix ‘jest-react-native’ –> ‘react-native’
2016-12-05 18:52 GMT+09:00 JeroenNelen notifications@github.com:
Hey, @cpojer, after updating I’m getting an error that seems to be related to mocking of native modules. Do I need to manually mock it?
In one of my files I have:
When I run the tests it gives this error, which seems to be because
StatusBarManager
is undefined (here)this issue happened to me after upgrading react-native to 0.38 and react to 15.4.1
it was fine using react-native
0.33
and react~15.3.0
@carera installing
react-dom
doesn’t help