`environment` duration is re-reported for every batch when a non-isolated worker reuses its environment
Describe the bug
With isolate: false and maxWorkers: 1, if files with different environments are interleaved (for example a node file sorts before the jsdom files), groupSpecs puts each file in its own batch. The worker sets up the jsdom environment once and reuses it for every later batch, which is fine. But every batch reports that one setup's duration again as its own environmentSetupDuration. So the environment figure in the summary grows with the number of batches, even though the work was done once.
The cause, in packages/vitest/src/runtime/workers/base.ts (dist/chunks/base.*.js in 5.0.5):
// setupBaseEnvironment: runs once per worker
_environmentTime = performance.now() - startTime
// runBaseTests: runs for every batch
state.durations.environment = _environmentTime
onCollected sets state.durations.environment = 0 after the first file, but that only works inside one batch. The next batch reassigns the cached value.
In a real suite (262 files, 2-vCPU CI runner, one worker) the summary showed environment 82s when a node file sorted first, and about 1s when the jsdom files came first. That looks like 80 s of environment setup is being wasted, and it sent us chasing a cost that didn't exist. Counting new JSDOM() calls showed one construction in both orders.
Expected: a reused environment reports 0 for the batches that reused it. One possible fix is to report _environmentTime only on the first runBaseTests call after setupBaseEnvironment, then clear it.
Reproduction
Six files plus a config and a small reporter that prints each file's environmentSetupDuration.
vitest.config.ts
import { defineConfig } from 'vitest/config'
import { BaseSequencer } from 'vitest/node'
// Alphabetical order, so the node file runs first (as on CI with no run cache).
class Alphabetical extends BaseSequencer {
async sort(files) {
return [...files].sort((a, b) => a.moduleId.localeCompare(b.moduleId))
}
}
export default defineConfig({
test: {
isolate: false,
maxWorkers: 1,
environment: 'jsdom',
sequence: { sequencer: Alphabetical },
reporters: ['default', './env-reporter.ts'],
},
})
env-reporter.ts
import type { Reporter, TestModule } from 'vitest/node'
export default class EnvReporter implements Reporter {
onTestRunEnd(modules: ReadonlyArray<TestModule>) {
for (const m of modules)
console.log(m.moduleId.split('/').pop(), `${Math.round(m.diagnostic().environmentSetupDuration)}ms`)
}
}
test/a-node.test.ts
// @vitest-environment node
import { test } from 'vitest'
test('node', () => {})
test/b-dom.test.ts (copy it to c-, d-, e-, f-dom.test.ts)
import { test } from 'vitest'
test('dom', () => {
// Every jsdom file sees the same window object, so the counter keeps climbing.
const w = window as any
w.__files = (w.__files ?? 0) + 1
console.log(`window seen by ${w.__files} file(s)`)
})
pnpm add -D [email protected] jsdom@27 && pnpm vitest run gives:
window seen by 1 file(s)
window seen by 2 file(s)
...
window seen by 5 file(s)
a-node.test.ts 0ms
b-dom.test.ts 1148ms
c-dom.test.ts 1148ms
d-dom.test.ts 1148ms
e-dom.test.ts 1148ms
f-dom.test.ts 1148ms
The window is shared, so the environment was built once, yet five files each report the same 1148 ms. Rename a-node.test.ts to z-node.test.ts so the jsdom files share one batch, and only the first jsdom file reports a duration (the rest report 0ms).
At a larger scale (1 node file + 23 jsdom files), the summed environment is 20.0 s with the node file first and 1.1 s with it last. I patched jsdom's constructor in a scratch install to count new JSDOM() calls, and there was exactly 1 call in both orders. 4.1.11 does the same (23.4 s vs 1.3 s).
System Info
System:
OS: macOS 26.6
CPU: (10) arm64 Apple M5
Memory: 32.00 GB
Binaries:
Node: 24.22.1
npm: 11.19.1
pnpm: 10.28.0
npmPackages:
vitest: 5.0.5 => 5.0.5
jsdom: 27.4.0
Also seen on vitest 4.1.11 on GitHub Actions ubuntu-latest (2 vCPU, so the default maxWorkers is 1).
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.
Disclosure: this issue was written by Claude (Anthropic's AI assistant, via Claude Code) on behalf of @HipHoff, who reviewed it and takes responsibility for it.
I looked into this issue and would like to work on a fix.
The environment setup duration appears to be reused across batches even when the worker hasn't initialized a new environment.
I'd like to add a regression test with interleaved environments and isolate: false, then ensure the setup duration is reported only for the batch that actually performed the initialization.
Is this an approach you'd be open to? If so, could the issue be approved for a PR?