I'm trying to write some tests for my code. I set up Mocha and all the necessary Babel transforms, and now I'm running into this error:
> mocha --require react-native-mock/mock.js --require test/setup.js --compilers js:babel-core/register --recursive test/
module.js:471
throw err;
^
Error: Cannot find module 'StyleSheet'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.get StyleSheet [as StyleSheet] (/path/to/project/node_modules/react-vr/Libraries/react-vr.js:39:29)
at Object.<anonymous> (/path/to/project/app/Something.js:105:16)
at Module._compile (module.js:570:32)
at loader (/path/to/project/node_modules/babel-register/lib/node.js:144:5)
Able to run tests that import react-vr.
Error thrown on importing StyleSheet coming from react-vr/Libraries/react-vr.js.
I guess I could create a sample project if it helps. But basically set up Mocha and import react-vr or a module that imports react-vr in a test.
├── [email protected]
├── [email protected]
React VR is similar to React Native in that internally, it uses FB's "haste" style of imports (const StyleSheet = require('StyleSheet')). This may be the source of your issue. The solution is to treat it like React Native (because it is 😎 ) and make sure your tests have a resolver that can handle haste imports. Anything that is designed to work for React Native will work here.
I have an update to the project creation CLI planned that initializes your app with everything you need for Jest unit testing, again following very much in step with how the RN CLI works these days.
Thanks
Can you share the details on how you have fixed this issue? I am running into the same issue when I try to setup Cucumber test for React Native. Thank you.
Most helpful comment
Can you share the details on how you have fixed this issue? I am running into the same issue when I try to setup Cucumber test for React Native. Thank you.