当我尝试使用mocha运行单元测试时,出现以下错误:
> plan@1.0.0 test /root/project/plan
> NODE_PATH=app/scripts:test mocha
/root/project/plan/test/specHelper.js:3
import chai from 'chai';
^^^^^^
SyntaxError:
Unexpected reserved word
------------------------
显然mocha
无法将babel应用于新的es6语法。
以下是specHelper
文件的内容:
import chai from 'chai';
import sinonChai from 'sinon-chai';
import chaiEnzyme from 'chai-enzyme';
chai.use(sinonChai);
chai.use(chaiEnzyme());
chai.config.includeStack = true;
GLOBAL.expect = chai.expect;
GLOBAL.AssertionError = chai.AssertionError;
GLOBAL.Assertion = chai.Assertion;
GLOBAL.assert = chai.assert;
以下是mocha.opts
--reporter min
--ui bdd
--compilers js:babel-register,less:test/ignoreCompiler,css:test/ignoreCompiler
--recursive
当我运行webpack
来构建源代码时,我没有遇到任何问题。
NODE_ENV=production webpack -p --config webpack.config.js
Webpack.config.js
config.devtool = 'cheap-module-eval-source-map';
config.entry.push('webpack-dev-server/client?http://127.0.0.1:8892');
config.entry.push('webpack/hot/only-dev-server');
config.entry.push('react-hot-loader/patch');
config.output.publicPath = 'http://127.0.0.1:8892/';
config.module.rules.push({
test: /\.js$/,
use: ['babel-loader'],
exclude: /node_modules/,
include: [path.join(__dirname, 'app'), path.join(__dirname, 'test')],
});
config.plugins.push(new webpack.HotModuleReplacementPlugin());
config.plugins.push(new webpack.NoErrorsPlugin());
.babelrc
{
"presets": [
["env", {
"modules": false,
"targets": {
"browsers": ["last 2 versions", "ie >= 10"]
}
}],
"react",
"stage-0"
],
"env": {
"es": {
"presets": [["env", { "loose": true, "modules": false }], "react", "stage-0"]
},
"test": {
"presets": [
["env", {
"targets": {
"node": "7.10.1"
}
}],
"react",
"stage-0"
]
}
}
}
在配置方面我错过了什么并导致了SyntaxError
?