简体   繁体   中英

Jest: How to merge coverage reports from different jest test runs

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.

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

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

To merge, I usually use nyc with --no-clean option, like this:

{
    "cover": "npm run cover:unit && npm run cover:integration && npm run cover:report",
    "cover:unit": "nyc --silent npm run test:unit",
    "cover:integration": "nyc --silent --no-clean npm run test:integration",
    "cover:report": "nyc report --reporter=text-lcov > coverage.lcov",
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM