Skip to content

feat(lib-report): add github-actions report - #43

Open
sumanthratna wants to merge 10 commits into
vitest-dev:mainfrom
sumanthratna:feat/github-actions-report
Open

sumanthratna wants to merge 10 commits into
vitest-dev:mainfrom
sumanthratna:feat/github-actions-report

Conversation

@sumanthratna

@sumanthratna sumanthratna commented Nov 25, 2026 •

Copy link
Copy Markdown

For vitest-dev/vitest#12061.

Summary

Adds a github-actions report: 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,
onDetail(file)
  lines = getUncoveredLines()                      # the text report's "Uncovered Line #s"
        + getBranchCoverageByLine() lines below 100%
        + start line of each function that never ran
  walk lines with statements, branches or functions in order
    an uncovered line starts or extends a run; a covered line ends it
  for each run [start, end]
    println ::<level> file=<path>,line=<start>,endLine=<end>,title=Uncovered code::Uncovered lines 40-42
  • 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".
  • file: the output file; defaults to the console, like teamcity and text-summary.
  • GitHub shows at most 10 warning and 10 error annotations per step and drops the rest, so a project with many gaps sees the first 10 in report order.
  • Output goes through context.writer like the other reports, using the format and escaping of Vitest's github-actions test reporter. No @actions/core dependency.

In Vitest: coverage.reporter: [["github-actions", { level: "error" }]]. Vitest could also enable it by default when GITHUB_ACTIONS is set, as it does for the github-actions test reporter.

Evidence

  • Before: a coverage gap shows up only in the text table 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.ts and untested lines 12-13 at the end of src/price.ts, which already had uncovered lines 2-3. The run reports:

    src/cart.ts:14         Uncovered line 14
    src/cart.ts:17-24      Uncovered lines 17-24
    src/price.ts:2-3       Uncovered lines 2-3
    src/price.ts:12-13     Uncovered lines 12-13
    

    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: endLine set to the start line, a covered line not ending a run, never extending a run, the singular message, dropping the branch or function lines, ignoring level, 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 exported ReportType and ReportOptions types.

Blast Radius: additive

Nothing changes for existing reports. Consumers only see the new key in ReportType and ReportOptions.

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, where a() 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?

@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/@vitest/istanbul-lib-coverage@20bd3a1
npm i https://pkg.pr.new/@vitest/istanbul-lib-instrument@20bd3a1
npm i https://pkg.pr.new/@vitest/istanbul-lib-report@20bd3a1
npm i https://pkg.pr.new/@vitest/istanbul-lib-source-maps@20bd3a1

commit: 20bd3a1

Comment on lines +50 to +54
// 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)}`,
);

@AriPerkkio AriPerkkio Nov 26, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

Files changed with coverage annotations

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.

Comment on lines +12 to +16
/**
* the annotation level, defaults to `warning`. Use `error` when any
* uncovered line fails the build, as with 100% coverage thresholds.
*/
level?: AnnotationLevel;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants