Last Updated: January 20, 2017
·
46.79K
· paulbjensen

Running Istanbul code coverage with mocha

Istanbul is a code coverage tool for JavaScript.

To run Istanbul with Mocha (a popular test framework), you can run this command:

npm install -g istanbul
istanbul cover node_modules/.bin/_mocha -- -- -u exports -R spec test/**/*

The reason we call _mocha</code> instead of mocha</code> is best explained in this github issue comment.

There are more options available when configuring Istanbul, see the command options here

4 Responses
Add your response

istanbul cover --report html nodemodules/.bin/mocha -- -R spec

It's easier to find all your tests if you end with *Spec.js

over 1 year ago ·

This makes clear how to specify test directories other than "spec". Thank you!!

over 1 year ago ·

if you have a grunt task setup to run mocha (or whatever) , its even easier

istanbul cover --report html grunt test

over 1 year ago ·

Have you tried this with a require.js project? I have a project that runs mocha via grunt, using an html page to configure everything for mocha, either in the browser or with phantomjs. Have you been able to do coverage with istanbul with anything like this?

Thanks!

over 1 year ago ·