Istanbul code coverage for Mocha tests Istanbul code coverage for Mocha tests node.js node.js

Istanbul code coverage for Mocha tests


Got it, finally.

https://github.com/gotwarlost/istanbul/issues/44#issuecomment-57708358 says:

Using _mocha directly does not work on windows. Please use the full path to the JS file instead

Then, after a little experimentation, victory:

istanbul cover C:/dev/my_project/node_modules/mocha/bin/_mocha --


Try this, may works for you. Script tag in your package.json

"cover" : "NODE_ENV=test babel-node ./node_modules/babel-istanbul/lib/cli cover --dir ./coverage _mocha -- -R spec --ui bdd ./test/setup.js ./test --recursive"

setup.js will be your setup javascript

./test will be the folder where all your tests sits.

packages need to install:

  • babel-istanbul
  • babel-node
  • mocha

This works for me :) (npm run cover)


Note: if on windows: remove NODE_ENV=test, and may need to make the _mocha full path to your node module folder


If you're setting up a new project and trying to get istanbul to work, ensure that you have a least one test file that references at least one of the project files for istanbul to reference for its coverage report.

"istanbul": "0.4.4",   "mocha": "3.0.0"

In my case, I was setting up a new project and getting the first index.js and test/index.spec.js files prepped. When I tried to run:

"test": "istanbul cover --report html node_modules/mocha/bin/_mocha -- test/**/*.js --ui bdd -R spec"

I got an output from mocha of all unit tests passing (which there were no tests written yet so all 0 of them passed) but I then received this error after that:

No coverage information was collected, exit without writing coverage information

The issue turned out to be that I had not yet added a require for the index.js file in the test/index.spec.js file yet. Istanbul had no files to reference from the test files to check for coverage information.

If it's a pathing issue to _mocha referenced in the npm script, there will be an additional "SyntaxError:" message that follows the "No coverage information was collected" message.