node.js – 使用Istanbul和Mocha来覆盖ES6代码

前端之家收集整理的这篇文章主要介绍了node.js – 使用Istanbul和Mocha来覆盖ES6代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有用ES6编写的Node代码,我通过发出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)

这是我试过的:

>安装伊斯坦布尔和谐(来自git://github.com/gotwarlost/istanbul.git#harmony)作为我的开发依赖.
>运行以下命令:“./ node_modules / .bin /istanbul”cover“./node_modules/mocha/bin/_mocha” – –harmony tests -R spec
>伊斯坦布尔和_mocha标志的组合

我如何运行istanbul来涵盖使用ES6功能编写的测试?我错过了什么?

解决方法

2016年7月

以下是package.json文件的相关部分,其中包含有效的“npm cover”命令,该命令对es6模块代码(即包括es6导入和导出),babel 6,istanbul-1.0-alpha.2有用.

我发布这个是因为我不得不花费几个小时来解决其他人的github问题线程(现在我找不到)的解决方案.似乎有很多“解决方案”不再解决覆盖问题,或者无法轻易地适应其他堆栈的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"
原文链接:https://www.f2er.com/nodejs/241213.html

猜你在找的Node.js相关文章