I have created a project using the standard boilerplate, then added babel-plugin-module-resolver: "2.7.1"to the package and in.babelrc`:
"plugins": [
["module-resolver", {
"alias": {
"mock": "./config/mock",
"fs": "./config/mock",
"sodium-universal": "libsodium",
"crypto": "react-native-crypto",
"path": "path-browserify"
}
}]
]
This because I have some node-based dependencies (without UI) that I want to reuse in my RN app.
With this plugin Babel neatly replaces imports and requires throughout node_modules.
When running the emulator I get an error complaining that fs.readFileSync is undefined, which is completely expected, as fs is just a mock, but is invoked from one of those node-related dependencies.
Now when running Jest unit tests that invoke the same methods, using yarn test (defined as "NODE_ENV=test jest --no-cache" in package.json) or in any other way I could think of, the tests succeed, where they should also fail!
So seems like Jest is completely ignoring the bundle and happily uses the original sources from disk.
Steps to reproduce:
fs to an empty js fileignite doctor results:
System
platform linux
arch x64
cpu 4 cores Intel(R) Core(TM) i7-4500U CPU @ 1.80GHz
JavaScript
node 7.10.1 /usr/local/bin/node
npm 4.2.0 /usr/local/bin/npm
yarn 0.24.6 /usr/bin/yarn
React Native
react-native-cli 2.0.1
app rn version 0.45.1
Ignite
ignite 2.0.0 /usr/local/bin/ignite
Android
java 1.8.0_111 /usr/bin/java
android home - undefined
PS. This may be more of a Jest or babel-jest issue, but I'm filing here to start with :)
Do you experience the same issue without ignite as well? react-native init Strangeness --version 0.45.1
If you mean after entering react-native init Strangeness --version 0.45.1 and yarn test, then yes the results are the same.
Gotcha. I'll close this up as this isn't actionable on our end.
If you do get some answers though, I'd love to hear how this gets solved. Maybe a mockfs library or something?
Thanks, I'll report back.
Hi @skellock
I received a response from the Jest team, but I don't understand it.
This is the expected behavior. Jest is supposed to run on source files. On any large codebase, generating a bundle before running tests is too slow, so Jest compiles files just-in-time, which makes it fast.
Have asked for more info. Could you have a quick look as well? Thanks.
https://github.com/facebook/jest/issues/4028#issuecomment-315273833
@skellock I did not receive any more help from Jest team. Going to switch to mocha, jasmine and/or karma. Thanks for your help earlier!
Sorry man. I know how frustrating it can be when you don鈥檛 get any answers.
Ah that's all-right. Its also part of an average developers' life. We should just live with that.. :wink:
PS I left a pointer on SO for someone who wants to harvest some reputation by solving this issue: https://stackoverflow.com/questions/45056952/debugging-jest-unit-tests-with-breakpoints-in-vs-code-with-react-native/45073796#comment77211417_45073796
Most helpful comment
Ah that's all-right. Its also part of an average developers' life. We should just live with that.. :wink: