【问题标题】:Running mocha tests result in "Unexpected reserved word" error运行 mocha 测试导致“意外的保留字”错误
【发布时间】:2016-06-27 09:14:54
【问题描述】:

步骤:npm test

错误:“意外的保留字”

我今天早些时候进行了这项工作,但我不知道我所做的任何更改可能会导致回归。当我运行npm test 时,我得到“意外的保留字”,它指向一个 ES6 导入语句。我的理解是--compilers js:babel-register 应该可以解决这个问题。

我尝试在全局和本地安装babel-core,并改用babel-core/register。结果一样。

这是我的package.json

{
  "name": "fresh",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "remotedev": "remotedev --hostname=localhost --port=8000",
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "mocha --compilers js:babel-register 'test/**/*.test.js'",
    "test:watch": "npm run test -- --watch --watch-extensions jsx"
  },
  "dependencies": {
    "immutable": "^3.7.6",
    "lodash": "^4.5.1",
    "ramda": "^0.19.1",
    "react-native": "^0.21.0",
    "react-native-router-flux": "^2.3.8",
    "react-native-sortable-listview": "0.0.5",
    "react-redux": "^4.4.0",
    "redux": "^3.3.1",
    "redux-saga": "^0.9.1",
    "reselect": "^2.0.3"
  },
  "devDependencies": {
    "babel-register": "^6.7.2",
    "chai": "^3.5.0",
    "chai-immutable": "^1.5.3",
    "mocha": "^2.4.5",
    "redux-devtools-filter-actions": "^1.1.0",
    "remote-redux-devtools": "^0.1.2",
    "remotedev-server": "0.0.3"
  }
}

这里有 2 个教程表明这应该可以工作。

https://onsen.io/blog/mocha-chaijs-unit-test-coverage-es6/

http://jamesknelson.com/testing-in-es6-with-mocha-and-babel-6/

同样,我今天早些时候成功地使用此脚本执行了测试。

【问题讨论】:

  • 我遇到了同样的问题。 @ken4z,你解决过这个问题吗?

标签: node.js ecmascript-6 mocha.js babeljs


【解决方案1】:
npm install babel-preset-es2015

然后创建.babelrc 文件

{
  "presets": ["es2015"]
}

【讨论】:

  • 感谢您的回复。我已经尝试过,以及其他几个 babel 预设都无济于事。
猜你喜欢
  • 2016-05-04
  • 1970-01-01
  • 2022-10-24
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
  • 2016-04-06
相关资源
最近更新 更多