#11433·vitest

Coverage: covered files with `(` or `[` in their path are re-processed as uncovered, and istanbul can record another module's coverage for an uncovered file

Author: VantrongsCreated Oct 1, 2026Updated Oct 1, 2026
Labelspending triage

Describe the bug

Two defects in the uncovered-files pass make our coverage reports nondeterministic. In a SvelteKit app (Browser Mode, @vitest/coverage-istanbul), three runs of the same commit reported 90.08 %, 89.88 % and 89.45 % statements for src/**/*.svelte. Components that tests replace with vi.mock got extra entries: statements past the end of the file and functions renamed <Name>_2. With both fixes below applied as local patches, three runs report 92.25 % with no extra entries.

1. Tested files become unescaped glob patterns. getUntestedFilesByRoot passes them to tinyglobby as ignore patterns:

typescript
ignore: [...this.options.exclude, ...testedFiles.map((file) => slash(file))],

For src/routes/(app)/[id]/page.ts, (app) is a group and [id] a character class, so the pattern doesn't match its own path and the file goes through the uncovered pass again. File-based routing puts many files in such paths: in our app 51 covered route files went through that pass on every run, while only 2 were really uncovered. #4940 was the same problem with user-written exclude patterns; here Vitest generates the patterns itself.

2. istanbul reads the shared lastFileCoverage() after an await. getCoverageMapForUncoveredFiles does

typescript
await transform(`${filename}?cache=${cacheKey}&vitest-uncovered-coverage=true`)
const lastCoverage = this.instrumenter.lastFileCoverage()

The loop is sequential because of the shared state, but other transforms on the same Vite server still run during the await. In Browser Mode the uncovered file is transformed in the client environment, where dev.preTransformRequests is on, so import analysis warms up the file's imports. Cached imports are no-ops, but a module replaced with vi.mock has its transformResult cleared after every test file (ServerMockResolver.invalidate in @vitest/mocker), so it is transformed and instrumented again. If that finishes first, lastFileCoverage() returns a zero-hit map of the mocked module in compiled-JS coordinates. It is stored under the mocked module's path, which after remap and merge gives the extra statements and _2 functions, and the uncovered file's own entry is lost. With coverage.changed, that can be a changed file at 0 % slipping past thresholds.perFile.

A probe in that loop (lastFileCoverage().path !== filename) caught 6 and 8 substitutions out of 53 uncovered files in two runs, and the files with extra entries were exactly the substituted ones.

Suggested fixes, both checked against 5.0.3 with the reproduction below:

  1. testedFiles.map(file => escapePath(slash(file))) with escapePath from tinyglobby, or filter the glob result with a Set of tested files instead of turning exact paths into patterns.
  2. Keep the coverage from the uncovered file's own transform: in onFileTransform, for ids with vitest-uncovered-coverage=true, store this.instrumenter.lastFileCoverage() in a Map keyed by id right after instrumentSync, and read it from there in the loop.

Fix 1 alone hid the symptom in our app, because neither of the 2 really uncovered files imports a mocked module; fix 2 removes the race. I can open a PR for both.

I investigated this with Claude Code as an assistant.

Reproduction

https://github.com/Vantrongs/vitest-coverage-uncovered-repro

  • npm run repro:glob (Node, no browser): a test covers src/routes/(app)/[id]/page.ts, yet DEBUG=vitest:coverage prints Uncovered file 0/1 … src/routes/(app)/[id]/page.ts. Expected: no uncovered files.
  • npm run repro:race: drives IstanbulCoverageProvider.generateCoverage against a Vite server whose plugin transforms src/other.ts while the uncovered src/uncovered.ts is pending, as import-analysis warm-up does. Expected […/src/uncovered.ts], got […/src/other.ts].

In a real project the race needs Browser Mode, the istanbul provider and an uncovered file (or, through defect 1, a covered file under (group)/[param]/) that imports a module some browser test mocks with vi.mock(path, factory). It is timing-dependent.

The app uses bun; the reproduction uses npm and behaves the same after bun install.

System Info

bash
System:
    OS: Linux 7.2 NixOS 26.05 (Yarara)
    CPU: (32) x64 AMD Ryzen 9 9950X 16-Core Processor
    Shell: 5.3.9 - bash
  Binaries:
    Node: 24.21.0
    npm: 11.19.0
  Browsers:
    Chrome: 154.0.8037.92
    Firefox: 157.0
  npmPackages:
    @vitest/coverage-istanbul: 5.0.3 => 5.0.3
    vite: 8.3.2 => 8.3.2
    vitest: 5.0.3 => 5.0.3

Used Package Manager

npm

Validations