Skip to content
Latchkey

How to Enforce a Code Coverage Threshold with GitHub Actions

A coverage threshold makes the test runner exit non-zero when coverage falls below your floor, failing the job.

Configure coverageThreshold in jest.config.js (or equivalent for your runner) and run with --coverage. When coverage dips below the global floor, the process exits non-zero and the job fails.

Steps

  • Set coverageThreshold.global in your Jest config (lines, statements, branches, functions).
  • Run jest --coverage in CI so the threshold is evaluated.
  • The runner exits non-zero on a shortfall, failing the job automatically.

Jest config

jest.config.js
// jest.config.js
module.exports = {
  collectCoverage: true,
  coverageThreshold: {
    global: { branches: 80, functions: 80, lines: 85, statements: 85 },
  },
};

Workflow

.github/workflows/ci.yml
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
      - run: npm ci
      - run: npx jest --coverage

Gotchas

  • Set realistic floors; thresholds that are too high get bypassed and lose meaning.
  • Per-directory thresholds catch untested new modules that a global average would hide.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →