Using Istanbul and Mocha to cover ES6 code

I have a Node code written in ES6 that I am testing by releasing mocha --harmony . Tests are great - everything works.

Now I want to add coverage and istanbul to the mix, but I continue to get errors when the arrow function is first detected:

 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) 

Here is what I tried:

  • Installed istanbul harmony (from git: //github.com/gotwarlost/istanbul.git#harmony) as a function of my dev.
  • Running the following command: "./node_modules/.bin/istanbul" cover "./node_modules/mocha/bin/_mocha" -- --harmony tests -R spec
  • Combinations on flags for both istanbul and _mocha

How can I run istanbul to cover tests written using ES6 features? What am I missing?

+6
source share
2 answers

Just got this olved by a useful guy in the LinkedIn Node.JS group. The command line should be:

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

While this is rather cumbersome, you can simply drop it in the script section of package.json and run npm run cover from the command line.

+2
source

July 2016

Here are the relevant parts of the package.json file with the "npm cover" working command, which is useful with the es6 module code (that is, including es6 import and export ), babel 6, istanbul-1.0-alpha.2

I am posting this because I had to spend several hours to happen on solving from someone else a github stream of problems (which now I cannot find). It seems that there are many β€œsolutions” that no longer solve the coverage problem or cannot easily be adapted to other devDependencies stacks. YMMV.

package.json scripts

  "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" 
+4
source

Source: https://habr.com/ru/post/988209/


All Articles