vamsiampolu vamsiampolu - 18 days ago 20
Node.js Question

mocha.opts not working with babel-register

This is my

mocha.opts
file:

--require babel-polyfill
--require jsdom-global/register
--compilers js:babel-register
--watch
--recursive
--bail
--check-leaks
--reporter spec


This is my
.babelrc
file, I am using the
env
preset which transpiles based on the targets provided:

{
"presets":[
"react",
[
"env",
{
"targets":{
"chrome":54,
"node":true
}
}
]
],
"plugins":[
"transform-class-properties",
["transform-object-rest-spread", {
"useBuiltIns":true
}
]
]
}


I find that when I run
mocha
, node objects violently to the use of
import
which works correctly in my code:

/* eslint-env node, mocha*/
import chai, { expect } from 'chai';
import { shallow, mount } from 'enzyme';
import chaiEnzyme from 'chai-enzyme';
chai.use(chaiEnzyme());

describe('EllipsisText', function(){
it('exists', function() {
expect(EllipsisText).to.exist;
});
});


This is the error that mocha throws:

/home/vamsi/Do/css-in-js-test/test/EllipsisText.js:2
import chai, { expect } from 'chai';
^^^^^^
SyntaxError: Unexpected token import
at Object.exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:513:28)
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.require (module.js:468:17)
at require (internal/module.js:20:19)
at /home/vamsi/.nvm/v6.2.0/lib/node_modules/mocha/lib/mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (/home/vamsi/.nvm/v6.2.0/lib/node_modules/mocha/lib/mocha.js:217:14)
at Mocha.run (/home/vamsi/.nvm/v6.2.0/lib/node_modules/mocha/lib/mocha.js:469:10)
at Object.<anonymous> (/home/vamsi/.nvm/v6.2.0/lib/node_modules/mocha/bin/_mocha:404: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 Function.Module.runMain (module.js:575:10)
at startup (node.js:160:18)
at node.js:449:3

Answer

When using babel, mocha needs to be run like this:

mocha --compilers js:babel-core/register
Comments