Skip to content

Instantly share code, notes, and snippets.

@dyaa
Last active February 5, 2020 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyaa/f5b729385f966ecdc1bcc60ee0218dc7 to your computer and use it in GitHub Desktop.
Save dyaa/f5b729385f966ecdc1bcc60ee0218dc7 to your computer and use it in GitHub Desktop.
Jest coverage collection from multi-project and merging them into single coverage .lcov file
import fs from 'fs-extra';
import glob from 'glob';
import { exec } from 'child_process';
const coverageOutputDir = '.nyc_output';
fs.emptyDirSync(coverageOutputDir);
glob('**/coverage-final.json', (err, coverageFiles) => {
if (err) {
throw err;
}
if (!coverageFiles.length) {
throw new Error('Error finding coverage files');
}
coverageFiles.forEach((coverageFile, index) => {
if (!coverageFile) {
throw new Error('Error finding coverage files');
}
const [fileName, fileExtention] = (coverageFile
.split('/')
.pop() as string).split('.');
const destination = `${coverageOutputDir}/${fileName}-${index +
1}.${fileExtention}`;
fs.copyFile(coverageFile, destination, copyFileError => {
if (copyFileError) {
throw copyFileError;
}
// tslint:disable-next-line no-console
console.log(`${coverageFile} was copied to ${destination}`);
});
});
exec(
`./node_modules/nyc/bin/nyc.js report --reporter=lcov --report-dir=${coverageOutputDir}`,
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment