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.globalin your Jest config (lines, statements, branches, functions). - Run
jest --coveragein 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 --coverageGotchas
- 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
How to Publish Coverage to Codecov with GitHub ActionsGenerate a coverage report in GitHub Actions and upload it to Codecov with codecov/codecov-action, using a re…
How to Publish Test Results as a Check with GitHub ActionsTurn a JUnit XML test report into a GitHub Actions check run with dorny/test-reporter, so failures appear inl…