2

Using Jest v28 --shard flag significantly speeds up tests in our CI/CD pipeline, but it creates the issue of having to merge multiple coverage reports into a single file (required for our code quality report on PRs).

I am forced to use lcov format for the coverage report. I am able to merge the coverage report using lcov CLI tool:

lcov --add-tracefile ./coverage-unit/lcov-1.info --add-tracefile ./coverage-unit/lcov-2.info  --output-file ./coverage-unit/lcov.info

But the merged ./coverage-unit/lcov.info file does not pick up the branch data:

Combining tracefiles.
Reading tracefile ./coverage-unit/lcov-1.info
Reading tracefile ./coverage-unit/lcov-2.info
Writing data to ./coverage-unit/lcov.info
Summary coverage rate:
  lines......: 87.9% (5003 of 5691 lines)
  functions..: 85.4% (543 of 636 functions)
  branches...: no data found

This results in a merged lcov.info that is omitting the BRH, BRF, and BRDA fields.

Is there a proper way to merge lcov report files?

Flash
  • 543
  • 2
  • 7
  • 21

2 Answers2

0

lcov has branch coverage data disabled by default. Using the lcov_branch_coverage=1 flag enables it.

The following command properly merges the coverage reports with branch coverage data:

lcov --rc lcov_branch_coverage=1 \
  --add-tracefile ./coverage-unit/lcov-1.info \
  --add-tracefile ./coverage-unit/lcov-2.info  \
  --output-file ./coverage-unit/lcov.info
Flash
  • 543
  • 2
  • 7
  • 21
0

My answer shows a different solution, instead of working with lcov report files. This is justified as it is easy to produce different output by just adding a different coverage reporter to the jest configuration.

I was able to merge the coverage reports of my shards by copying them all together in a directory called .nyc_output [1]. To be precise, you need the json coverage reporter and you need to copy the coverage-final.json files (they're named exactly like this) to the .nyc_output directory (renaming them, e.g. by adding the shard number to the file name). Contrary to the linked reference, you don't need a single out.json file. Any number of json files (with arbitrary names) will be picked up when generating the report.

For example:

$ ls .nyc_output
coverage-shard-1.json  coverage-shard-2.json  coverage-shard-3.json 

From there (to be precise, from a parent folder relative to .nyc_output), you can just run npx nyc report --reporter=html (you might want to add a version number to the nyc package, i.e. at the time of writing this, nyc@latest equals nyc@15). This will generate the HTML coverage report and save it to the coverage directory.

If you're not interested in an HTML report it might work by specifying other reporters in the --reporter argument (I did not test this). You can alternatively merge all the json files together into a single json file like this [2]:

npx nyc merge .nyc_output your-merged-coverage.json

References:

  1. https://istanbul.js.org/docs/advanced/coverage-object-report/
  2. https://github.com/istanbuljs/nyc#what-about-nyc-merge
NicBright
  • 7,289
  • 5
  • 18
  • 19