Test Durable Object eviction with new cloudflare:test helpers
The @cloudflare/vitest-pool-workers package now includes evictDurableObject and evictAllDurableObjects test helpers, exported from cloudflare:test.
These helpers let you test how a Durable Object behaves across evictions, simulating the production lifecycle where an idle Durable Object can be evicted from memory.
For more context, refer to Lifecycle of a Durable Object.
import { evictDurableObject, evictAllDurableObjects } from "cloudflare:test";import { env } from "cloudflare:workers";
const id = env.COUNTER.idFromName("my-counter");const stub = env.COUNTER.get(id);
// Evict the Durable Object instance pointed to by a specific stubawait evictDurableObject(stub);
// Close WebSockets instead of hibernating themawait evictDurableObject(stub, { webSockets: "close" });
// Evict all currently-running Durable Objects in evictable namespacesawait evictAllDurableObjects();These helpers are available in @cloudflare/vitest-pool-workers@0.16.20 and later.
Learn more in the Test APIs reference and the Testing Durable Objects guide.