Understanding Jest Coverage Output: Hide Files Not Included in Global Threshold
When working with Jest, a powerful JavaScript testing framework, one of the key features is its coverage report. This report provides insights into how well your code is tested, highlighting areas that may need additional attention. One aspect of this report that can be quite useful is the ability to hide files not included in the global threshold. Let’s delve into this feature and understand its significance.
What is Jest Coverage?
Jest Coverage is a metric that measures the percentage of your code that is covered by tests. It helps you identify untested parts of your codebase, making it easier to write comprehensive tests. The coverage report generated by Jest provides a detailed breakdown of which parts of your code are covered and which are not.
Global Thresholds in Jest Coverage
Global thresholds in Jest coverage refer to the minimum percentage of coverage required for a file or a directory to be considered “covered.” These thresholds can be set globally or on a per-project basis. If a file or directory’s coverage falls below the specified threshold, it is marked as “uncovered” in the coverage report.
Why Hide Files Not Included in Global Threshold?
While global thresholds are a great way to ensure that your codebase is well-covered, there are situations where you might want to hide files not included in the global threshold. Here are a few reasons why you might consider doing so:
-
Excluding third-party libraries: If you have third-party libraries in your project, you might want to exclude them from the coverage report. These libraries are often well-tested by their respective maintainers, so including them in your coverage report might not provide much value.
-
Excluding utility files: Utility files, such as helper functions or utility classes, might not require extensive testing. Hiding these files from the coverage report can help you focus on the more critical parts of your codebase.
-
Excluding configuration files: Configuration files, such as .env files or JSON configuration files, are not typically tested and can be excluded from the coverage report to avoid cluttering the report.
How to Hide Files Not Included in Global Threshold
By default, Jest does not hide files not included in the global threshold. However, you can achieve this by using the thresholds
option in your Jest configuration file. Here’s how you can do it:
module.exports = { thresholds: { global: { branches: 80, functions: 80, lines: 80, statements: -10 }, hideFilesNotInThreshold: true }};
In the above configuration, the hideFilesNotInThreshold
option is set to true
, which will hide files not included in the global threshold from the coverage report.
Example Configuration
Let’s consider an example where you want to hide files not included in the global threshold. Here’s a sample configuration:
module.exports = { thresholds: { global: { branches: 80, functions: 80, lines: 80, statements: -10 }, hideFilesNotInThreshold: true }};
In this configuration, the global threshold is set to 80% for branches, functions, and lines, with a -10% threshold for statements. Files that do not meet these thresholds will be hidden from the coverage report.
Table: Coverage Metrics
Metric | Threshold | Description |
---|---|---|
Branches | 80% | Percentage of branches covered by tests |
Functions | 80% | Percentage of functions covered by tests |
Lines | 80% | Percentage of lines covered by tests |
Statements | -10% | Percentage of statements covered by tests (negative value indicates no minimum requirement) |