react-native-material-design
react-native-material-design copied to clipboard
Testing with Enzyme/Mocha: Unexpected token export
I have a React Native component that imports some stuff from this package. I am trying to test this component with enzyme, using Mocha as the driver. I get the following error:
$ mocha --require react-native-mock/mock --compilers js:babel-core/register --recursive imports/**/*.tests.js
/Users/jcursi/Sites/joncursi/redbirdNative/node_modules/react-native-material-design/lib/index.js:1
(function (exports, require, module, __filename, __dirname) { export { COLOR, TYPO, PRIMARY_COLORS, THEME_NAME } from './config';
^^^^^^
SyntaxError: Unexpected token export
at Object.exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:513:28)
at Module._extensions..js (module.js:550:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/jcursi/Sites/joncursi/redbirdNative/node_modules/babel-register/lib/node.js:159:7)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (imports/ui/components/shared/ActionButtons/ActionButtons.jsx.js:1:1)
at Module._compile (module.js:541:32)
at loader (/Users/jcursi/Sites/joncursi/redbirdNative/node_modules/babel-register/lib/node.js:151:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/jcursi/Sites/joncursi/redbirdNative/node_modules/babel-register/lib/node.js:161:7)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (imports/ui/components/shared/ActionButtons/ActionButtons.tests.js:9:1)
at Module._compile (module.js:541:32)
at loader (/Users/jcursi/Sites/joncursi/redbirdNative/node_modules/babel-register/lib/node.js:151:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/jcursi/Sites/joncursi/redbirdNative/node_modules/babel-register/lib/node.js:161:7)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at /Users/jcursi/Sites/joncursi/redbirdNative/node_modules/mocha/lib/mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (/Users/jcursi/Sites/joncursi/redbirdNative/node_modules/mocha/lib/mocha.js:217:14)
at Mocha.run (/Users/jcursi/Sites/joncursi/redbirdNative/node_modules/mocha/lib/mocha.js:485:10)
at Object.<anonymous> (/Users/jcursi/Sites/joncursi/redbirdNative/node_modules/mocha/bin/_mocha:405:18)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (node.js:348:7)
at startup (node.js:140:9)
at node.js:463:3
npm ERR! Test failed. See above for more details.
Not sure how to get past this error. Do I need to install some babel plugins to get it to recognize the export keyword being used by this package?
I have tried adding the babel export plugin but no luck
https://babeljs.io/docs/plugins/transform-export-extensions/
We ran into the same issue with most of the react-native modules, the issue is that babel by default won't include and compile node_modules.
We ended up writing a mocha compiler to get babel to compile react-native-* node modules (as we also had issues if we just compiled everything...)
@mlabrum would you be able to paste a gist of what you did to get babel to cooperate? This would be a great addition to open source
For those looking for a work-around, I found this blog post to be extremely helpful! https://blog.addjam.com/testing-react-native-with-mocha-and-enzyme-6b77cd9e52a1#.88r68mxuz
I'm having this exact issue as well.