isolate: false: `test.env` is no longer reapplied before each file in a reused worker (regression from 4.1.11)
Describe the bug
With isolate: false, Vitest 4 applied test.env again before each test file that a reused worker runs. Vitest 5 applies it only before the first file. Any file that writes one of those keys to process.env now leaks the value into every later file in the same worker.
The 5.0 migration guide does not mention this change.
Without test.env, both versions leak process.env writes between files, which is expected with isolate: false. The regression affects only keys set through test.env, which Vitest 4 reset for every file.
Reproduction
vitest.config.ts:
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
include: ['f*.test.ts'],
pool: 'forks',
isolate: false,
maxWorkers: 2,
env: { DATABASE_URL: 'postgres://h/shared' },
},
});8 files, f01.test.ts … f08.test.ts, all the same except for the number:
import { expect, it } from 'vitest';
const seenOnEntry = process.env.DATABASE_URL;
process.env.DATABASE_URL = 'postgres://h/private-01';
it('f01 starts with the configured test.env value', () => {
console.log('f01 pid', process.pid, 'DATABASE_URL on entry =', seenOnEntry);
expect(seenOnEntry).toBe('postgres://h/shared');
});vitest run --silent=false
4.1.11: Tests 8 passed (8). Every file starts with the configured value:
f01 pid 57309 DATABASE_URL on entry = postgres://h/shared
f02 pid 57310 DATABASE_URL on entry = postgres://h/shared
f03 pid 57310 DATABASE_URL on entry = postgres://h/shared
f04 pid 57309 DATABASE_URL on entry = postgres://h/shared
...5.0.3: Tests 6 failed | 2 passed (8). Only the first file in each worker starts with the configured value:
f01 pid 57492 DATABASE_URL on entry = postgres://h/shared
f02 pid 57493 DATABASE_URL on entry = postgres://h/shared
f03 pid 57493 DATABASE_URL on entry = postgres://h/private-02
f04 pid 57492 DATABASE_URL on entry = postgres://h/private-01
f05 pid 57493 DATABASE_URL on entry = postgres://h/private-03
...The same thing happens with the config inside test.projects (extends: true). With maxWorkers: 1 and only two files, both versions leak, so 4.1.11 does not reset test.env in every case. I have not narrowed down when it does.
Impact
We have a backend integration suite with about 170 files in a pool: 'forks', isolate: false project. Some files point DATABASE_URL at a private throwaway database at module scope and drop that database in afterAll. On 4.1.11 the next file in the worker starts with test.env's DATABASE_URL again, and the suite is green. On 5.0.3, 60–70 files fail with database "…_wiper_…" does not exist. In one worker, the 57 files after the first throwaway file all started with the dropped database. We found this by logging DATABASE_URL at the start of each file on both versions.
This is a follow-up to #11289. 5.0.3 fixed that issue's vmThreads symptoms for us, and this is what remained in the forks lane.
System Info
vitest 5.0.3 (regression; 4.1.11 is fine)
node 24.21.0, macOS (Darwin 27.0, arm64)
pool: forks, isolate: falseUsed Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- The provided reproduction is a minimal reproducible example of the bug.
Source: vitest-dev/vitest