Skip to content

coverage-v8 reports incorrect coverage when using multiple environments #5783

@jerrythomas

Description

@jerrythomas

Describe the bug

The following code runs differently in different environments (jsdom/windows)

export const runningOn = typeof window === 'undefined' ? 'server' : 'browser'

If I write tests which tests these two paths shouldn't the coverage reported be 100%?

/**
 * @vitest-environment jsdom
 */
describe('Logger on browser', () => {
	it('should identify running_on as "browser"', () => {
		console.log('Running on environment:', runningOn)
		expect(runningOn).toEqual('browser')
	})
})
/**
 * @vitest-environment node
 */
describe('Logger on browser', () => {
	it('should identify running_on as "server"', () => {
		console.log('Running on environment:', runningOn)
		expect(runningOn).toEqual('server')
	})
})

Actual reported coverage is

stdout | src/run_on_server.spec.js > Logger on browser > should identify running_on as "server"
Running on environment: server

stdout | src/run_on_browser.spec.js > Logger on browser > should identify running_on as "browser"
Running on environment: browser

 ✓ src/run_on_server.spec.js (1)
 ✓ src/run_on_browser.spec.js (1)

 Test Files  2 passed (2)
      Tests  2 passed (2)
   Start at  11:32:26
   Duration  1.27s (transform 66ms, setup 0ms, collect 31ms, tests 8ms, environment 717ms, prepare 293ms)

 % Coverage report from v8
--------------|---------|----------|---------|---------|-------------------
File          | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
--------------|---------|----------|---------|---------|-------------------
All files     |     100 |        0 |     100 |     100 |                   
 constants.js |     100 |        0 |     100 |     100 | 1                 
--------------|---------|----------|---------|---------|-------------------

Reproduction

repro-vitest-v8-coverage

System Info

Need to install the following packages:
envinfo@7.13.0
Ok to proceed? (y) y

  System:
    OS: macOS 14.4.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 20.21 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.20.0 - ~/.nvm/versions/node/v18.20.0/bin/node
    npm: 10.5.0 - ~/.nvm/versions/node/v18.20.0/bin/npm
    pnpm: 9.1.3 - ~/Library/pnpm/pnpm
    bun: 1.0.30 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 119.1.60.118
    Chrome: 125.0.6422.78
    Safari: 17.4.1
  npmPackages:
    @vitest/coverage-v8: ^1.4.0 => 1.6.0 
    @vitest/ui: ~1.6.0 => 1.6.0 
    vite: ^5.2.7 => 5.2.11 
    vitest: 1.6.0 => 1.6.0

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    Status

    Has plan

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions