vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
13.06k stars 1.17k forks source link

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

Open jerrythomas opened 5 months ago

jerrythomas commented 5 months ago

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

AriPerkkio commented 1 month ago

Let's re-check if this issue is still valid

AriPerkkio commented 1 month ago

This is still valid issue. It happens only on V8 provider - istanbul one works correctly. My guess is that this is a merging issue here but didn't yet debug it:

https://github.com/vitest-dev/vitest/blob/11b9432eca226d1c6a5601d4b2d6d70b67c89123/packages/coverage-v8/src/provider.ts#L250

Even a more verbose case like below is incorrect: