Code coverage with Mocha

tusharmath picture tusharmath · May 19, 2013 · Viewed 138.2k times · Source

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.

Answer

Dan Kohn picture Dan Kohn · May 19, 2013

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"
  }
}