381

I am using Mocha for testing my NodeJS application. I am not able to figure out how to use its code coverage feature. I tried googling it but did not find any proper tutorial. Please help.

4 Answers 4

516

You need an additional library for code coverage, and you are going to be blown away by how powerful and easy istanbul is. Try the following, after you get your mocha tests to pass:

npm install nyc

Now, simply place the command nyc in front of your existing test command, for example:

{
  "scripts": {
    "test": "nyc mocha"
  }
}
Sign up to request clarification or add additional context in comments.

14 Comments

And if you're running a locally installed version of mocha, try istanbul cover node_modules/mocha/bin/_mocha.
Or, install both istanbul and mocha locally, and add the following to the scripts section of your package.json and then just npm run coverage: "coverage": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --ui bdd -R spec -t 5000
I had trouble getting this command to run on windows, but by specifying the full path to the mocha bin I was able to get it to work. istanbul.cmd cover C:\Users\{UserName}\AppData\Roaming\npm\node_modules\mocha\bin\_mocha
$(npm bin) is a canonical shortcut to ./node_modules/.bin/, and istanbul/lib/cli.js is aliased to istanbul in the bin folder. So here's a shorter command: $(npm bin)/istanbul cover $(npm bin)/_mocha -- --ui bdd -R spec -t 5000
@ Windows users: istanbul cover node_modules/mocha/bin/_mocha -- -R spec
|
253

Now (2024) the preferred way to use istanbul is via its "state of the art command line interface" nyc.

Setup

First, install it in your project with

npm i nyc --save-dev

Then, if you have a npm based project, just change the test script inside the scripts object of your package.json file to execute code coverage of your mocha tests:

{
  "scripts": {
    "test": "nyc --reporter=text mocha"
  }
}

Run

Now run your tests

npm test

and you will see a table like this in your console, just after your tests output:

Istanbul Nyc Mocha code coverage

Customization

Html report

Just use

nyc --reporter=html

instead of text. Now it will produce a report inside ./coverage/index.html.

Report formats

Istanbul supports a wide range of report formats. Just look at its reports library to find the most useful for you. Just add a --reporter=REPORTER_NAME option for each format you want. For example, with

nyc --reporter=html --reporter=text

you will have both the console and the html report.

Don't run coverage with npm test

Just add another script in your package.json and leave the test script with only your test runner (e.g. mocha):

{
  "scripts": {
    "test": "mocha",
    "test-with-coverage": "nyc --reporter=text mocha"
  }
}

Now run this custom script

npm run test-with-coverage

to run tests with code coverage.

Force test failing if code coverage is low

Fail if the total code coverage is below 90%:

nyc --check-coverage --lines 90 

Fail if the code coverage of at least one file is below 90%:

nyc --check-coverage --lines 90 --per-file

10 Comments

This works perfectly for jasmine also : "nyc --reporter=html jasmine"
mine works but I do not see the code coverage showing which lines are covered by green / red in the code itself.
thanks for adding "now(2017)" - really helpful in this fast moving javascript world
In case anyone else was confused - the npm repository istanbul seems to have been superceded by nyc. Per its listed dependencies, istanbul was split apart into various packages which are all maintained in their istanbuljs monorepo
I am having the --reporter=html enabled but the html file is empty always, there is nothing shown about uncovered blocks or % covered etc just headers of the table
|
26

The accepted answer (nyc) does not work if you are using ESM modules.

C8 appears to be the best solution now, which leverages built-in NodeJS capabilities and utilizes istanbul (like nyc, and shares the same config files).

npm install -g c8
c8 mocha

It will use .nycrc for configuration. A sample configuration I'm using is:

{
    "all": true,
    "exclude": ["test"],
    "output": "reports",
    "reporter" : [
        "html",
        "text"
    ]
}

(Note: I was pointed to c8 by an answer to another question https://stackoverflow.com/a/69846825/1949430)

1 Comment

It worked really well for me, even with lots of added mocha parameters, e.g. from my package.json: { "scripts": { "test": "mocha --reporter=spec --recursive test/spec", "coverage": "c8 mocha --reporter=html --recursive test/spec", ... ... ...
20

Blanket.js works perfect too.

npm install --save-dev blanket

in front of your test/tests.js

require('blanket')({
    pattern: function (filename) {
        return !/node_modules/.test(filename);
    }
});

run mocha -R html-cov > coverage.html

2 Comments

require('blanket')({ pattern: function (filename) { return !/node_modules/.test(filename); } });
As of 2015, blanket.js is not maintained anymore and doesn't support ES6. Istanbul is highly recommended.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.