Mocha为ES6对象散布运算符引发了意外的令牌错误 [英] Mocha throws unexpected token error for ES6 object spread operator

查看:106
本文介绍了Mocha为ES6对象散布运算符引发了意外的令牌错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行mocha时,如果我在应用程序中使用对象传播运算符,它将突然中断并显示Unexpected token错误:

When I run mocha, it abruptly interrupts with an Unexpected token error if I use the object spread operator in my app:

SyntaxError: ../app/middleware/api.js: Unexpected token (30:13)
  28 |
  29 |   // Dispatch beginning action
> 30 |   dispatch({ ...payload, type: startAction });

Object.assign替换它可以解决此问题.我在webpack.config.js文件中包含了transform-object-rest-spread插件,如下所示:

Replacing it with Object.assign fixes the issue. I've included the transform-object-rest-spread plugin in the webpack.config.js file like so:

  module: {
    loaders: [{
      test: /\.js?$/,
      exclude: /(node_modules|bower_components)/,
      loader: 'babel',
      query: {
        presets: ['stage-2','es2015','react'],
        plugins: ['transform-class-properties', 'transform-object-rest-spread']
      }
    }]
  }

该应用程序在浏览器中运行良好,这使我相信像这样运行摩卡咖啡:

The app works fine in the browser, which leads me to believe that running mocha like this:

mocha './app/tests/*.spec.js' --compilers js:babel-register --recursive

不包括babel插件.

does not include the babel plugins.

有没有办法让它们包括在内?

Is there a way to make it include them?

推荐答案

由于您的配置位于webpack.config.js文件中,因此Babel仅在通过Webpack运行时才编译文件.通过Mocha运行文件时,Babel无需任何配置即可运行.

Since your config is inside your webpack.config.js file, Babel will only compile files when run through Webpack. When you run your files through Mocha, Babel will run without any configuration.

您应将配置移到项目中的.babelrc文件中,该文件包含:

You should move your config into a .babelrc file in your project containing:

{
    presets: ['stage-2','es2015','react'],
    plugins: ['transform-class-properties', 'transform-object-rest-spread']
}

并从您的webpack配置中删除该配置.

and remove the config from your webpack config.

这篇关于Mocha为ES6对象散布运算符引发了意外的令牌错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆