Use Codecov Patch Coverage to Catch Untested Changes in Pull Requests
A high aggregate coverage badge can hide untested lines in your pull request. Codecov patch thresholds gate merges on changed code, not the project average.
07 Dec 2025, 21:31 UTC

The Problem: Aggregate Coverage Hides Untested Changes
Your Codecov badge can report 92% coverage while a pull request leaves a critical payment path completely untested. That happens because the badge usually reflects project-wide coverage: an average across every tracked file, including code that changed months ago. A small improvement in an old, well-tested module can mask a new, untested branch.
The practical fix is to stop gating on the aggregate percentage and enforce patch coverage instead. Codecov can evaluate only the lines added or modified in a pull request and fail the CI status check when those lines fall below a target. This keeps the badge honest and prevents untested changes from merging.
What Patch Coverage Means
Coverage report: a file, often JSON or XML, produced by your test runner that records which lines executed during tests.
Patch: the set of lines added or changed in a pull request compared with the base branch.
Project coverage: coverage across all tracked files, usually what a badge displays.
Patch coverage: coverage limited to the changed lines in a pull request.
Threshold: the maximum allowed drop or minimum required percentage before Codecov marks a status check as failed.
Worked Example: Gate Pull Requests on Changed Lines
Add a codecov.yml file to your repository root. This example keeps the project target flexible but requires changed lines to stay at or above 90% coverage.
coverage:
status:
project:
default:
target: auto
threshold: 0.5%
if_ci_failed: error
patch:
default:
target: 90%
if_ci_failed: error
Key fields:
target: autotells Codecov to compare the project against the previous commit rather than a fixed number.threshold: 0.5%fails the project check if aggregate coverage falls by more than half a percent.patch.default.target: 90%requires new or modified lines to reach 90% coverage.if_ci_failed: errorturns a missing upload into a failed status check instead of a silent pass.
Next, add a GitHub Actions workflow that uploads the report. Commit this to .github/workflows/coverage.yml.
name: Coverage
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test -- --coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
Where to run: the workflow executes on GitHub Actions for push and pull_request events after you push the file.
Permissions: you need write access to the repository to add the workflow and the CODECOV_TOKEN secret. Copy the token from your repository settings in Codecov and add it as a GitHub Actions secret.
Placeholder: ${{ secrets.CODECOV_TOKEN }} references that secret. Replace the coverage path if your test runner writes to a different directory.
Expected checks: after a push, the Codecov action uploads the report and posts a pull request comment showing project and patch coverage. The status check should pass only when thresholds are met.
Risks: an incorrect files path produces no data and can fail silently unless fail_ci_if_error is set. A token without repository access prevents uploads. A strict patch target can block legitimate PRs that touch hard-to-test code.
Limitations and How to Verify
Coverage percentage does not measure whether tests assert the right behavior. A badge can also become inaccurate if the CI pipeline fails before uploading, leaving the previous number on display. Private repositories require careful token rotation and access control.
Practical check: open a pull request that modifies a source file without adding tests. The Codecov comment should show patch coverage below your target and a failing status check. Then compare the Codecov dashboard against your local coverage report from the same test run. Finally, view the README badge after a successful main branch upload to confirm it reflects the latest project percentage.
Rollback
Because committing codecov.yml and the workflow changes repository state, the rollback is to revert those commits or delete the files. Do this if thresholds block legitimate work while you tune the target percentage.
Closing
Start with a loose patch target, such as 70%, observe the Codecov comments on a few pull requests, then tighten the threshold as your team adapts. The badge is a summary; the patch check is the gate.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.