Skip to content

feat(reporters): annotate uncovered code in the github-actions reporter #12061

Open

Description

@sumanthratna

Clear and concise description of the problem

The github-actions reporter annotates failed tests, but coverage gets no annotations. When coverage.thresholds fails a CI job, the check page shows only "Process completed with exit code 1". To find the uncovered file and lines, you have to open the full job log and scroll the text coverage table.

In our monorepo, coverage thresholds were the most common reason a pull request's CI failed: 8 of 23 failed jobs in a sample of recent red PRs. Every test failure in that sample already had an annotation; none of the coverage failures did.

Suggested solution

Let the github-actions reporter emit one ::error workflow command per file that has uncovered statements, functions, or branches. Anchor each annotation at the file's first uncovered line and list the rest in the message:

::error file=src/render.ts,line=40,title=Uncovered code::Uncovered lines 40-42, 55

A single annotation per file matters because GitHub shows at most 10 error annotations per step and 50 per job.

The API could be an opt-in option on the reporter:

reporters: [["github-actions", { coverage: true }]]

Another option is to annotate only when thresholds fail, which would require running after the threshold check (see below).

Alternative

We use a custom reporter that implements onCoverage(coverageMap). For each file it walks s/f/b from fileCoverageFor(path).data, collects the start line of every entry with zero hits, and posts one annotation per file with core.error from @actions/core, which formats and escapes the workflow command.

This approach has three rough edges, and all would go away upstream:

  1. onCoverage runs before @vitest/coverage-v8 checks thresholds, so the reporter cannot tell whether the gate failed. It is only correct when every threshold is 100%, where any uncovered line fails the gate.
  2. @actions/core 3 is ESM-only, so the config file that defines the reporter must load as ESM (we renamed ours to .mts).
  3. Adding the reporter means setting reporters, and a non-empty list replaces the defaults. So you have to list default and github-actions again by hand.
Reporter (about 30 lines)
import path from "node:path";
import { error } from "@actions/core";
import type { Reporter } from "vitest/node";

class CoverageAnnotations implements Reporter {
  onCoverage(coverage: unknown) {
    const map = coverage as {
      files(): string[];
      fileCoverageFor(f: string): { data: any };
    };
    for (const file of map.files()) {
      const d = map.fileCoverageFor(file).data;
      const lines = new Set<number>();
      for (const [id, n] of Object.entries<number>(d.s)) if (n === 0) lines.add(d.statementMap[id].start.line);
      for (const [id, n] of Object.entries<number>(d.f)) if (n === 0) lines.add(d.fnMap[id].loc.start.line);
      for (const [id, hits] of Object.entries<number[]>(d.b))
        hits.forEach((n, i) => n === 0 && lines.add(d.branchMap[id].locations[i].start.line));
      if (lines.size === 0) continue;
      const sorted = [...lines].sort((a, b) => a - b);
      error(`Uncovered lines ${sorted.join(", ")}`, {
        file: path.relative(process.cwd(), file),
        startLine: sorted[0],
        title: "Uncovered code",
      });
    }
  }
}

Additional context

  • Seen on Vitest 4.1.12. The 5.1.1 github-actions reporter has no coverage hook either: it implements onInit, onTestCaseAnnotate, and onTestRunEnd, and its options are onWritePath, displayAnnotations, and jobSummary.
  • I searched existing issues and discussions and found no request for coverage annotations.
  • Happy to send a PR if the API shape is agreed.

Validations

Activity

  1. AriPerkkio commented on Nov 23, 2026

    @AriPerkkio
    Member

    Could this be custom coverage reporter instead of test reporter, as it's reporting coverage results?

    So something like this:

    import { defineConfig } from 'vitest/config'
    
    export default defineConfig({
      test: {
        coverage: {
          reporter: [
            'html',
            'json',
            'lcov',
    +       'github-actions',
          ],
        },
      },
    })
  2. sumanthratna commented on Nov 23, 2026

    @sumanthratna
    Author

    Agreed, a coverage reporter fits better. I moved our reporter to one and it works in CI. It also leaves test.reporters alone, so users no longer re-list default and github-actions.

    The config adds the reporter by path on GitHub Actions:

    reporter: [
      "text",
      "lcov",
      ...(process.env.GITHUB_ACTIONS === "true" ? [`${import.meta.dirname}/vitest.coverage-annotations.mts`] : []),
    ],

    And the reporter, excerpted (uncoveredLines collects the start line of every zero-hit entry in s/f/b; annotateCoverage groups by file and calls core.error from @actions/core once per file):

    class CoverageAnnotations {
      execute(context: ReportContext): void {
        const gaps: { path: string; line: number }[] = [];
        context.getTree().visit({
          onDetail: (node) => {
            const { path, data } = node.getFileCoverage();
            for (const line of uncoveredLines(data)) gaps.push({ path, line });
          },
        });
        annotateCoverage(gaps);
      }
    }
    
    // istanbul-reports constructs the value `require` returns, which for an ES module is this export.
    export { CoverageAnnotations as "module.exports" };

    Happy to send a PR for a built-in github-actions coverage reporter if that shape works for you.

  3. AriPerkkio commented on Nov 25, 2026

    @AriPerkkio
    Member

    Feel free to send PR at https://github.com/vitest-dev/istanbuljs for this. I'm interested to see what the actual output would look like on CI. 👀

  4. added
    feat: coverage
    on Nov 25, 2026
  5. sumanthratna commented on Nov 25, 2026

    @sumanthratna
    Author

    Opened vitest-dev/istanbuljs#43 with a github-actions report.

    CI output from a demo run on my fork, with that repo's own coverage going through the report: https://github.com/sumanthratna/istanbuljs/actions/runs/45694687030. It prints one warning per file, anchored at the first uncovered line, such as Uncovered lines 20-21 on coverage-map.ts:20. A level option switches to error for projects where any uncovered line fails the build.

    There's one open question in the PR body, about lines where only some of the statements ran.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: coverage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions