Generate Test Coverage Report

Explore how to generate and use coverage reports using Jest.

Test coverage is a metric that indicates the percentage of our codebase covered by tests. In this lesson, we’ll explore the concept of test coverage and learn how to generate the coverage report using Jest.

Overview

Test coverage is determined by checking how much of the code in a project is tested by the unit tests. The process involves dividing the number of lines or branches covered by tests by the codebase’s total number of lines or branches. For example, if a unit test suite covers 80 out of 100 lines of code, the test coverage would be 80%.

Test coverage assesses how thoroughly the automated tests explore different paths within the code. This measurement helps pinpoint areas that might lack sufficient testing. It serves as a metric to gauge the effectiveness of testing efforts, reducing the risk of undetected defects and improving the overall reliability of the project.

How much test coverage should we aim for?

High test coverage is a good indicator, meaning unit tests cover a significant percentage of the codebase. However, achieving 100% test coverage might be impractical or unnecessary in many cases. The ideal unit test coverage goal can vary depending on various factors, such as the nature of the project, industry standards, and team preferences. However, a goal of around 80% coverage is a good balance between achieving comprehensive coverage and recognizing practical constraints. A project with critical functionalities or in industries with strict quality standards might aim for 90% or even higher coverage.

It’s important to note that high unit test coverage alone does not guarantee a bug-free application. It’s also possible to have high coverage and still miss certain edge cases. Focusing on writing meaningful tests that can catch ...