Traveling Tech Guy Traveling Tech Guy - 1 year ago 176
Node.js Question

Using Istanbul and Mocha to cover ES6 code

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
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:

  1. Installed istanbul-harmony (from git:// as my dev dependency.

  2. Running the following command:
    "./node_modules/.bin/istanbul" cover "./node_modules/mocha/bin/_mocha" -- --harmony tests -R spec

  3. Combinations on flags for both istanbul and _mocha

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download