nicolodavis.com

100% Test Coverage in Jest

December 28, 2018

Jest can generate test coverage reports using Istanbul by passing the --coverage flag. If all goes well, you’ll see a neat report like this on the terminal:

Terminal Coverage Report

The report contains useful pointers to things like uncovered lines (highlighted in red), or uncovered branches (highlighted in yellow).

Even after fixing problems, you might hit the odd pesky function that isn’t covered resulting in a less than perfect test coverage score.

Terminal Coverage Report

This might happen if you use a default function as a fallback (see below) and you don’t test the case where callback is undefined.

function Main(callback) {
  this.callback = callback || () => {};
  ...
}

The way to identify these coverage gaps is to use the HTML reporter. The HTML report is generated in ./coverage/lcov-report. You can spin up a HTTP server to see the report like this:

$ npm install --global http-server
$ http-server coverage/lcov-report

It will overlay coverage statistics on the actual source code so that you can identify problems more easily.

HTML Coverage Report


Nicolo John Davis
Nicolo Davis