Devflare Docs
Test harness Devflare

Use and as the default runtime-shaped test harness

Start tests with so the same config, bindings, routes, and handler surfaces the app uses in real runtime flows are available in Bun tests.

Devflare’s recommended test story is not a pile of hand-built mocks. loads the nearest supported config, wires the local runtime surface, and gives you helpers that feel like the Worker entrypoints the app actually uses.

Best for
Runtime-shaped tests that should stay close to the real worker surface
Default harness
plus helpers
Optional extra
for custom class round-trips across local RPC-style bridge calls, especially Durable Object methods

Let the harness discover the normal worker shape first

When you omit the config path, walks upward from the calling test file and finds the nearest supported config filename. It then autodetects the conventional worker surfaces that belong to that package instead of making you wire each one by hand.

That is the main reason the built-in harness scales: the same config and file conventions keep working as the package gains routes, queues, scheduled handlers, inbound email, or tail handlers.

  • Config path autodiscovery starts from the calling test file when you omit the argument.
  • Conventional files such as , , , , , and are discovered automatically when present.
  • Service bindings and other config-driven runtime surfaces are discovered from the same authored config instead of a separate test-only schema.
  • If a local RPC-style bridge call under test later needs custom class round-trips, the harness can also discover automatically.

Know which helpers wait for background work and which do not

These helpers are runtime-shaped and context-accurate for handler logic, but they do not try to recreate every internal Cloudflare dispatch step byte for byte. Their timing rules are documented explicitly instead of being left to guesswork.

Each surface is also exported standalone for tree-shaking — is the same function as the named export, just as mirrors . The Durable Object helper takes a DO instance you construct in the test (not a handler file path), mirroring how the runtime wrapper invokes .

HelperCurrent behavior
Returns when the handler resolves, without waiting for its work. waits for whatever of that work is still running, for up to 2 seconds by default, before it tears the runtime down.
Waits for queued background work before it returns.
Waits for scheduled background work before it returns.
In tests, directly invokes the configured local email handler and waits for its queued work; otherwise it falls back to the local email endpoint.
Works when exists, supports a default or named export, and waits for the handler plus its work before it returns.
Fires a Durable Object instance’s handler under a event context (the standalone export is the same trigger) and awaits it, returning .

Do not assert the wrong timing contract

If a test depends on side effects being complete, a plain assertion may be too early. Either assert the side effect directly or move that check into a higher-fidelity path.

waits for the background work left running

A handler’s work keeps running after returns, as it does in workerd. When is called, it first waits for whatever of that work is still running, for up to 2 seconds, and only then shuts the runtime down. To wait longer, pass and raise the hook timeout to match, because Bun gives an hook 5 seconds by default: .

Work that settles before is called is left to Bun: nothing is attached to it, and reads its state with , which attaches nothing either. A rejection nothing handled fails the test running at that moment, or is reported between tests, without naming the request. A rejection the handler awaited and recovered from is not reported at all. Promise subclasses, such as an SDK client’s , behave the same way. A thenable that is not a Promise is resolved once, when is called, as workerd does, and the promise that produces is the one tracked; nothing in your code can handle its rejection.

Work still running when starts is reported by . If it rejects while dispose waits, throws a after teardown that names the request, for example , and carries the original error as its . That includes a promise your code also handles elsewhere, as in , when it is still pending as dispose starts and rejects while dispose waits. If work is still running when the budget runs out, tears down anyway and throws a naming the request; a rejection from that work after the teardown is logged with and fails no test. is exported from .

Each drains only its own work. If an earlier context was never disposed, its work is neither waited for nor blamed on the next context, and creating the next context logs which requests started the work still pending.

These rules hold under Bun. Outside Bun there is no , so a Promise subclass that settled before can also be reported by .

Tail handlers are a public config surface with a real test helper

Tail support is a real helper surface in the harness and a public config key, alongside , , , and . When is unset, auto-discovers , wires automatically, and runs the handler with the same runtime helper access as the other test surfaces.

The handler can export a default function or a named function. The helper accepts either full trace items or smaller option objects through , then waits for the handler and any queued work before it returns.

  • is auto-discovered when is unset; set to point at a custom path, or to disable discovery — the same model as , , , and .
  • Use when the test only needs a few trace fields, and pass full trace items when the payload details are the point of the assertion.
  • Reach for a higher-fidelity integration path when the question is Cloudflare ingress behavior rather than your own log or trace handling logic.

Documented like the other handler surfaces

Tail has a public config key and is documented like fetch, queue, scheduled, and email: leave it unset to auto-discover , point it at a custom path, or set it to to disable discovery.

A tiny tail handler plus one honest harness test

Start with one small proof test before layering helpers on top

Keep the first test boring

If the harness is working, you should be able to prove one route or handler path quickly before you hide it behind bigger factory helpers or shared test setup.

A minimal runtime-shaped test

Run as many test files per process as you like, even with Durable Objects

A config with Durable Objects has its class graph bundled before the runtime boots. That bundle is cached on disk under , so the second test file in a process reuses it rather than rebuilding it.

That is a correctness fix as much as a speed one. Rebuilding it mid-process is what suites used to spend a whole process per test file to avoid: once the test runner has loaded a module on that graph, the bundler will not re-read it and fails against an ordinary source file with a misleading file error.

  • An entry is reused only when nothing it depended on moved: the content of every file on the transitive graph — editing a module a Durable Object imports rebuilds it, same as editing the Durable Object — plus the / files above them, a listing of each directory they came from, and the bundler and devflare versions.
  • It lives beside the config rather than under , so parallel test slots pointed at their own generated directories still share one build.
  • is already in the generated-state directory you ignore in version control; nothing new needs ignoring.
  • Each context still boots its own runtime, so two files in one process never see each other’s KV, R2, D1, or Durable Object state.
  • The config is loaded once per process too. The config module was only ever evaluated once anyway, but the env and overlay is now held with it — call from if a test rewrites those between contexts.

Storage isolation is per context, not per process

takes the runtime and its storage with it, so the next starts empty however many contexts a process has already created.

Delete the cache when resolution changed out from under it

What the cache tracks is what the last build read and resolved through. A resolver input that did not exist then and sits outside those directories — a added further up the tree, a newly installed package that shadows one — can leave an entry looking fresh. Removing forces a rebuild, and from does the same within one process.

Add only when local RPC-style bridge calls in tests must preserve custom classes

Most tests do not need a transport file because strings, numbers, arrays, and plain JSON objects already cross the bridge naturally.

Reach for when a local RPC-style bridge call returns a real class instance and the caller needs that class again instead of a plain object. In practice that is most often a Durable Object method round-trip inside , not an ordinary HTTP response.

  • Keep the encoded payload plain and JSON-friendly.
  • Use one small transport entry per value type so decode rules stay reviewable.
  • Set when you want to disable the convention explicitly for one package.

Know where to go when the harness is only part of the question

The harness is the center, not the whole map

is the default test loop, but binding-specific caveats, runtime-context rules, and automation concerns still belong on their own pages.

Previous

Testing overview

Devflare’s testing story is layered: start with one real unit test, use and for the runtime-shaped harness, then jump to binding-specific guides or CI-focused pages only when the question changes.

Next

Binding testing

Every binding overview page already links a hidden testing guide. This page collects those guides in one place so you can jump straight to the right harness, caveats, and escalation path for the binding that changed.