Merge JSON coverage reports from Jest

749 Views Asked by At

I'm currently collecting coverage for modules within a Lerna monorepo. Within each package.json I run jest --coverage --json --outputFile someDir.

This creates multiple JSON reports, with a schema like:

{
  "numFailedTestSuites": 0,
  "numFailedTests": 0,
  "numPassedTestSuites": 0,
  "numPassedTests": 18,
  "numPendingTestSuites": 0,
  "numPendingTests": 0,
  "numRuntimeErrorTestSuites": 0,
  "numTodoTests": 0,
  "numTotalTestSuites": 8,
  "numTotalTests": 18,
  "openHandles": [],
  "snapshot": {
    // ...
  }
  // ...
}

I want to merge all of these outputs into one JSON report at the root directory. My main objective is to collect and report the coverage through a GitHub Actions workflow (which needs the --json report from Jest).

I've been attempting to use Istanbul to merge this, however, it seems to only work with the coverage maps, so with the coverage-final.json file which is produced.

When I try to merge from the JSON reports produced, I get:

Error: Coverage must be initialized with a path or an object

I assume this is because the schema is incorrect.

1

There are 1 best solutions below

1
Raky On

In order to merge multiple JSON reports from Jest into a single report, you can use the istanbul-merge tool provided by Istanbul. However, as you mentioned, Istanbul typically works with coverage maps and the coverage-final.json file.

To merge the Jest JSON reports, you'll need to convert them into the Istanbul-compatible coverage format. Here's an example workflow you can follow:

Install the required packages by running the following command in your project's root directory:

npm install --save-dev istanbul istanbul-merge

Create a new script in your project's package.json file to handle the merging process. Add the following script under the scripts section:

"scripts": {
  "merge-coverage": "node merge-coverage.js"
}

Create a new file named merge-coverage.js in your project's root directory. This file will contain the code to merge the JSON reports. Add the following code to merge-coverage.js:

const fs = require('fs');
const istanbul = require('istanbul');
const merge = require('istanbul-merge');

const mergedReportPath = './coverage/merged-report.json'; // Adjust the output path as needed
const reportPaths = [
  // Specify the paths of your individual Jest JSON reports
  './path/to/report1.json',
  './path/to/report2.json',
  // Add more report paths as needed
];

// Load the reports
const reports = reportPaths.map((reportPath) => JSON.parse(fs.readFileSync(reportPath, 'utf8')));

// Merge the reports
const mergedReport = merge.createCoverageMap();

for (const report of reports) {
  const coverageMap = istanbul.createCoverageMap(report);
  mergedReport.merge(coverageMap);
}

// Write the merged report to a file
fs.writeFileSync(mergedReportPath, JSON.stringify(mergedReport.toJSON(), null, 2));

console.log('Coverage reports merged successfully!');

Run the merging script by executing the following command:

npm run merge-coverage

This will execute the merge-coverage script and generate the merged coverage report at the specified mergedReportPath.

You can now use the merged report file (merged-report.json) for your GitHub Actions workflow or any other coverage reporting purposes.

With above steps, you should be able to merge the individual Jest JSON reports into a single JSON report using Istanbul and generate the desired coverage report for your GitHub Actions workflow.

Hope this will work for you.