使用伊斯坦布尔和摩卡来覆盖ES6的代码

我有用ES6编写的Node代码,通过发布mocha --harmonytesting。 testing很好 – 一切正常。

现在我想添加覆盖和伊斯坦布尔的混合,但我不断遇到遇到的第一个箭头函数的错误:

 No coverage information was collected, exit without writing coverage information c:\Users\Guy\Code\alpha-dev\tests\helpers.js:12 setTimeout(() => { ^ SyntaxError: Unexpected token ) at exports.runInThisContext (vm.js:73:16) at Module._compile (module.js:443:25) at Module._extensions..js (module.js:478:10) at Object.Module._extensions..js (c:\Users\Guy\Code\alpha-dev\node_modules\istanbul\lib\hook.js:101:13) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) 

这是我的尝试:

  1. 安装伊斯坦布尔和谐(从git://github.com/gotwarlost/istanbul.git#harmony)作为我的开发依赖。
  2. 运行以下命令: "./node_modules/.bin/istanbul" cover "./node_modules/mocha/bin/_mocha" -- --harmony tests -R spec
  3. 伊斯坦布尔和_mocha旗帜的组合

我如何运行istanbul来覆盖使用ES6function编写的testing? 我错过了什么?

2016年7月

下面是一个package.json文件的相关部分,带有一个可用于es6模块代码(即包括es6 importexport ),babel 6,istanbul-1.0-alpha.2的有用的“npm封面”命令

我张贴这个,因为我不得不花费几个小时发生在别人的github问题线程(现在我找不到)的解决scheme。 似乎有很多“解决scheme”不再解决覆盖问题或不能轻易适应其他堆栈的devDependencies。 因人而异。

package.json脚本

  "scripts": { "clean": "rm -rf ./build ./doc ; mkdir ./build", "build": "node_modules/.bin/babel build src/index.js -o build/index.js", "doc": "node_modules/.bin/esdoc -c esdoc.json", "lint": "node_modules/.bin/eslint src/index.js", "lint-test": "node_modules/.bin/eslint test/index.js", "test": "node_modules/.bin/mocha --compilers js:babel-core/register --reporter spec --slow 50 --timeout 60000", "cover": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -u exports --compilers js:babel-register --timeout 60000", "go": "npm run clean && npm run lint && npm run lint-test && npm run test && npm run build" }, 

package.json devDependencies

  "devDependencies": { "babel": "^6.5.2", "babel-cli": "^6.10.1", "babel-core": "^6.10.4", "babel-preset-es2015": "^6.9.0", "coveralls": "^2.11.9", "esdoc": "^0.4.7", "eslint": "^3.0.1", "istanbul": "^1.0.0-alpha.2", "mocha": "^2.5.3", "should": "^8.3.1" }, 

.babelrc

 { "presets": ["es2015"] } 

.travis.yml

 language: node_js node_js: - 6 install: - npm install script: - npm run lint - npm run lint-test - npm run cover after_script: - "cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js" 

刚刚得到LinkedIn Node.JS组的帮助, 命令行应该是:

 node --harmony ./node_modules/istanbul-harmony/lib/cli.js cover --hook-run-in-context ./node_modules/mocha/bin/_mocha -- --R spec --U exports tests 

虽然这很麻烦,但您可以将其放在package.json脚本部分,然后从命令行运行npm run cover

模块版本:

 "istanbul": "0.4.2", "mocha": "2.4.5" 

我的解决scheme

 node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha