Node.js 使用伊斯坦布尔和摩卡覆盖ES6代码
我有用ES6编写的节点代码,我通过发布Node.js 使用伊斯坦布尔和摩卡覆盖ES6代码,node.js,code-coverage,mocha.js,ecmascript-6,istanbul,Node.js,Code Coverage,Mocha.js,Ecmascript 6,Istanbul,我有用ES6编写的节点代码,我通过发布mocha--harmony来测试它。 测试很好,一切正常 现在,我想将coverage和istanbul添加到混合中,但在遇到的第一个arrow函数中不断出现错误: No coverage information was collected, exit without writing coverage information c:\Users\Guy\Code\alpha-dev\tests\helpers.js:12 setTimeout(()
mocha--harmony
来测试它。
测试很好,一切正常
现在,我想将coverage和istanbul添加到混合中,但在遇到的第一个arrow函数中不断出现错误:
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)
以下是我尝试过的:
”/node\u modules/.bin/伊斯坦布尔“cover”。/node\u modules/mocha/bin/\u mocha”---harmony tests-R spec
如何运行伊斯坦布尔以覆盖使用ES6功能编写的测试?我错过了什么?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
scripts部分,然后从命令行运行npm run cover
。2016年7月
下面是package.json文件的相关部分,该文件带有一个工作的“npm cover”命令,该命令被证明对es6模块代码(即包括es6import
和export
)有用,babel 6,伊斯坦布尔-1.0-alpha.2
我之所以发布这篇文章,是因为我不得不花几个小时从其他人的github问题线程(现在我找不到)中找到一个解决方案。似乎有很多“解决方案”不再解决覆盖率问题,或者无法轻松适应其他DevDependence堆栈。YMMV
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 devdependency
"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"
},
B.法律改革委员会
{
"presets": ["es2015"]
}
特拉维斯·伊梅尔先生
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"
我相信大多数人都会用,虽然我自己还没有用过。你用的是伊斯坦布尔的哪个版本?自v0.3.9起,它支持箭头功能3.12。您使用的是什么命令行?现在不推荐使用伊斯坦布尔harmony。改用伊斯坦布尔@>=0.4.0