Last active
January 3, 2020 08:06
-
-
Save boyanio/36b9d6d9b32fae761250f7bd4fd6cbb4 to your computer and use it in GitHub Desktop.
Merge code coverage reports from multiple JSON ones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const glob = require('glob'); | |
const istanbulReport = require('istanbul-lib-report'); | |
const istanbulCoverage = require('istanbul-lib-coverage'); | |
const istanbulReports = require('istanbul-reports'); | |
const reporters = ['cobertura', 'html']; | |
const coverageDir = './coverage'; | |
const coverageMergeDir = './coverage/merged'; | |
const coverageJsonFiles = findCoverageJsonFiles(coverageDir); | |
const coverageMap = istanbulCoverage.createCoverageMap(); | |
mergeCoverageJsonFilesIntoCoverageMap(coverageMap, coverageJsonFiles); | |
createMergedCoverageReports(coverageMap, coverageMergeDir, reporters); | |
function findCoverageJsonFiles(rootDir) { | |
return glob | |
.sync('**/coverage-final.json', { cwd: rootDir }) | |
.map(relativeCoverageJsonFile => | |
path.resolve(rootDir, relativeCoverageJsonFile) | |
); | |
} | |
function mergeCoverageJsonFilesIntoCoverageMap(coverageMap, coverageJsonFiles) { | |
coverageJsonFiles | |
.map(file => JSON.parse(fs.readFileSync(file, 'utf8'))) | |
.forEach(coverage => coverageMap.merge(coverage)); | |
} | |
function createMergedCoverageReports(coverageMap, outputDir, reporters) { | |
const context = istanbulReport.createContext({ coverageMap, dir: outputDir }); | |
reporters.forEach(reporter => { | |
istanbulReports.create(reporter, {}).execute(context); | |
console.log(`${reporter} merged coverage report generated`); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment