vitest-dev / vitest-dev/vitest

coverage-v8: Duplicate statements when merging coverage from multiple projects with different environments

Open
#9,366 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feat: coverage upstream
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

Describe the bug

When using Vitest projects with different environments (e.g., jsdom for unit tests + browser for component tests), files covered by multiple projects have duplicate coverage entries, resulting in inflated statement/function/branch counts.

Reproduction

https://github.com/stevez/vitest-coverage-merge-bug

git clone https://github.com/stevez/vitest-coverage-merge-bug
cd vitest-coverage-merge-bug
npm install
npm run test:coverage
node check-coverage.cjs

Output shows duplicate statements for constants.ts:

File: constants.ts
  Statements: 6
    [0] line 1:22 -> 7:null      <- from jsdom
    [1] line 9:21 -> 9:null      <- from jsdom
    [2] line 11:22 -> 15:null    <- from jsdom
    [3] line 1:22 -> 7:10        <- from browser (duplicate!)
    [4] line 9:21 -> 9:58        <- from browser (duplicate!)
    [5] line 11:22 -> 15:10      <- from browser (duplicate!)

Expected: 3 statements
Actual: 6 statements

Root Cause

Vite's SSR transform (used for jsdom) produces source maps with end.column: null, while browser transform produces specific end column values. Istanbul's merge() treats these as different statements because the locations don't match exactly.

System Info

System:
  OS: Windows 11
  CPU: Intel(R) Core(TM) Ultra 9 285K
  Memory: 64 GB
  Node: v22.16.0
Binaries:
  npm: 10.9.2
npmPackages:
  vitest: ^4.0.16
  @vitest/coverage-v8: ^4.0.16
  @vitest/browser: ^4.0.16

Used Package Manager

npm

Validations

  • Check that you are using the latest version of Vitest
  • Read the contribution guide
  • Check existing issues for duplicates

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the linked reproduction with the listed npm commands and inspect check-coverage.cjs to confirm the duplicate locations. Then trace the coverage-v8 merge path and Istanbul's merge handling for the jsdom and browser source-map locations. Done means constants.ts reports three statements rather than six while preserving coverage from both environments.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, vite
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.