7. Coverage in Jest
In computer science, test coverage is a measure used to describe the degree to which the source code of a program is executed when a particular test suite runs.
Code coverage is usually used as a quality metric for software eg. “Our code has to have 80%+ test coverage”. Gathering test coverage with Jest is as simple as using the --coverage
flag on invocation.
In this section you will learn to:
- collect coverage with Jest
- set Jest coverage thresholds and read coverage reports
- a few strategies to exclude statements, functions and other JavaScript constructs from Jest coverage.
How does Jest even calculate coverage?
Jest uses istanbul under the hood to calculate coverage. Mostly Jest abstracts this from the end user, all you have to do in your application is call jest --coverage
(and configured the appropriate coverage configuration fields). The fact that istanbul is used internally does show, for example, the documentation for coverageReporters mentions that “Any istanbul reporter can be used.”, which shows what’s actually collecting coverage data and generating the reports.