SupercovBlog

Guide

Merge coverage reports from shards: Vitest, Jest and coverage.py

Each way to combine shard coverage in Vitest, Jest and coverage.py, measured, including the ones that lose a shard without an error.

Two shards. One coverage report. Two brushed-aluminum trays of white tiles pushed together edge to edge into one, a violet tile at the seam.

Run each shard with its own output, then merge the raw data, not the summaries. In Vitest, write blob reports and run vitest --merge-reports --coverage. In Jest, put each shard’s coverage-final.json in one folder, then run nyc merge and nyc report. In Python, run coverage combine with a [paths] mapping.

A diagram of three runners. Vitest shards with --reporter=blob write .vitest/blob JSON files that vitest --merge-reports --coverage turns into one report. Jest shards with --coverageDirectory write coverage-final.json files that nyc merge and nyc report combine. coverage.py with parallel = true writes .coverage files that coverage combine merges.
Shards to one report: merge the per-line data, not summaries.

Merge commands at a glance

RunnerEach shardThen
Vitestvitest run --coverage --reporter=blob --shard=1/2vitest --merge-reports --coverage
Jestjest --coverage --shard=1/2 --coverageDirectory=coverage/shard-1Copy coverage-final.json files into one folder, nyc merge, nyc report
coverage.pycoverage run with parallel = true, or rename .coverage per jobcoverage combine, with [tool.coverage.paths]

We measured each with two shards of a small project, one testing pricing and one testing shipping: Vitest 5.1.0 with the V8 provider, Jest 30.5.2 with nyc 18, and coverage.py 7.17.2 with pytest-cov 7.1.0. Each correct merge matched a single run of all the tests.

A grid of eight merge attempts against two shards, pricing and shipping. Vitest blob with merge-reports: both, 5/6 lines. Vitest without blob: shipping only, 2/6. Vitest blob without coverage: empty. Jest nyc merge from one folder: both, 7/8. Jest default coverage folder: pricing only, 4/8. Jest nyc merge on subfolders: empty. Python combine with path mapping: both, 83%. Python two runs without parallel: shipping only, 33%.
Which shard's coverage survived each merge attempt.

Vitest: blob reports and –merge-reports

vitest run --coverage --reporter=blob --shard=1/2
vitest run --coverage --reporter=blob --shard=2/2
vitest --merge-reports --coverage

Each shard writes .vitest/blob/blob-1-2.json and blob-2-2.json. The merge reported 5 of 6 lines, the same as one full run. In CI, upload .vitest/blob from every job and download them all into that folder before merging.

What we triedWhat happened
Two shards with --coverage, no blobcoverage/ held only the last shard: 2 of 6 lines
Blob shards without --coverageMerge wrote an empty report, and exited 0
--merge-reports without --coverageNo merged coverage; coverage/ still held shard 2
Both shards writing --outputFile=.vitest/blob/blob-1.jsonOne overwrote the other: 2 of 6 lines

Jest: nyc merge

Jest has no merge command. Write each shard’s coverage to its own folder, then merge the coverage-final.json files with nyc:

jest --coverage --shard=1/2 --coverageDirectory=coverage/shard-1
jest --coverage --shard=2/2 --coverageDirectory=coverage/shard-2

mkdir -p shards merged
cp coverage/shard-1/coverage-final.json shards/shard-1.json
cp coverage/shard-2/coverage-final.json shards/shard-2.json
nyc merge shards merged/coverage-final.json
nyc report --temp-dir merged --reporter=lcov --reporter=text

This reported 7 of 8 lines, the same as one run. Copying the files straight into .nyc_output and running nyc report worked too.

What we triedWhat happened
Two shards in the default coverage/The second overwrote the first: 4 of 8 lines
nyc merge coverage with the shard files in subfoldersExit 0, empty report: it reads only the top folder
Both files copied under one nameOne shard lost: 4 of 8 lines
nyc merge on coverage-summary.json files“Invalid file coverage object”, exit 1

Keep json in coverageReporters: coverage-final.json has the per-line counts that a merge needs, and the summary has only totals.

Python: coverage combine

[tool.coverage.run]
source = ["shop"]
branch = true
parallel = true

[tool.coverage.paths]
source = ["src/", "*/src/"]

With parallel = true, each coverage run writes its own .coverage.<host>.<pid>.<random> file. coverage combine merges them into .coverage and deletes the inputs; --keep leaves them. coverage report combined them by itself in 7.17.2. For pytest-cov in separate CI jobs, rename each job’s .coverage to something like .coverage.job1, download them into one folder and run coverage combine.

What we triedWhat happened
Two coverage run calls, no parallelThe second overwrote .coverage: 33%
The second with --appendCombined: 83%
Two pytest-cov runs in one folderThe second deleted the first’s .coverage.pricing
Two checkouts at different paths, no [paths]Each file listed twice, 42% over 16 statements
[paths] pattern /tmp/ci/*/src/ on macOSNo match: paths were recorded under /private/tmp
[paths] with */src/, or relative_files = trueMerged: 83% over 8 statements

Use --cov-append when several pytest-cov runs share a folder.

What Supercov adds

npx supercov -- npx jest --shard=1/2
npx supercov -- npx jest --shard=2/2
npx supercov merge
npx supercov runs check --min-branches 80

supercov merge with no runs named combined the two shard runs on the same source, and refuses runs from a different source or configuration rather than add them up. The merged run keeps which test covered each line, with branches and MC/DC, and runs check gates it. See Jest coverage threshold for the floors, and Vitest coverage for the providers.

More coverage guides