davelosert/vitest-coverage-report-action

Can code coverage threshold check be based on changed files in PRs ?

Closed this issue · 2 comments

Hi,

I have introduced vitest and this github action in a untested codebase. I would like to define some code coverage thresholds and have them being enforced only on changed files whenever a new PR is opened.

This is my config :

import react from "@vitejs/plugin-react";
import tsconfigPaths from "vite-tsconfig-paths";
import { defineConfig } from "vitest/config";

export default defineConfig({
    plugins: [react(), tsconfigPaths()],
    test: {
        coverage: {
            reporter: ["text-summary", "json-summary", "json"],
            reportOnFailure: true,
            thresholds: {
                lines: 40,
                branches: 40,
                functions: 40,
                statements: 40,
            },
        },
        environment: "jsdom",
    },
});

But when the github workflow runs vitest run --coverage.enabled true --coverage.file-coverage-mode changes in a PR, it fails with this result

ERROR: Coverage for lines (4.17%) does not meet global threshold (40%)
ERROR: Coverage for functions (0.64%) does not meet global threshold (40%)
ERROR: Coverage for statements (4.17%) does not meet global threshold (40%)
ERROR: Coverage for branches (0.96%) does not meet global threshold (40%)

So, would there be a way to fail only if the changed files don't meet the coverage thresholds ?

Hi @kermorgant and thank you for the issue,

as this action currently only works on the finalized report given from vitest, and the actual coverage is calculated there, this is more of an issue with vitest / the vitest command you execute itself.

The simplest solution maybe would be to use vitest --coverage.all=false, as that will only report coverage for files that actually also have a test.

An potential alternative could be using the --coverage.include option and combine it / fill it with the results of GitHubs API to list all changed files of a pull-request e.g. by using something like the changed-files action](https://github.com/marketplace/actions/changed-files.

But as written, that would have to happen in an action-step executed before this action.

I am planning to someday in the future also execute the tests itself with this action, but I am currently very limited with my time so that won't happen anytime soon.

Hope that still helps!

Closing this issue as there was no further feedback, thus I am assuming this is resolved.