React-360: `StyleSheet` not found in Mocha unit tests

Created on 10 Mar 2017  ·  3Comments  ·  Source: facebookarchive/react-360

Description

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)

Expected behavior

Able to run tests that import react-vr.

Actual behavior

Error thrown on importing StyleSheet coming from react-vr/Libraries/react-vr.js.

Reproduction

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.

Additional Information

├── [email protected]
├── [email protected]

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.

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jordanpapaleo picture jordanpapaleo  ·  3Comments

baloo887 picture baloo887  ·  3Comments

tlakomy picture tlakomy  ·  3Comments

borisyankov picture borisyankov  ·  4Comments

ryanf12 picture ryanf12  ·  3Comments