25

Has anyone managed to combine test coverage report from two separate jest test runs?

I am newbie trying to use the default jest coverage reporters: ["json", "lcov", "text", "clover"]

I have tried using nyc to combine coverage-final*.json files from tmp folder and output to a full-test-coverage/ folder.

npx nyc report --report-dir=full-test-coverage/ --reporter=html -t tmp 

The full-test-coverage folder is created with index.html etc. However, the combined report is empty.

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
anon_dcs3spp
  • 2,342
  • 2
  • 28
  • 62

4 Answers4

37

I managed to get it working with nyc. Steps:

  • Collect multiple coverage reports using coverage reporter "json"
  • Put them all in one directory (which in my case required renaming multiple coverage-final.json files)
  • nyc merge multiple-sources-dir merged-output/merged-coverage.json
  • nyc report -t merged-output --report-dir merged-report --reporter=html --reporter=cobertura
Hongbo Miao
  • 45,290
  • 60
  • 174
  • 267
jrr
  • 1,877
  • 19
  • 29
  • 2
    Many thanks @jrr for including and highlighting deprecation and highlighting an alternative :) – anon_dcs3spp Jul 22 '20 at 13:40
  • For anyone trying this out, I'm still using it and it still works, but I don't have a ton of confidence in the accuracy of the report produced by `nyc merge`, especially when the inputs came from different tools. Related issues: https://github.com/istanbuljs/nyc/issues?q=is%3Aissue+is%3Aopen+merge – jrr May 21 '21 at 04:26
  • 2
    unfortunately the reports are not at all accurate, and significantly underreport coverage levels. nyc appears to be abandoned also. – Dom Barker Apr 08 '22 at 10:43
  • 3
    so what's the latest tool that could do this in July 2022? – Peter Poliwoda Jul 18 '22 at 12:52
  • 1
    @peter-poliwoda https://github.com/ljharb/istanbul-merge, see the other answer – sezanzeb Nov 23 '22 at 14:48
32

I was struggling with this too but I managed to do it by using the istanbul-merge package

So assuming that you want to merge two test coverage named coverage-final.json located in two different folders f1 and f2,and name the output f3/coverage.json you can do :

npx istanbul-merge --out coverage.json ./f1/coverage-final.json ./f2/coverage-final.json

and then use instanbul to create the HTML report :

npx istanbul report --include coverage.json --dir f3 html

nalmada
  • 472
  • 4
  • 8
  • 1
    ...and it worked :) thanks again @nalmada, much appreciated!!! – anon_dcs3spp Jun 29 '20 at 15:21
  • 4
    I started with this approach (which works!), but when looking up other options for `istanbul` I discovered the tool's deprecated ( https://www.npmjs.com/package/istanbul ). – jrr Jul 21 '20 at 05:43
  • I'm running two coverage tests in a monorepo but the reports are on separate levels so to speak, one is at level and the second at /packages/my-app, the combined report loses a lot of the files maybe due to similarly named directoy structure. How do I get the report to be generated properly with all the files included? – Peter Poliwoda Jul 18 '22 at 12:50
  • OK, FYI isanbul merge doesnt fail if you dont actually have a coverage-final.json fie – Peter Poliwoda Jul 18 '22 at 13:08
0

Building off of another answer, here is a script that will collect all the coverage files from multiple yarn workspaces and merge them:

#! /bin/bash

set -e

rm -rf coverage
mkdir -p coverage/workspaces
yarn workspaces foreach -Apv exec bash -c '[ ! -f coverage/coverage-final.json ] && exit 0 || cp coverage/coverage-final.json '$(pwd)'/coverage/workspaces/$(basename $(pwd))-coverage-final.json'
yarn run nyc merge coverage/workspaces coverage/monorepo-coverage.json
yarn run nyc report -t coverage --report-dir coverage/html --reporter=html-spa

Note that the $(pwd) executes in the script's context (I close and re-open the single quotes around that command) while the other subshells are quoted and will occur in the context of the workspace exec (and so have a CWD of the workspace.

Carl G
  • 17,394
  • 14
  • 91
  • 115
0

As of 2023, this solution works for me, but my use case was a bit different. I got coverage from two directories in a monorepo, and wanted to merge them in various coverage report types(for other tooling in CI):

  1. intall istanbul-merge and nyc packages (I got v2.0.0 and 15.1.0 respectively)
  2. add these three scripts provided that you want to merge the reports after running all tests via yarn test:
"merge-coverage-report-jsons": "istanbul-merge --out coverage-temp/coverage-final.json packages/ui/coverage/coverage-final.json packages/plugins-core/coverage/coverage-final.json",
"generate-coverage-all-packages": "nyc report --reporter=html --reporter=text --reporter=lcov --reporter=clover --report-dir=coverage --temp-dir=coverage-temp",
"posttest": "yarn merge-coverage-report-jsons && yarn generate-coverage-all-packages",