How to Generate Jest Coverage in CI
Run Jest with --coverage and configure lcov output so CI has both a printed summary and a machine-readable file.
Add --coverage to your Jest invocation and set coverageReporters to include lcov and text. Jest writes coverage/lcov.info and prints a table you can read in the log.
Steps
- Run
jest --coverage(ornpm test -- --coverage) in the CI step. - Set
coverageReportersto["text", "lcov", "cobertura"]for both logs and uploads. - Point later upload steps at
coverage/lcov.info.
jest.config.js
jest.config.js
module.exports = {
collectCoverage: true,
coverageDirectory: 'coverage',
coverageReporters: ['text', 'lcov', 'cobertura'],
collectCoverageFrom: ['src/**/*.{js,ts}', '!src/**/*.d.ts'],
};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
- run: npm ci
- run: npx jest --coverageGotchas
collectCoverageFrommatters: without it, files never imported by a test are counted as fully uncovered or not at all.- The
textreporter prints to the log;lcovandcoberturaare what upload tools consume.
Related guides
How to Fail CI Under a Jest Coverage ThresholdEnforce a minimum coverage level in CI with Jest coverageThreshold, so the test command exits non-zero when l…
How to Upload Coverage to Codecov in CISend a coverage report to Codecov from CI with codecov/codecov-action, providing the token so private repos a…
How to Exclude Files From Coverage in CIKeep generated code, tests, and vendored files out of coverage numbers with per-tool exclude patterns so the…