这是我的mocha.opts
文件:
--require babel-polyfill
--require jsdom-global/register
--compilers js:babel-register
--watch
--recursive
--bail
--check-leaks
--reporter spec
这是我的.babelrc
文件,我使用env
预设,根据提供的目标进行转换:
{
"presets":[
"react",
[
"env",
{
"targets":{
"chrome":54,
"node":true
}
}
]
],
"plugins":[
"transform-class-properties",
["transform-object-rest-spread", {
"useBuiltIns":true
}
]
]
}
我发现当我运行mocha
时,节点对象暴力使用import
,这在我的代码中正常工作:
/* 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;
});
});
这是mocha抛出的错误:
/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
答案 0 :(得分:0)
使用babel时,mocha需要像这样运行:
mocha --compilers js:babel-core/register