How to make Istanbul generate coverage for all of my source code? How to make Istanbul generate coverage for all of my source code? node.js node.js

How to make Istanbul generate coverage for all of my source code?


Found the answer, I think I'm partly lucky that the directory structure I have chosen allows me to use this option, but my test command is now:

$ istanbul --include-all-sources cover _mocha -- -R dot --recursive test/

The --include-all-sources is the important part.


Istanbul recommends using nyc in order to check code coverage. It suggests an approach like this:

nyc mocha

After running this command, we'll get the coverage report. But there is a couple of pitfalls.

First of all, by default mocha is looking for tests in folder test. In order to override it, we have to set our own path in file mocha.opts like this:

nyc mocha --opts ./mocha.opts

And mocha.opts contains such code, for example:

spec/unit/back-end/**/*.spec.js

Another problem is that by default nyc checks coverage of only required files, that is your question is about. The solution is to set two options for nyc (I run test as an npm script so I set options in package.json). Here is the code:

"nyc": {  "all": true,  "include": [    "routes/*.js",    "routes/**/*.js",    "models/*.js"  ]},"scripts": {  "mocha": "nyc mocha --opts ./mocha.opts",}

Another way to achieve it is to set not include, but exclude option in order to exclude from coverage checking inappropriate files. It's strange, but the sole option all doesn't work, it requires include or exclude options. You can get more info about nyc options via nyc --help.

P.S. I don't know nyc and mocha deeply and I'm only based on my own experience.


For generating coverage for all files, have the following in your package.json

"istanbulCoverage": "nyc --reporter=lcov --reporter=text-lcov --all -x \"./node_modules/\" -x \"./coverage/\" check-coverage --functions 90 npm run test"

Here --all flag fetches all the files in your project. If u want to exclude specific files or folders, you can use -x option.

Apart from this, if you want to specify coverage rate for you application, then use check-coverage option to specify the threshold. In my case, I've specified functions to have a coverage threshold of 90%. Otherwise, the coverage report generation fails.(i am running my karma test after coverage report generation).

Hope this helped:)