Skip to content

Check Test Coverage with NYC

James Richford edited this page Jan 25, 2017 · 5 revisions

Setting up test coverage can give you great insights into how well tested your application is but also allows you to publish figures so that your users can see you are committed to building a quality application.

Setup

First off you'll need to do a bit of setup

Assuming you have a unit-tests npm script with something like alsatian "./**/*.spec.js" then all you should need to do is add the following to check the coverage of your unit tests nyc --reporter=lcov --reporter=html npm run unit-tests && nyc report

example package.json

{
   "scripts": {
      "unit-tests": "alsatian \"./**/*.spec.js\"",
      "check-coverage": "nyc --reporter=lcov --reporter=html npm run unit-tests && nyc report"
   }
}

Breaking it down

nyc start up nyc --reporter=lcov lets NYC know to build an LCOV report which is used by various third party services such as coveralls --reporter=html builds you an awesome html report which you can peruse yourself to get insights about the your coverage (missed lines, branches etc.) npm run unit-tests the script that runs your unit tests nyc report output the reports that have been generated

Further reading

Check out NYC's docs for more info!