Hone logo
Hone
Problems

Jest Code Coverage Threshold Configuration

Jest's code coverage reporting is a powerful tool for understanding the quality and completeness of your tests. Often, you'll want to enforce a minimum level of code coverage to ensure your codebase is well-tested. This challenge focuses on configuring Jest to fail builds if coverage falls below predefined thresholds.

Problem Description

You are tasked with setting up Jest's code coverage thresholds in your TypeScript project. The goal is to configure Jest so that it reports an error and fails the test runner if the achieved code coverage for specific metrics (like lines, functions, branches, or statements) falls below a defined minimum percentage. This ensures that as the project evolves, a baseline level of test coverage is maintained.

Key Requirements:

  • Configure Jest to use a coverageThreshold object in its configuration.
  • Set minimum acceptable thresholds for at least two coverage metrics (e.g., lines and functions).
  • The configuration should apply to all files matching a certain pattern (e.g., all .ts files).
  • When coverage is below the threshold, Jest should exit with a non-zero status code, indicating a failure.

Expected Behavior: When running Jest with coverage enabled (jest --coverage), if any of the configured thresholds are not met for any file or the global coverage, Jest should output a coverage report highlighting the shortcomings and terminate with an error.

Edge Cases:

  • What happens if no files match the specified pattern?
  • How does Jest handle coverage for files with no executable code (e.g., empty files or only comments)?

Examples

Example 1:

// jest.config.js (or equivalent config file)
module.exports = {
  // ... other Jest configurations
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 70,
      lines: 85,
      statements: 85,
    },
    './src/**/*.ts': { // Applying to all TS files in src directory
      branches: 75,
      functions: 65,
      lines: 80,
      statements: 80,
    },
  },
  // ...
};

Input to Jest: A TypeScript project with some files in the ./src directory. A test run is executed with jest --coverage.

Output of Jest: If the global line coverage is 84%, and the line coverage for ./src/utils.ts is 79%, Jest will output a coverage report indicating these failures and exit with a non-zero code.

--------------------|---------|----------|---------|---------|-------------------
File                | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------|---------|----------|---------|---------|-------------------
All files           | 82.5%   | 78%      | 68%     | 83%     |
 src/utils.ts       | 80%     | 70%      | 60%     | 79%     | 10, 12, 25
--------------------|---------|----------|---------|---------|-------------------

(Note: The exact output will vary based on Jest version and actual coverage data. The key is the presence of a failure indication.)

Explanation: The coverageThreshold configuration sets minimum percentages. The global lines threshold is 85%, but the actual is 83%, causing a failure. The ./src/**/*.ts specific threshold for lines is 80%, but ./src/utils.ts has 79%, also failing.

Example 2 (Edge Case - No Matching Files): If the configuration specifies './nonexistent-directory/**/*.ts' and no such files exist, Jest will likely not report coverage failures for that specific glob, but it will still evaluate global thresholds.

Input to Jest: A project with no files matching ./nonexistent-directory/**/*.ts. jest --coverage is run.

Output of Jest: Jest will report coverage based on the files it finds and evaluate them against the global thresholds. If global thresholds are met, it will pass. If the specific glob had a threshold and no files matched, it wouldn't trigger a failure based on that specific glob.

Explanation: Jest gracefully handles globs that don't match any files without causing errors. The focus remains on the global thresholds or thresholds for matched files.

Constraints

  • Your Jest configuration file (e.g., jest.config.js, jest.config.ts, or package.json) must be updated to include the coverageThreshold property.
  • The thresholds should be represented as integers between 0 and 100.
  • The configuration should be able to handle multiple file patterns if necessary.
  • The solution should work with a standard Jest setup for TypeScript projects.

Notes

  • You'll need to have Jest installed and configured for your TypeScript project.
  • Consider using ts-jest if you are using TypeScript directly in your Jest configuration.
  • The coverageThreshold can be applied globally or to specific file patterns using glob syntax.
  • When running Jest, ensure you include the --coverage flag to generate coverage reports.
  • Pay attention to the different coverage metrics Jest can report on: branches, functions, lines, and statements. Choose the ones most relevant to your project's needs.
Loading editor...
typescript