-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support monocart report #521
Changes from 5 commits
2308d4a
caae21f
1a7d020
59a6161
4e856a2
28be91c
6c20c54
7e60c5c
aecd2b3
2192d25
1a82a95
4dbfe35
27966b3
5aa4422
9566904
2218b53
237d262
4830f62
1d2ff88
85303b4
714cdd6
c88abbf
6fd00e5
4b0947a
67bbd57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ const getSourceMapFromFile = require('./source-map-from-file') | |
const v8toIstanbul = require('v8-to-istanbul') | ||
const util = require('util') | ||
const debuglog = util.debuglog('c8') | ||
const { CoverageReport } = require('monocart-coverage-reports') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should move this into a try/catch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we should move if into a try/catch in a separate monocart.js helper file, then we can mock that file to test the help output. |
||
|
||
class Report { | ||
constructor ({ | ||
|
@@ -36,7 +37,8 @@ class Report { | |
allowExternal = false, | ||
skipFull, | ||
excludeNodeModules, | ||
mergeAsync | ||
mergeAsync, | ||
monocartArgv | ||
}) { | ||
this.reporter = reporter | ||
this.reporterOptions = reporterOptions || {} | ||
|
@@ -60,6 +62,7 @@ class Report { | |
this.src = this._getSrc(src) | ||
this.skipFull = skipFull | ||
this.mergeAsync = mergeAsync | ||
this.monocartArgv = monocartArgv | ||
} | ||
|
||
_getSrc (src) { | ||
|
@@ -73,6 +76,9 @@ class Report { | |
} | ||
|
||
async run () { | ||
if (this.monocartArgv) { | ||
return this.runMonocart() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we were unable to require Monocart, we should print the install instructions here and exit with 1. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have made the following improvements based on your suggestion
Please review, thanks. |
||
} | ||
const context = libReport.createContext({ | ||
dir: this.reportsDirectory, | ||
watermarks: this.watermarks, | ||
|
@@ -89,6 +95,128 @@ class Report { | |
} | ||
} | ||
|
||
async runMonocart () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 I prefer now that we're sharing some of the same behaviour in |
||
const argv = this.monocartArgv | ||
const exclude = this.exclude | ||
|
||
function getEntryFilter () { | ||
if (argv.entryFilter) { | ||
return argv.entryFilter | ||
} | ||
if (argv.filter) { | ||
return argv.filter | ||
} | ||
return (entry) => { | ||
return exclude.shouldInstrument(fileURLToPath(entry.url)) | ||
} | ||
} | ||
|
||
function getSourceFilter () { | ||
if (argv.sourceFilter) { | ||
return argv.sourceFilter | ||
} | ||
if (argv.filter) { | ||
return argv.filter | ||
} | ||
return (sourcePath) => { | ||
if (argv.excludeAfterRemap) { | ||
// console.log(sourcePath) | ||
return exclude.shouldInstrument(sourcePath) | ||
} | ||
return true | ||
} | ||
} | ||
|
||
function getReports () { | ||
const reports = Array.isArray(argv.reporter) ? argv.reporter : [argv.reporter] | ||
const reporterOptions = argv.reporterOptions || {} | ||
|
||
return reports.map((reportName) => { | ||
const reportOptions = { | ||
...reporterOptions[reportName] | ||
} | ||
if (reportName === 'text') { | ||
reportOptions.skipEmpty = false | ||
reportOptions.skipFull = argv.skipFull | ||
reportOptions.maxCols = process.stdout.columns || 100 | ||
} | ||
return [reportName, reportOptions] | ||
}) | ||
} | ||
|
||
// --all: add empty coverage for all files | ||
function getAllOptions () { | ||
if (!argv.all) { | ||
return | ||
} | ||
|
||
const src = argv.src | ||
const workingDirs = Array.isArray(src) ? src : (typeof src === 'string' ? [src] : [process.cwd()]) | ||
return { | ||
dir: workingDirs, | ||
filter: (filePath) => { | ||
return exclude.shouldInstrument(filePath) | ||
} | ||
} | ||
} | ||
|
||
function initPct (summary) { | ||
Object.keys(summary).forEach(k => { | ||
if (summary[k].pct === '') { | ||
summary[k].pct = 100 | ||
} | ||
}) | ||
return summary | ||
} | ||
|
||
// adapt coverage options | ||
const coverageOptions = { | ||
logging: argv.logging, | ||
name: argv.name, | ||
inline: argv.inline, | ||
lcov: argv.lcov, | ||
outputDir: argv.reportsDir, | ||
clean: argv.clean, | ||
|
||
reports: getReports(), | ||
all: getAllOptions(), | ||
|
||
// use default value for istanbul | ||
defaultSummarizer: 'pkg', | ||
|
||
entryFilter: getEntryFilter(), | ||
sourceFilter: getSourceFilter(), | ||
|
||
onEnd: (coverageResults) => { | ||
// for check coverage | ||
this._allCoverageFiles = { | ||
files: () => { | ||
return coverageResults.files.map(it => it.sourcePath) | ||
}, | ||
fileCoverageFor: (file) => { | ||
const fileCoverage = coverageResults.files.find(it => it.sourcePath === file) | ||
return { | ||
toSummary: () => { | ||
return initPct(fileCoverage.summary) | ||
} | ||
} | ||
}, | ||
getCoverageSummary: () => { | ||
return initPct(coverageResults.summary) | ||
} | ||
} | ||
} | ||
} | ||
const coverageReport = new CoverageReport(coverageOptions) | ||
coverageReport.cleanCache() | ||
|
||
// read v8 coverage data from tempDirectory | ||
await coverageReport.addFromDir(argv.tempDirectory) | ||
|
||
// generate report | ||
await coverageReport.generate() | ||
} | ||
|
||
async getCoverageMapFromAllCoverageFiles () { | ||
// the merge process can be very expensive, and it's often the case that | ||
// check-coverage is called immediately after a report. We memoize the | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should explain what monocart is a bit better. It's an alternate tool for converting raw V8 coverage reports to other coverage formats, as an alternate to
v8-to-istanbul
and istanbul reports.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added some explanations, please take a look.