Skip to content

Jest Coverage Threshold Not Enforcing Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Jest Coverage Threshold Not Enforcing Fix. We cover key concepts, practical examples, and best practices.

Your Jest coverage thresholds don't fail the build — even when coverage drops below the configured minimum. Tests pass but the coverage report shows values under the threshold.

The Problem

// WRONG — thresholds in wrong config section
// jest.config.js
module.exports = {
  coverageThreshold: 80, // Wrong — should be an object
};
No effect — Jest ignores a number value for coverageThreshold

The threshold must be an object with specific metric keys. A plain number is silently ignored.

Step-by-Step Fix

1. Set global coverage thresholds

// RIGHT — proper threshold configuration
// jest.config.js
module.exports = {
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: -10, // Allow up to 10 uncovered statements
    },
  },
};

2. Set per-file thresholds

// RIGHT — different thresholds per file
module.exports = {
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 85,
      lines: 85,
      statements: 80,
    },
    './src/utils/**/*.js': {
      branches: 90,
      functions: 95,
      lines: 95,
      statements: 90,
    },
    './src/legacy/**/*.js': {
      branches: 50,
      functions: 60,
      lines: 60,
    },
  },
};

3. Combine with CI enforcement

// package.json — CI script
{
  "scripts": {
    "test:coverage": "jest --coverage --coverageThreshold='{}'",
    "test:ci": "jest --ci --coverage"
  }
}

4. Handle edge cases with negative thresholds

// RIGHT — allow a specific number of uncovered items
module.exports = {
  coverageThreshold: {
    './src/legacy/**/*.js': {
      statements: -5, // Allow 5 uncovered statements
    },
  },
};

Expected output:

FAIL  jest
  ● Your test suite has coverage thresholds not met:
    global: branches: 75% < 80%
    ./src/utils/helper.js: lines: 70% < 95%

Prevention Tips

  • Always use object format for coverageThreshold
  • Set separate thresholds for critical and legacy code
  • Use negative values to allow specific counts of uncovered items
  • Run with --ci flag for stricter enforcement
  • Review coverage reports in PR pipelines

Common Mistakes with coverage threshold

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

These mistakes appear frequently in real-world JEST code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does my build pass even when coverage is below the threshold?

Check that coverageThreshold is in the correct format (nested object) and that --coverage flag is passed. Also verify Jest version — some older versions had different threshold behavior.

Can I set thresholds for individual files?

Yes. Use glob patterns as keys in the coverageThreshold object. File patterns override global thresholds. This is useful for maintaining high standards on critical code while being lenient on legacy modules.

What's the difference between positive and negative threshold values?

Positive values (e.g., 80) are percentages — coverage must be at least 80%. Negative values (e.g., -10) are absolute counts — allow up to 10 uncovered items regardless of total size.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro