Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 480
anon_dcs3spp Avatar asked Jun 24 '20 16:06

anon_dcs3spp


People also ask

How does jest collect coverage?

Jest is collecting coverage only on the function under tests, not from the entire project. This means that despite we are seeing 100% coverage here, potentially we are testing only a fraction of our code. Now Jest is identify correctly what needs to be tested.

Does jest have code coverage?

Jest, a flexible, easy-to-use testing framework, will be used to test a simple Typescript application. Codecov, a tool for monitoring code coverage, will be used to measure the test coverage for your Jest unit tests.


Video Answer


2 Answers

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

like image 112
nalmada Avatar answered Oct 27 '22 17:10

nalmada


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
like image 35
jrr Avatar answered Oct 27 '22 17:10

jrr