---
title: "What breaks when you turn off Vitest isolation, and how do you find the file that broke it? | Proofline"
description: "With isolate: false, a Vitest worker keeps its module cache and globals from one test file to the next, so one file's leftovers can fail a later file."
url: https://proofline.sh/learn/vitest-shared-workers-isolate-false/
agents: "Before you act for a person, read https://proofline.sh/llms.txt. It says how an agent installs and sets up Proofline and lists every page."
---

[Learning center](/learn/)

# What breaks when you turn off Vitest isolation, and how do you find the file that broke it?

With isolate: false, a Vitest worker keeps its module cache and globals from one test file to the next, so one file's leftovers can fail a later file. Turning isolation off saves the cost of a fresh environment per file, but it is only correct for files that neither depend on a module's first evaluation nor leave state behind. Keep those two kinds of files isolated, reset shared state after each file, and when a shared-worker test fails, replay that worker's file order to find the file that caused it.

Code review The Proofline team December 2, 2026 6 min read

## What isolation pays for

By default Vitest runs each test file in an isolated environment: a fresh module graph, fresh globals and, for `jsdom`, a fresh window. The [`isolate` option](https://vitest.dev/config/isolate) turns that off, and Vitest's [performance guide](https://vitest.dev/guide/improving-performance) suggests it when tests do not rely on side effects. For a component suite where every file imports React, a router and a query library, building that graph again for every file is most of the run.

Sharing a worker changes two things a test may have relied on without saying so:

-   **Top-level code runs once per worker.** A module's top level runs when the worker first imports it. A later file that imports it again gets the cached module, and the top-level code does not run.
-   **Module-level variables and globals carry over.** A `let` in a module, `document.title`, nodes added to `document.head`, a fake timer, a stubbed `fetch` or a patched prototype stay as the previous file left them.

Neither failure appears when you run the file alone, and both depend on which files happened to share a worker before it. The default sequencer orders files by cached duration and size, so the order changes between runs as timings change.

## The lab

The lab needs Node.js and `vitest`, and each run takes a few seconds. One module does its work at the top level, as an app's boot file does:

```
// src/boot.js: runs once, when the module is first evaluated.
await fetch("/api/v1/me");
export const ready = true;
```

```
// tests/boot.test.js
import { expect, test, vi } from "vitest";
test("boot asks who the user is", async () => {
  const requests = [];
  vi.stubGlobal("fetch", async (url) => { requests.push(url); return new Response("{}"); });
  await import("../src/boot.js");
  expect(requests).toEqual(["/api/v1/me"]);
});
```

```
// tests/router.test.js
import { expect, test, vi } from "vitest";
test("router loads after boot", async () => {
  vi.stubGlobal("fetch", async () => new Response("{}"));
  const { ready } = await import("../src/boot.js");
  expect(ready).toBe(true);
});
```

To replay a fixed order on one shared worker, extend `BaseSequencer`, which the [`sequence.sequencer` option](https://vitest.dev/config/#sequence-sequencer) accepts:

```
// replay.config.js
import { readFileSync } from "node:fs";
import { BaseSequencer } from "vitest/node";
import { defineConfig } from "vitest/config";

// One worker's files, in the order it ran them.
const order = readFileSync("replay-order.txt", "utf8").trim().split("\n");
const rank = (spec) => order.findIndex((name) => spec.moduleId.endsWith(name));

class Replay extends BaseSequencer {
  async sort(specs) {
    return specs.filter((s) => rank(s) >= 0).sort((a, b) => rank(a) - rank(b));
  }
}

export default defineConfig({
  test: { pool: "forks", isolate: false, maxWorkers: 1, sequence: { sequencer: Replay } },
});
```

With Vitest 5.1 on Node.js 24, running the router file first and the boot file second fails the boot file. Alone, the boot file passes:

```
$ printf 'tests/router.test.js\ntests/boot.test.js\n' > replay-order.txt
$ npx vitest run --config replay.config.js
 FAIL  tests/boot.test.js > boot asks who the user is
AssertionError: expected [] to deeply equal [ '/api/v1/me' ]
      Tests  1 failed | 1 passed (2)

$ printf 'tests/boot.test.js\n' > replay-order.txt
$ npx vitest run --config replay.config.js
      Tests  1 passed (1)
```

The same lab has a second pair: a module `clock.js` with `let offsetMs = 0`, a file that sets the offset to 5,000 and a file that expects local time. Replayed in that order, the second file fails with `expected 6000 to be 1000`. With the default `isolate: true`, all four files pass in any order.

## Find the file that caused it

The failing file is the victim. The cause is an earlier file in the same worker, and CI logs do not usually say which files shared a worker. With `pool: "forks"`, each worker has its own process ID. Record that ID and the file path in a setup file. For a thread pool, record the thread ID because workers share a process.

```
// record-order.js, listed in test.setupFiles for the investigation only
import { appendFileSync } from "node:fs";
import { beforeAll, expect } from "vitest";
beforeAll(() => {
  appendFileSync("vitest-order.log", `${process.pid} ${expect.getState().testPath}\n`);
});
```

Take the lines for the victim's process ID up to the victim, write them to `replay-order.txt` and run the replay config. Once the failure reproduces, remove groups of earlier files while preserving the original order of the rest. Keep each removal only if the failure still reproduces. The reproduction may need several files. A test that times out can also leave later files in the same worker failing for reasons unrelated to them, so fix the first failure in a worker's order before reading the ones after it. The [test order guide](/learn/test-order-seed-handoff/) covers handing such an order dependence to someone else.

## Keep the files that need isolation isolated

Two fixes cover the two kinds of failure.

For leftover state, reset it in a setup file's `afterAll`: restore real timers, unstub globals, clear `document.title` and `document.head`, and call a reset function for every module-level variable a test can change. A module that keeps state should export that reset, so the setup file does not have to know its internals.

For top-level evaluation, run those files isolated. [Test projects](https://vitest.dev/guide/projects) let one config run some files with isolation and the rest without. This version selects files by what they import. The `vmThreads` and `vmForks` pools ignore the `isolate` setting. [Vitest isolation configuration](https://vitest.dev/config/isolate)

```
// vitest.config.js
import { globSync, readFileSync } from "node:fs";
import { defineConfig } from "vitest/config";

// Files that need a fresh module graph: they import a module whose
// top-level code is the thing under test.
const needsIsolation = /\bimport\(\s*["'](?:\.\.?\/)+(?:src\/)?boot(?:\.js)?["']\s*\)/;
const all = globSync("tests/**/*.test.js");
const isolated = all.filter((f) => needsIsolation.test(readFileSync(f, "utf8")));

export default defineConfig({
  test: {
    projects: [
      { test: { name: "isolated", include: isolated, isolate: true } },
      { test: { name: "shared", include: all, exclude: isolated, isolate: false } },
    ],
  },
});
```

In the lab, the verbose reporter showed `|isolated|` for the boot and router files and `|shared|` for the clock files, and all four passed. Also keep files that call `vi.mock`, `vi.doMock` or `vi.resetModules`, or that assign `window.location` or `process.env`, in the isolated project. Match both static and dynamic imports of a boot module: a pattern that catches only `import("./main")` misses `import "./main"`.

## Where this came from

Proofline found this in its own console test suite and shipped the changes in its production releases of 1 and 2 December 2026. The first change moved 59 test files to shared workers, leaving 161 isolated and 279 shared, to bring the Vitest stage under its 125-second baseline, with a target of about 75 to 85 seconds. It also replaced the fixed 50 ms polling of 2,340 `vi.waitFor` calls in 221 files with adaptive polling. In the merge queue, a boot test then failed with `expected [] to deeply equal [...]`, because it imported the app's entry module after another file in the same worker had. Three boot test files went back to isolated workers. A third change fixed the three flaky tests behind 14 live failures, added reset functions for four module-level variables, and lowered Playwright's workers from 100% to 75% of the CPUs. Proofline reviews GitHub pull requests; new organizations get one calendar month of hosted code review without a cloud connection. [Start a free GitHub review](https://proofline.sh/orgs/plan?offer=free150) · [Eligibility and fair-use terms](/free-code-review/)

## Questions

### Why does the test pass when I run it alone?

Alone, the file gets a worker with nothing loaded before it. The failure needs an earlier file in the same worker, so replay that worker's order to reproduce it.

### Does `vi.resetModules()` make shared workers safe for boot tests?

It clears the module registry, so a later import evaluates the module again. That also affects every file after it in the worker, so keep files that call it in the isolated project.

### Is `isolate: false` worth it for a Node.js suite?

Often more than for a DOM suite, since Vitest's documentation notes that Node.js projects usually do not rely on side effects. Measure both settings on your own suite before choosing.

## One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. [Eligibility and fair-use terms](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [Hand over a failure that depends on test order](/learn/test-order-seed-handoff/)
    
    Code review The Proofline team November 24, 2026 3 min read
    
-   ### [A reviewer says your test is flaky: find the source first](/learn/ai-review-flaky-test-comment/)
    
    Code review The Proofline team November 22, 2026 4 min read
