mocha和es6:意外的保留字

时间:2018-03-09 02:23:48

标签: ecmascript-6 mocha

当我尝试使用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

0 个答案:

没有答案