23

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.

3 Answers 3

36

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
5
  • 2
    Many thanks @jrr for including and highlighting deprecation and highlighting an alternative :) Jul 22, 2020 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: github.com/istanbuljs/nyc/issues?q=is%3Aissue+is%3Aopen+merge
    – jrr
    May 21, 2021 at 4:26
  • 2
    unfortunately the reports are not at all accurate, and significantly underreport coverage levels. nyc appears to be abandoned also.
    – Dom Barker
    Apr 8, 2022 at 10:43
  • 3
    so what's the latest tool that could do this in July 2022? Jul 18, 2022 at 12:52
  • 1
    @peter-poliwoda github.com/ljharb/istanbul-merge, see the other answer
    – sezanzeb
    Nov 23, 2022 at 14:48
31

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

4
  • 1
    ...and it worked :) thanks again @nalmada, much appreciated!!! Jun 29, 2020 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 ( npmjs.com/package/istanbul ).
    – jrr
    Jul 21, 2020 at 5:43
  • I'm running two coverage tests in a monorepo but the reports are on separate levels so to speak, one is at <root> level and the second at <root>/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? Jul 18, 2022 at 12:50
  • OK, FYI isanbul merge doesnt fail if you dont actually have a coverage-final.json fie Jul 18, 2022 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.