This issue is tighly coupled with issue from jest: https://github.com/facebook/jest/issues/1840
My comment for explanation, why it's related to React: https://github.com/facebook/jest/issues/1840#issuecomment-263004102 . I will dupe some info in current issue.
As 15.4.0-rc.2 -> 15.4.0-rc.3 version update, @providesModule React is dissapeared from production code. Under "production" I mean that code which ships with npm. And, jest tests became broken. And no way to fix it, because of version tags. Latest version with @providesModule is 15.4.0-rc.2, and it just can't be used as a workaround:
npm WARN [email protected] requires a peer of react@~15.4.0-rc.4 but none was installed.
Git tags compare ( https://github.com/facebook/react/compare/v15.3.2...v15.4.0-rc.3 ) shows an interesting commit, https://github.com/facebook/react/commit/2d049e8df67b7c2165fbdf49e61e16f160dd5002
As we can see, that commit comes from this Pull Request: https://github.com/facebook/react/pull/7968
The providesModule system was never a supported way to consume React modules. The recommended way is to require('react') which should still work. Lower case CommonJS.
Is there an issue with using jest in react-native that is depending on require('React') or in your code?
Is there an issue with using jest in react-native that is depending on require('React') or in your code?
I personally don't use mixedcase commonjs ... only lowercase (I hase case-sensitive FS). React-native project builds successfully, so may be it's a bug somewhere in jest internally
cc @yungsters
cc @cpojer
@sebmarkbage I don't think what you are saying is true. We used haste modules from React's (and previously also fbjs) npm package in react-native-packager which is why Jest is whitelisting the package with the jest-react-native preset as well. I'm just getting back from a trip so I'm a bit unsure what happened recently but it seems to me like react-native needs to have a @providesModule React haste module proxy, no?
It does: https://github.com/facebook/react-native/commit/b06b7ae60932d8be08bc792b3c70c32907b871b0
@ColCh does [email protected] help at all? just curious.
That's odd, in that case Jest definitely knows about that React module proxy. When using jest-react-native, things should just work.
Hmm, actually when we updated react-native to 15.4 we had this issue and we added an entry to the moduleNameMapper config. jest-react-native was fully merged into react-native. Simply changing your Jest config to "preset": "react-native" when using RN 0.38+, this should work.
We haven't forwarded jest-react-native to react-native yet, unfortunately. cc @kentaromiura
@sebmarkbage [email protected] not helps
simple grep shows that there is node-haste comment
./node_modules/react-native/Libraries/react-native/React.js: * @providesModule React
And, at same time:
Cannot find module 'React' from 'ReactNativeMount.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:155:17)
at Object.<anonymous> (node_modules/react-native/Libraries/Renderer/src/renderers/native/ReactNativeMount.js:14:11)
at Object.<anonymous> (node_modules/react-native/Libraries/Renderer/src/renderers/native/ReactNative.js:19:22)
In that file /node_modules/react-native/Libraries/Renderer/src/renderers/native/ReactNativeDefaultInjection.js we actually see this import, which can't be resolved:
var React = require('React');
@cpojer it seems that this simple change
diff --git a/package.json b/package.json
index 173950e..770acde 100644
--- a/package.json
+++ b/package.json
@@ -19,7 +19,7 @@
"start": "node node_modules/react-native/local-cli/cli.js start"
},
"jest": {
- "preset": "jest-react-native",
+ "preset": "react-native",
"collectCoverageFrom": [
"scripts/*.js",
"src/**/*.js",
resolved require('React')
Hm... it seems it finally started to work nicely 馃憤 there are some diffs in snapshots, but I can handle it. Thanks!
I'll update jest-react-native today to forward to react-native. I'm also updating Jest's documentation to hint that you won't need jest-react-native any longer as the default setup with 0.38 includes a working Jest configuration.
See #2182.
Most helpful comment
Hmm, actually when we updated react-native to 15.4 we had this issue and we added an entry to the
moduleNameMapperconfig.jest-react-nativewas fully merged into react-native. Simply changing your Jest config to"preset": "react-native"when using RN 0.38+, this should work.We haven't forwarded jest-react-native to react-native yet, unfortunately. cc @kentaromiura