Skip to content

composeDurableInstance

function composeDurableInstance(host: IFlareHost, state: DurableObjectState, env: Env, cls: FlareDurableObjectClass): DurableHandler

Composes one DO instance’s per-instance lazy container, seeding DurableState and Bindings for the given state and env, and returns a DurableHandler you can drive in-process for white-box tests.

const inst = composeDurableInstance(host, makeFakeDurableState({ name: "room-1" }), makeEnv(), MyDO);
const res = await inst.fetch(new Request("https://do/route"));
const svc = inst.inject([MyService], MyService);

The DO class constructor is bypassed. workerd’s native DurableObject base rejects a fake DurableObjectState, so the DO class itself can only be constructed by the workerd runtime. This function composes only the per-instance Flare container, not the DO class instance. To exercise the constructor, alarm, or RPC methods, use a real cloudflare:test binding. WebSocket routes can be driven directly through the returned handler’s fetch(req) against a fake DurableObjectState.

host IFlareHost · The built FlareHost that owns the DO registration and compiled arc.

state DurableObjectState · A real or fake DurableObjectState seeded as DurableState in the instance graph.

env Env · The Worker env seeded as Bindings in the instance graph.

cls FlareDurableObjectClass · The registered DO class (must have been passed to host.durableObject() before host.build()).

returns DurableHandler A DurableHandler scoped to this instance. Call inst.fetch(req) to dispatch HTTP or inst.inject(deps, token) to resolve services from the per-instance container.

Learn: Test DOs in-process

Last updated: