Skip to content

Coverage reports missing when using @vitest/coverage-v8/browser with in-source testing #7917

@egfx-notifications

Description

@egfx-notifications

Describe the bug

Source files which include their tests with in-source testing will be dropped from coverage reported by @vitest/coverage-v8/browser

I tracked this down to

if (coverage.url.includes('?browserv=') || coverage.url.includes('&browserv=')) {
return false
}

Since the source files containing the in-source tests are loaded with the prefix &browserv= in the URL they are omitted from coverage reporting. So to fix this the filter should be adjusted to not filter out those files.

Reproduction

@vitest/coverage-v8 doesn't work on StackBlitz, so I made a repo based on the in-source testing example:
https://github.com/egfx-notifications/vitest-issue-reproduction

Clone the repository, then run

npm install
npm run test

This shows that the in-source test passes with zero code coverage.

You can run npm run test:negative to demonstrate that code coverage works fine with a separate test file.

System Info

System:
    OS: Windows 11 10.0.26100
    CPU: (22) x64 Intel(R) Core(TM) Ultra 7 165H
    Memory: 40.73 GB / 63.45 GB
  Binaries:
    Node: 22.14.0 - ~\scoop\apps\nvm\current\nodejs\nodejs\node.EXE
    npm: 10.9.2 - ~\scoop\apps\nvm\current\nodejs\nodejs\npm.CMD
    pnpm: 10.9.0 - ~\scoop\apps\nvm\current\nodejs\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (133.0.3065.69)
  npmPackages:
    @vitest/browser: ^3.1.2 => 3.1.2
    @vitest/coverage-v8: ^3.1.2 => 3.1.2
    playwright: ^1.52.0 => 1.52.0
    vitest: latest => 3.1.2

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: browserIssues and PRs related to the browser runnerfeat: coverageIssues and PRs related to the coverage featurep3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions