Skip to content

Commit

Permalink
fix: improve performance combining coverage on large codebases (#753)
Browse files Browse the repository at this point in the history
Co-authored-by: Jennifer Shehane <jennifer@cypress.io>
  • Loading branch information
koenvangeert and jennifer-shehane authored Oct 2, 2024
1 parent e18f4ec commit bf696bb
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions task.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const {
includeAllFiles
} = require('./task-utils')
const { fixSourcePaths } = require('./support-utils')
const { removePlaceholders } = require('./common-utils')

const debug = require('debug')('code-coverage')

Expand Down Expand Up @@ -59,6 +58,13 @@ const nycReportOptions = (function getNycOption() {

const nycFilename = join(nycReportOptions['temp-dir'], 'out.json')

let coverageMap = (() => {
const previousCoverage = existsSync(nycFilename)
? JSON.parse(readFileSync(nycFilename, 'utf8'))
: {}
return istanbul.createCoverageMap(previousCoverage)
})()

function saveCoverage(coverage) {
if (!existsSync(nycReportOptions.tempDir)) {
mkdirSync(nycReportOptions.tempDir, { recursive: true })
Expand Down Expand Up @@ -122,7 +128,7 @@ const tasks = {
resetCoverage({ isInteractive }) {
if (isInteractive) {
debug('reset code coverage in interactive mode')
const coverageMap = istanbul.createCoverageMap({})
coverageMap = istanbul.createCoverageMap({})
saveCoverage(coverageMap)
}
/*
Expand All @@ -148,21 +154,7 @@ const tasks = {

fixSourcePaths(coverage)

const previousCoverage = existsSync(nycFilename)
? JSON.parse(readFileSync(nycFilename, 'utf8'))
: {}

// previous code coverage object might have placeholder entries
// for files that we have not seen yet,
// but the user expects to include in the coverage report
// the merge function messes up, so we should remove any placeholder entries
// and re-insert them again when creating the report
removePlaceholders(previousCoverage)

const coverageMap = istanbul.createCoverageMap(previousCoverage)
coverageMap.merge(coverage)
saveCoverage(coverageMap)
debug('wrote coverage file %s', nycFilename)

return null
},
Expand All @@ -172,6 +164,7 @@ const tasks = {
* NPM script to generate HTML report
*/
coverageReport() {
saveCoverage(coverageMap)
if (!existsSync(nycFilename)) {
console.warn('Cannot find coverage file %s', nycFilename)
console.warn('Skipping coverage report')
Expand Down

0 comments on commit bf696bb

Please sign in to comment.