coverage-v8 reports incorrect coverage when using multiple environments
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
System Info
Need to install the following packages:
[email protected]
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
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
Let's re-check if this issue is still valid
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:
This will be fixed by:
- https://github.com/vitest-dev/vitest/pull/7736
Here's screenshot from collecting coverage for this file three times. 1st with both tests, then one test at a time just to see uncovered branches properly: