With @cloudflare/vitest-plugin, a Durable Object counter test passes alone but fails after another test in its file, and adding reset() causes 'no such table' D1 errors. Why?
answer
- the isolation boundary is the file
- writes persist between tests in a file
- reset() wipes every binding
- the schema goes with the data
- unique names or re-applied migrations
basics
~20 sThe plugin isolates storage per test file, not per test, so a counter named "home" keeps the earlier test's increments. reset() deletes all data from every binding, including the D1 tables migrations created, so later tests need the migrations re-applied.
solid answer
~40 sStorage isolation in `@cloudflare/vitest-plugin` is per test file: each file gets its own storage, and writes made by one test stay visible to the next test in that file until the file ends. So a second test that calls `env.COUNTER.getByName("home").increment()` sees 2, not 1. The per-test `isolatedStorage` option was removed when the integration was rebuilt for Vitest 4. `reset()` from `cloudflare:test` stops the leak by deleting all data from all bound storage, but that includes the tables a setup file created with `applyD1Migrations()`, and the setup file runs only once per file, so the next query fails with `D1_ERROR: no such table`. Either give each test its own keys and object names, or pair `afterEach(reset)` with a `beforeEach` that re-applies the migrations.
code
javascript · 23 linesimport { env, exports } from "cloudflare:workers";
import { applyD1Migrations, reset } from "cloudflare:test";
import { afterEach, beforeEach, expect, it } from "vitest";
beforeEach(async () => {
await applyD1Migrations(env.DB, env.TEST_MIGRATIONS);
});
afterEach(async () => {
await reset();
});
it("counts the first visit", async () => {
const res = await exports.default.fetch("https://example.com/?page=home", { method: "POST" });
expect(await res.json()).toEqual({ page: "home", count: 1 });
});
it("starts from an empty counter and table again", async () => {
const res = await exports.default.fetch("https://example.com/?page=home", { method: "POST" });
expect(await res.json()).toEqual({ page: "home", count: 1 });
const { n } = await env.DB.prepare("SELECT COUNT(*) AS n FROM visits").first();
expect(n).toBe(1);
});go deeper
Recall that the plugin isolates storage per test file, so tests in one file see each other's writes.
Explain what reset() deletes, why the setup file does not run again before each test, and how unique object names avoid the problem.
Diagnose the upgrade from per-test to per-file isolation from its symptoms, choose between unique names, reset plus re-applied migrations and file splits, and check for unawaited storage calls and undisposed RPC results.
Set the suite's convention for test data, unique names or explicit resets, so isolation bugs do not reappear with every new test author or plugin upgrade.
## The isolation model today The Workers Vitest plugin gives every **test file** its own storage environment covering KV, R2, D1, Durable Objects and the other simulated stores. Three consequences follow: - Writes made in one test file are never visible to another test file. - Writes made by one test **are** visible to the following tests in the same file. - At the end of the file, its storage is discarded. This matches Vitest's own per-file isolation. Earlier versions of the integration (published as `@cloudflare/vitest-pool-workers` before its 0.13 release) isolated storage **per test**, undoing each test's writes automatically, and offered `isolatedStorage` and `singleWorker` options to tune it. Both options were removed in the Vitest 4 rework, which is why suites written for the old model start failing in the second test of a file after an upgrade. ## Reading the first symptom The counter test passes on its own because it starts from an empty object. Run after another test in the same file that also incremented the object named "home", it reads 2 instead of 1. Nothing is wrong with the Durable Object; the test assumed a fresh store that the isolation model no longer provides between tests. Running the failing test alone with a filter hides the problem, which is the classic signature of state shared through storage. The same suite often passed before an upgrade. Under the old per-test model, the first test's increment was rolled back before the second test started, so both tests could use the object named "home" and both saw 1. The tests did not change; the isolation boundary moved from the test to the file, and the hidden dependency between the two tests became visible. ## Why reset() takes the schema with it `reset()` from `cloudflare:test` deletes all data from all attached bindings. The sequence that produces the second symptom: 1. The setup file listed in `test.setupFiles` runs once, before the test file, and calls `applyD1Migrations()`, which creates the tables and the `d1_migrations` record. 2. The first test passes and `afterEach(reset)` wipes everything, including every table and `d1_migrations` itself. 3. The second test queries D1 and gets `D1_ERROR: no such table: visits: SQLITE_ERROR`, because nothing re-ran the migrations. The schema is data in the simulated database, so a reset that deletes all data deletes it too. ## Diagnosing it quickly 1. Run the failing test alone with a name filter. If it passes alone and fails in the file, the cause is state left by an earlier test in the same file. 2. Look for shared names: the same `getByName()` argument, KV key, R2 key or row values used in several tests. 3. If D1 reports `no such table`, check whether anything deleted the schema after the setup file ran, usually a `reset()` in `afterEach`. 4. If a test in another file is suspected, remember that files do not share storage unless Vitest runs with `--max-workers=1 --no-isolate`; look inside the failing file first. 5. After an upgrade from the old package, search the config for `isolatedStorage` or `singleWorker`. Their presence means the suite predates per-file isolation and needs the conventions below. ## Fixes, compared | Fix | How | Cost | |---|---|---| | Unique names per test | `getByName("counter-" + crypto.randomUUID())`, keys and rows scoped to the test | Assertions must not count global rows | | Reset and re-apply | `afterEach(reset)` plus `beforeEach(() => applyD1Migrations(env.DB, env.TEST_MIGRATIONS))` | Rebuilds the schema for every test | | One scenario per file | Split tests that need a pristine store into their own files | More files; setup repeated | | Shared storage on purpose | Vitest's `--max-workers=1 --no-isolate` flags | Files share state and run serially | Unique names are usually the cheapest. Because `applyD1Migrations()` skips migrations already recorded, re-applying after a reset rebuilds the schema only when it was actually wiped. ## Related traps - **Unawaited storage calls**: every promise that reads or writes storage should be awaited, or the file's storage cleanup can misbehave. - **Unconsumed bodies**: a response from `fetch()` or `R2.get()` should have its body read, even when only the status is asserted. - **Undisposed RPC results**: a Durable Object or service RPC method returning an object should be received with `using`, so the stub is released. - **Workflow introspectors**: introspectors created in tests must be disposed with `await using` or `dispose()`, or instance state leaks into later tests. - **Durable Object WebSockets**: not supported under per-file isolation; those tests run with the shared-storage flags.
- When would you deliberately make test files share storage?For integration suites whose files depend on shared state, and for Durable Object WebSocket tests, which per-file isolation does not support. Run Vitest with `--max-workers=1 --no-isolate`. The price is order dependence between files and no parallelism, so keep that run separate from the default suite.
- Why is a unique object name per test often better than reset()?It keeps the schema in place, avoids rebuilding it for every test, and makes each test's data self-describing, so a failure points at one object rather than at whatever ran before. It needs assertions scoped to that test's keys or rows, never counts over the whole table.
- Each test gets fresh storage automatically, as it did with isolatedStorage.
- reset() only clears Durable Objects, so D1 tables survive it.
- Files in test.setupFiles run again before every individual test.
- Test files share storage by default, so another file caused the count of 2.
- abortAllDurableObjects() wipes stored counts, so it fixes the leak between tests.
Assumes @cloudflare/vitest-plugin 1.x; storage became isolated per test file, and the isolatedStorage and singleWorker options were removed, in the Vitest 4 rework of the integration.