Skip to content

coverage.thresholds.autoUpdate: pass previous threshold as second argument #10459

Description

@wouterkroes

Clear and concise description of the problem

As a developer using Vitest I want coverage.thresholds.autoUpdate to receive
the previous threshold as a second argument so that I can tell apart V8 variance
from a real regression.

The current callback signature is (newValue: number) => number. Without the
old value there is no way to write logic like "only accept a drop of max X%"

Suggested solution

Pass the current (pre-update) threshold as a second argument to the autoUpdate
callback. The value is already in scope inside updateThresholds as thresholds[key] — it just isn't forwarded to the formatter. The change is a single line and fully backwards-compatible (existing single-arg callbacks continue to work).

// packages/vitest/src/node/coverage.ts — updateThresholds()
// before
const formattedValue = thresholdFormatter(actual)
// after
const formattedValue = thresholdFormatter(actual, threshold ?? 100)

This enables a drift-tolerance pattern:

// vitest.config.ts
coverage: {
  thresholds: {
    autoUpdate: (newValue, oldValue) => {
      if ((oldValue - newValue) > X) {
        throw new Error(`Coverage dropped ${(oldValue - newValue).toFixed(1)}% — intentional?`)
      }
      return Math.max(0, Math.floor(newValue) - X)
    }
  }
}

Alternative

No response

Additional context

In my experience V8's native coverage can produce slightly different numbers between runs, which is why a fixed buffer feels necessary. Is this something others run into too? We would love to hear what buffer values people are using in practice.

I looked at the source and the change appears small enough that we would be happy to open a PR if the maintainers agree with the direction. The old threshold is already in scope when thresholdsToUpdate is built — it just needs to be
carried through to the formatter call, and the autoUpdate type signature updated to (newValue: number, oldValue: number) => number.
Is that the right approach?

Validations

Activity

  1. AriPerkkio commented on May 26, 2026

    @AriPerkkio
    Member

    In my experience V8's native coverage can produce slightly different numbers between runs, which is why a fixed buffer feels necessary.
    [...]
    Is that the right approach?

    We haven't seen reports about this before. Sounds like bug. Fixing it with new feature doesn't sound like the right approach.

    I would recommend to open bug report with minimal reproduction so that the root cause can be fixed properly.

  2. wouterkroes commented on Jun 1, 2026

    @wouterkroes
    Author

    @AriPerkkio thanks for the response. I did some further digging and the root cause for the V8 variance was elsewhere. My apologies for this "false bug report".

    Having the previous threshold as a second argument could still be a nice improvement, but I also understand that this issue is closed for now.

  3. AriPerkkio commented on Jun 1, 2026

    @AriPerkkio
    Member

    Sure, I don't have any strong opinions against the idea of passing the old value as second argument. I would imagine someone could use it to log messages like Updated threshold from x to y.

  4. added
    feat: coverage
    and removed on Jun 1, 2026
  5. wouterkroes commented on Jun 1, 2026

    @wouterkroes
    Author

    I haven't contributed to Vitest before, but I'd be happy to give it a shot and submit a pull request for this feature.
    Would that be a good way to move forward?

  6. AriPerkkio commented on Jun 1, 2026

    @AriPerkkio
    Member

    Sounds good! The fix should be done somewhere around here:

    const thresholdFormatter = typeof this.options.thresholds?.autoUpdate === 'function' ? this.options.thresholds?.autoUpdate : (value: number) => value
    for (const [threshold, newValue] of thresholdsToUpdate) {
    const formattedValue = thresholdFormatter(newValue)

    And a test to verify it could be something similar to this:

    test('formats values with custom formatter', async () => {
    const config = parseModule(`export default ${initialConfig}`)
    const autoUpdate = vi.fn().mockImplementation(value => value + 10_000)
    const updatedConfig = await updateThresholds(config, { thresholds: { autoUpdate } })
    expect(updatedConfig).toMatchInlineSnapshot(`
    "export default {
    "test": {
    "coverage": {
    "thresholds": {
    "lines": 10050,
    "branches": 10060,
    "functions": 10070,
    "statements": 10080
    }
    }
    }
    }"
    `)
    const calls = autoUpdate.mock.calls.flatMap(call => call[0])
    expect(calls.sort()).toEqual([50, 60, 70, 80])
    })

  7. wouterkroes commented on Jun 1, 2026

    @wouterkroes
    Author

    @AriPerkkio #10495 what do you think of the fix?

  8. locked and limited conversation to collaborators on Jul 30, 2026
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions