Durable Objects, Workers - Test Durable Object eviction with new cloudflare:test helpers
- Announced
- Tags
- Durable-Objects, Workers
- Severity
- info, set by
reef/rss-rule-v1 - Source
- https://developers.cloudflare.com/changelog/post/2026-06-25-durable-object-eviction-test-helpers/
<p>The <code>@cloudflare/vitest-pool-workers</code> package now includes <code>evictDurableObject</code> and <code>evictAllDurableObjects</code> test helpers, exported from <code>cloudflare:test</code>.</p> <p>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.</p> <p>For more context, refer to <a href="https://developers.cloudflare.com/durable-objects/concepts/durable-object-lifecycle/">Lifecycle of a Durable Object</a>.</p> <figure class="nb-code-figure" data-nb-lang="ts"><pre class="astro-code astro-code-themes github-light github-dark nb-shiki-c6xiwz" tabindex="0" data-language="ts" data-nb-lang="ts"><code><span class="line"><span class="nb-shiki-1itgoe">import</span><span class="nb-shiki-140thh"> { evictDurableObject, evictAllDurableObjects } </span><span class="nb-shiki-1itgoe">from</span><span class="nb-shiki-mdbnqw"> "cloudflare:test"</span><span class="nb-shiki-140thh">;</span></span> <span class="line"><span class="nb-shiki-1itgoe">import</span><span class="nb-shiki-140thh"> { env } </span><span class="nb-shiki-1itgoe">from</span><span class="nb-shiki-mdbnqw"> "cloudflare:workers"</span><span class="nb-shiki-140thh">;</span></span> <span class="line"></span> <span class="line"><span class="nb-shiki-1itgoe">const</span><span class="nb-shiki-dzsirb"> id</span><span class="nb-shiki-1itgoe"> =</span><span class="nb-shiki-140thh"> env.</span><span class="nb-shiki-dzsirb">COUNTER</span><span class="nb-shiki-140thh">.</span><span class="nb-shiki-1t8gfj">idFromName</span><span class="nb-shiki-140thh">(</span><span class="nb-shiki-mdbnqw">"my-counter"</span><span class="nb-shiki-140thh">);</span></span> <span class="line"><span class="nb-shiki-1itgoe">const</span><span class="nb-shiki-dzsirb"> stub</span><span class="nb-shiki-1itgoe"> =</span><span class="nb-shiki-140thh"> env.</span><span class="nb-shiki-dzsirb">COUNTER</span><span class="nb-shiki-140thh">.</span><span class="nb-shiki-1t8gfj">get</span><span class="nb-shiki-140thh">(id);</span></span> <span class="line"></span> <span class="line"><span class="nb-shiki-21nrsd">// Evict the Durable Object instance pointed to by a specific stub</span></span> <span class="line"><span class="nb-shiki-1itgoe">await</span><span class="nb-shiki-1t8gfj"> evictDurableObject</span><span class="nb-shiki-140thh">(stub);</span></span> <span class="line"></span> <span class="line"><span class="nb-shiki-21nrsd">// Close WebSockets instead of hibernating them</span></span> <span class="line"><span class="nb-shiki-1itgoe">await</span><span class="nb-shiki-1t8gfj"> evictDurableObject</span><span class="nb-shiki-140thh">(stub, { webSockets: </span><span class="nb-shiki-mdbnqw">"close"</span><span class="nb-shiki-140thh"> });</span></span> <span class="line"></span> <span class="line"><span class="nb-shiki-21nrsd">// Evict all currently-running Durable Objects in evictable namespaces</span></span> <span class="line"><span class="nb-shiki-1itgoe">await</span><span class="nb-shiki-1t8gfj"> evictAllDurableObjects</span><span class="nb-shiki-140thh">();</span></span></code></pre></figure> <p>These helpers are available in <code>@cloudflare/vitest-pool-workers@0.16.20</code> and later.</p> <p>Learn more in the <a href="https://developers.cloudflare.com/workers/testing/vitest-integration/test-apis/#durable-objects">Test APIs reference</a> and the <a href="https://developers.cloudflare.com/durable-objects/examples/testing-with-durable-objects/#testing-eviction">Testing Durable Objects guide</a>.</p>
The text above is the source's. reef added the severity, the dates it could read from the text, and the endpoints and scopes named in code.