我有用ES6编写的节点代码,我通过发布mocha --harmony
进行测试。
测试很好 - 一切正常。
现在我想添加coverage和istanbul,但是我遇到了第一个箭头函数的错误:
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_modules/.bin/istanbul" cover "./node_modules/mocha/bin/_mocha" -- --harmony tests -R spec
如何运行istanbul来涵盖使用ES6功能编写的测试?我错过了什么?
答案 0 :(得分:4)
以下是package.json文件的相关部分,其中包含工作" npm封面"对es6模块代码有用的命令(即包括es6 import
和export
),babel 6,istanbul-1.0-alpha.2
我发布这个是因为我不得不花费几个小时来解决其他人的github问题线程(现在我找不到)的解决方案。似乎有很多"解决方案"不再解决覆盖问题或不能轻易地适应其他堆devDependencies。 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 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"
答案 1 :(得分:2)
刚刚在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
。