Repository navigation
feat(lib-report): add github-actions report - #43
sumanthratna wants to merge 10 commits into
Conversation
commit: |
| // workflow command format: https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-commands#setting-a-warning-message | ||
| this.cw!.println( | ||
| `::${this.level} file=${escapeProperty(fc.path)},line=${lines[0]},title=Uncovered code` + | ||
| `::Uncovered ${lines.length === 1 ? "line" : "lines"} ${lineRanges(lines)}`, | ||
| ); |
There was a problem hiding this comment.
So do these show up on the correct line in a PR similar to Vitest's test reporter's annotations are rendered? In your example workflow run it's probably not showing it correctly as there's no changed files? Can you post example of such using #43 (comment) in some example project?
Also are you aware of https://github.com/davelosert/vitest-coverage-report-action? I wonder if we should have something similar to its output.
There was a problem hiding this comment.
Also cc. @davelosert from https://github.com/davelosert/vitest-coverage-report-action. Looking at your action, it seems to do everything in post-processing step of a vitest --run command. Have you ever thought about using a custom coverage reporter instead? We could probably upstream some of your Github Action's logic directly into Vitest here. Your action would likely need to handle just Github comment posting then. 🤔
There was a problem hiding this comment.
Thanks, the demo caught a problem. The first version put one annotation per file at its first uncovered line, so new untested code further down a file that already had a gap got no marker of its own. I changed it to one annotation per run of uncovered lines (line + endLine), grouped like the text report's "Uncovered Line #s", so each gap sits next to its own code.
Demo PR: sumanthratna/vitest-coverage-annotations-demo#1. It adds a partly tested src/cart.ts and an untested addTax at lines 12-13 of src/price.ts, which already had uncovered lines 2-3. The demo installs a packed build of 2c2ff5d, since pkg.pr.new only builds after CI is approved.
The trade-off is GitHub's cap of 10 warning annotations per step. A project with more than 10 gaps sees the first 10 in report order, which may not include the lines the PR touched. Filtering to the PR's diff would fix that but needs the base ref and git, which a coverage report doesn't have.
I've seen vitest-coverage-report-action. It posts a summary comment with the coverage table and per-file numbers for changed files. This report handles the inline, per-line part. A reporter could also write that summary table to $GITHUB_STEP_SUMMARY, which needs no token. Posting the PR comment would stay with the action.
| /** | ||
| * the annotation level, defaults to `warning`. Use `error` when any | ||
| * uncovered line fails the build, as with 100% coverage thresholds. | ||
| */ | ||
| level?: AnnotationLevel; |
There was a problem hiding this comment.
level: "notice" | "warning" | "error", default "warning". Reports run before Vitest checks coverage.thresholds, so a green run with uncovered lines gets warnings. Projects whose thresholds fail on any uncovered line can set "error".
We could actually calculate thresholds in Vitest and give them to reporters as context. Somewhere here https://github.com/vitest-dev/vitest/blob/a718e12109d7b98bac10c9f376dfeb6fe1ec309f/packages/coverage-v8/src/provider.ts#L167-L183. Then this reporter could check each file's threshold (or global one) to derive the correct level.
There was a problem hiding this comment.
That would be better than a fixed level. If Vitest passes the resolved thresholds (global and per-glob) to reporters, this report can use error for files that fail their threshold and warning for the rest. Until that context exists, level lets projects pick by hand. I can open the Vitest side as a follow-up and switch this report to use it when it lands, or wait and do both here if you'd rather not ship level at all.
…report's uncovered lines
…of uncovered lines

For vitest-dev/vitest#12061.
Summary
Adds a
github-actionsreport: one annotation per run of uncovered lines, grouped like the text report's "Uncovered Line #s", so each gap shows next to its code in a PR's Files changed view.const reports = { clover: CloverReport, cobertura: CoberturaReport, + "github-actions": GithubActionsReport, html: HtmlReport,level:"notice" | "warning" | "error", default"warning". Reports run before Vitest checkscoverage.thresholds, so a green run with uncovered lines gets warnings. Projects whose thresholds fail on any uncovered line can set"error".file: the output file; defaults to the console, liketeamcityandtext-summary.context.writerlike the other reports, using the format and escaping of Vitest'sgithub-actionstest reporter. No@actions/coredependency.In Vitest:
coverage.reporter: [["github-actions", { level: "error" }]]. Vitest could also enable it by default whenGITHUB_ACTIONSis set, as it does for thegithub-actionstest reporter.Evidence
Before: a coverage gap shows up only in the
texttable in the job log. The run page has no annotations.After: demo PR that installs a packed build of this branch: Add cart totals sumanthratna/vitest-coverage-annotations-demo#1. It adds a partly tested
src/cart.tsand untested lines 12-13 at the end ofsrc/price.ts, which already had uncovered lines 2-3. The run reports:Each shows inline at its lines in Files changed. GitHub resolves the absolute paths to repo files on Linux, macOS, and Windows.
test/reports/github-actions/index.test.ts, 5 cases. Each goes red on its mutant:endLineset to the start line, a covered line not ending a run, never extending a run, the singular message, dropping the branch or function lines, ignoringlevel, printing covered files, and skipping%,:,,, CR, or LF escaping in the path.Merge Danger
Door: two-way
The report is opt-in by name. Removing it later would drop
"github-actions"from the exportedReportTypeandReportOptionstypes.Blast Radius: additive
Nothing changes for existing reports. Consumers only see the new key in
ReportTypeandReportOptions.Open question
getUncoveredLines()counts a line as covered when any statement on it ran. A line where one statement ran and another didn't (a(); b();on one line, wherea()always throws) gets no annotation unless a branch or function on it was missed, though a 100% statement threshold still fails on it. The alternative is to annotate the start line of every statement with zero hits, which catches those lines but can list lines the text report's "Uncovered Line #s" column leaves out. I went with matching the text report. Which would you prefer?