docs/guide/recipes/disable-isolation.md
By default, every test file runs in its own isolated module graph, which protects against one file leaking state into another. That isolation costs setup time on every file, which is fine for integration tests that genuinely need it and wasted on pure unit tests that don't share mutable state.
Use projects to apply isolate: false to the unit suite while keeping the integration suite isolated.
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
projects: [
{
test: {
// Non-isolated unit tests
name: 'Unit tests',
isolate: false,
exclude: ['**.integration.test.ts'],
},
},
{
test: {
// Isolated integration tests
name: 'Integration tests',
include: ['**.integration.test.ts'],
},
},
],
},
})
A test file is safe to deisolate when it does not:
let bindings)vi.stubGlobal or vi.stubEnvDate.prototype, Array.prototype, …)process or other long-lived emittersvi.mock factoriesIf any of those apply, isolation is doing real work and should stay on.
Run the suite twice with shuffling to surface inter-file pollution:
vitest --shuffle --run --project='Unit tests'
vitest --shuffle --run --project='Unit tests'
If the second run produces different results, you have order-dependent tests. Either fix the offender or leave isolation enabled for that file.
isolate only governs the threads and forks pools. The vmThreads and vmForks pools always run isolated regardless of the flag, since they trade startup cost for stronger guarantees.