Skip to content

Commit

Permalink
Increase code quality by increasing coverage: lib/report.js (#452)
Browse files Browse the repository at this point in the history
    refactor: refactor some of the functions in lib/report.js to target
    certain versions of node, to ignore certain lines by c8 for
    consideration of removal, and to increase code coverage (#452)

    test: Added two test cases in test/report.js to test error handling (#452)
    test: Added file test/report-mock.js to support test cases in test/report.js (#452)
    test: moved the fake-source-map.js test inside of a describe block. (#452)

    Expose the Report Class for future extension (#452)

    refactor: lib/report.js to expose the class Report (#452)
    refactor: lib/command/* and test/fixtures/report/* to import report constructor (#452)
  • Loading branch information
mcknasty committed Jan 8, 2024
1 parent 5e18365 commit aa14b3c
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 3,390 deletions.
43 changes: 27 additions & 16 deletions lib/report.js
Expand Up @@ -73,10 +73,11 @@ class Report {
}

async run () {
const coverageMap = await this.getCoverageMapFromAllCoverageFiles()
const context = libReport.createContext({
dir: this.reportsDirectory,
watermarks: this.watermarks,
coverageMap: await this.getCoverageMapFromAllCoverageFiles()
coverageMap: coverageMap
})

for (const _reporter of this.reporter) {
Expand Down Expand Up @@ -117,6 +118,8 @@ class Report {
})
await converter.load()

// Todo: Might be able to remove this whole block. Not sure what it accomplishes
/* c8 ignore next 5 */
if (resultCountPerPath.has(path)) {
resultCountPerPath.set(path, resultCountPerPath.get(path) + 1)
} else {
Expand Down Expand Up @@ -147,19 +150,21 @@ class Report {
_getSourceMap (v8ScriptCov) {
const sources = {}
const sourceMapAndLineLengths = this.sourceMapCache[pathToFileURL(v8ScriptCov.url).href]
if (sourceMapAndLineLengths) {
// See: https://github.com/nodejs/node/pull/34305
if (!sourceMapAndLineLengths.data) return
sources.sourceMap = {
sourcemap: sourceMapAndLineLengths.data
}
if (sourceMapAndLineLengths.lineLengths) {
let source = ''
sourceMapAndLineLengths.lineLengths.forEach(length => {
source += `${''.padEnd(length, '.')}\n`
})
sources.source = source
}

// See: https://github.com/nodejs/node/pull/34305
const earlyExit = typeof sourceMapAndLineLengths === 'undefined' ||
(Object.keys(sourceMapAndLineLengths).includes('data') && sourceMapAndLineLengths.data === null)
if (earlyExit) return

sources.sourceMap = {
sourcemap: sourceMapAndLineLengths.data
}
if (sourceMapAndLineLengths.lineLengths) {
let source = ''
sourceMapAndLineLengths.lineLengths.forEach(length => {
source += `${''.padEnd(length, '.')}\n`
})
sources.source = source
}
return sources
}
Expand Down Expand Up @@ -341,6 +346,12 @@ class Report {
for (const v8ScriptCov of v8ProcessCov.result) {
// https://github.com/nodejs/node/pull/35498 updates Node.js'
// builtin module filenames:
// Remain backwards compatibility with previous version of Node.js
// While accurately reporting code coverage

// This line is only reached on the LTS versions of node 16 and up
// Going to ignore for now
/* c8 ignore next 3 */
if (/^node:/.test(v8ScriptCov.url)) {
v8ScriptCov.url = `${v8ScriptCov.url.replace(/^node:/, '')}.js`
}
Expand Down Expand Up @@ -397,6 +408,6 @@ class Report {
}
}

module.exports = function (opts) {
return new Report(opts)
module.exports = function (opts, instance = false) {
return (instance === false) ? new Report(opts) : Report
}

0 comments on commit aa14b3c

Please sign in to comment.