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.
In order to merge multiple JSON reports from Jest into a single report, you can use the
istanbul-mergetool provided by Istanbul. However, as you mentioned, Istanbul typically works with coverage maps and thecoverage-final.jsonfile.To merge the Jest
JSONreports, 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:
Create a new script in your project's package.json file to handle the merging process. Add the following script under the scripts section:
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 tomerge-coverage.js:Run the merging script by executing the following command:
This will execute the
merge-coveragescript 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
Istanbuland generate the desired coverage report for your GitHub Actions workflow.Hope this will work for you.