I have Node code, written in ES6, that I test by issuing mocha --harmony
.
Tests are fine - everything works.
Now I want to add coverage and istanbul to the mix, but I keep getting errors on the first arrow function encountered:
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's what I tried:
"./node_modules/.bin/istanbul" cover "./node_modules/mocha/bin/_mocha" -- --harmony tests -R spec
How can I run istanbul to cover tests written using ES6 features? What am I missing?
Here are the relevant portions of a package.json file with a working "npm cover" command that is proving useful with es6 module code (i.e. including es6 import
and export
), babel 6, istanbul-1.0-alpha.2
I am posting this because I had to spend a few hours to happen on a solution from someone else's github issue thread (which now I can not find). There seem to be a lot of "solutions" that no longer solve the coverage issue or can not be easily adapted to other stacks of devDependencies. 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"
Just got this olved by a helpful guy on 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 quite cumbersome, you can just drop it in your package.json
scripts section, and run npm run cover
from command line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With