I currently maintain parallelized Cypress tests in CI with Jenkins. We’re using IstanbulJS a.k.a. nyc
for code coverage. As of August 2024, there is an inconsistency in coverage output that breaks the merge
command — coverage info is sometimes available at json.fileName and sometimes at json.fileName.data.
We can work around this with a simple NodeJS script:
// extractData.js
const fs = require("fs");
const args = process.argv.slice(2);
args.forEach(file => {
const fileContent = JSON.parse(fs.readFileSync(file, "utf-8"));
for (const key of Object.keys(fileContent)) {
let newValue = fileContent[key];
if (fileContent[key].data) {
newValue = fileContent[key].data;
}
fileContent[key] = newValue;
}
fs.writeFileSync(file, JSON.stringify(fileContent));
});
All we have to do is clean the coverage files with this script before running the typical merge command. Our package.json scripts look like this:
// package.json
scripts: {
...
"coverage:component": "npx --no nyc report --reporter=json --report-dir=$PARALLEL_STEP",
"coverage:mv": "mv $PARALLEL_STEP/coverage-final.json merging/coverage-final-$PARALLEL_STEP.json",
"coverage:clean": "node extractData.js merging/coverage-final-$PARALLEL_STEP.json",
"coverage:merge": "npx --no nyc report --reporter=cobertura --temp-dir=merging --report-dir=final",
"test:ciwithcoverage": "npm run coverage:component && npm run coverage:mv && npm run coverage:clean"
...
},
We invoke test:ciwithcoverage
in each parallel stage and invoke npm run coverage:merge
once after all stages have finished.