Skip to content
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

Merged
merged 25 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Here is a list of common options. Run `c8 --help` for the full list and document
| `--per-file` | check thresholds per file | `boolean` | `false` |
| `--temp-directory` | directory V8 coverage data is written to and read from | `string` | `process.env.NODE_V8_COVERAGE` |
| `--clean` | should temp files be deleted before script execution | `boolean` | `true` |
| `--experimental-monocart` | see [section below](#using-monocart-coverage-reports-experimental) for more info | `boolean` | `false` |

## Checking for "full" source coverage using `--all`

Expand Down Expand Up @@ -119,6 +120,23 @@ The `--100` flag can be set for the `check-coverage` as well:
c8 check-coverage --100
```

## Using Monocart coverage reports (experimental)
Monocart is an alternate library for outputting [v8 code coverage](https://v8.dev/blog/javascript-code-coverage) data as Istanbul reports.

Monocart also provides reporters based directly on v8's byte-offset-based output. Such as, `console-details` and `v8`. This removes a complex transformation step and may be less bug prone for some environments.

**Example usage:**

```sh
c8 --experimental-monocart --reporter=v8 --reporter=console-details node foo.js
```

NOTE: Monocart requires additional `monocart-coverage-reports` to be installed:

```sh
npm i monocart-coverage-reports --save-dev
```

## Ignoring Uncovered Lines, Functions, and Blocks

Sometimes you might find yourself wanting to ignore uncovered portions of your
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ exports.outputReport = async function (argv) {
src: argv.src,
skipFull: argv.skipFull,
excludeNodeModules: argv.excludeNodeModules,
mergeAsync: argv.mergeAsync
mergeAsync: argv.mergeAsync,
monocartArgv: (argv.experimentalMonocart || process.env.EXPERIMENTAL_MONOCART) ? argv : null
})
await report.run()
if (argv.checkCoverage) await checkCoverages(argv, report)
Expand Down
5 changes: 5 additions & 0 deletions lib/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ function buildYargs (withCommands = false) {
describe: 'supplying --merge-async will merge all v8 coverage reports asynchronously and incrementally. ' +
'This is to avoid OOM issues with Node.js runtime.'
})
.option('experimental-monocart', {
default: false,
type: 'boolean',
describe: 'Use Monocart coverage reports'
})
.pkgConf('c8')
.demandCommand(1)
.check((argv) => {
Expand Down
138 changes: 137 additions & 1 deletion lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class Report {
allowExternal = false,
skipFull,
excludeNodeModules,
mergeAsync
mergeAsync,
monocartArgv
}) {
this.reporter = reporter
this.reporterOptions = reporterOptions || {}
Expand All @@ -60,6 +61,7 @@ class Report {
this.src = this._getSrc(src)
this.skipFull = skipFull
this.mergeAsync = mergeAsync
this.monocartArgv = monocartArgv
}

_getSrc (src) {
Expand All @@ -73,6 +75,9 @@ class Report {
}

async run () {
if (this.monocartArgv) {
return this.runMonocart()
Copy link
Owner

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@cenfun cenfun Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made the following improvements based on your suggestion

  • Move the import library to try/catch. see function getMonocart ()
  • Use the import("xxx") function to load the lib asynchronously. It should be good enough without creating a new help file, and added new test case to cover it (rename the lib entry file can mock it).
  • Print the install instructions and exit with 1 when failed to load the lib.

Please review, thanks.

}
const context = libReport.createContext({
dir: this.reportsDirectory,
watermarks: this.watermarks,
Expand All @@ -89,6 +94,137 @@ class Report {
}
}

async getMonocart () {
let MCR
try {
MCR = await import('monocart-coverage-reports')
} catch (e) {
console.error('ERROR: Failed to import module "monocart-coverage-reports". Please try to install it: npm i monocart-coverage-reports --save-dev')
bcoe marked this conversation as resolved.
Show resolved Hide resolved
process.exit(1)
}
return MCR
}

async runMonocart () {
Copy link
Owner

Choose a reason for hiding this comment

The 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 report.js.

const MCR = await this.getMonocart()
if (!MCR) {
return
}

const argv = this.monocartArgv
const exclude = this.exclude

function getEntryFilter () {
return argv.entryFilter || argv.filter || function (entry) {
return exclude.shouldInstrument(fileURLToPath(entry.url))
}
}

function getSourceFilter () {
return argv.sourceFilter || argv.filter || function (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,

reports: getReports(),

outputDir: argv.reportsDir,
baseDir: argv.baseDir,

entryFilter: getEntryFilter(),
sourceFilter: getSourceFilter(),

inline: argv.inline,
lcov: argv.lcov,

all: getAllOptions(),

clean: argv.clean,

// use default value for istanbul
defaultSummarizer: 'pkg',

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 MCR.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
Expand Down
Loading