Miniflare: Local Workers Testing, Fetch Dispatch, and Storage
How to use the Miniflare API to test Cloudflare Workers locally—dispatchFetch, bindings, Durable Objects, v5 changes, and security limits.
This page summarises Cloudflare’s documentation on Miniflare: a TypeScript simulator that runs a local workerd process so you can develop and test Workers without deploying. Most teams should use Wrangler (wrangler dev, the Cloudflare Vite plugin, or @cloudflare/vitest-plugin) instead of calling Miniflare directly; the API matters when you need fine-grained control in tests or advanced automation.
Miniflare vs Wrangler: when to use the API
Section titled “Miniflare vs Wrangler: when to use the API”Wrangler is the default path for build, run, and deploy. The Miniflare API is for dispatching events without real HTTP, simulating Worker-to-Worker links, and driving local emulations of KV, R2, Durable Objects, and related bindings—useful for integration tests and custom tooling.
The Miniflare API allows you to dispatch events to workers without making actual HTTP requests, simulate connections between Workers, and interact with local emulations of storage products like KV, R2, and Durable Objects. This makes it great for writing tests, or other advanced use cases where you need finer-grained control.
— Cloudflare, Get Started
Most users should use Wrangler to build, run, and deploy their Workers.
— Cloudflare, Miniflare
Miniflare v3+ no longer ships a standalone CLI; local dev servers use Wrangler (npx wrangler@3 dev) per the v2 migration guide.
Initialise Miniflare: script, modules, and cleanup
Section titled “Initialise Miniflare: script, modules, and cleanup”Node.js ES modules and first dispatchFetch
Section titled “Node.js ES modules and first dispatchFetch”Run Node in ES module mode ("type": "module" in package.json), import Miniflare, pass script or scriptPath, and call dispatchFetch to exercise the worker without a browser.
import { Miniflare } from "miniflare";
const mf = new Miniflare({ modules: true, script: ` export default { async fetch(request, env, ctx) { return new Response("Hello Miniflare!"); } } `,});
const res = await mf.dispatchFetch("http://localhost:8787/");console.log(await res.text()); // Hello Miniflare!await mf.dispose();— pattern from Cloudflare, Get Started
Use scriptPath for a file such as worker.js instead of an inline string. After tests, call dispose() to stop listeners and release storage connections.
setOptions, reload, and HTTP server
Section titled “setOptions, reload, and HTTP server”setOptions() accepts the full configuration object (not a partial patch in v3+), applies it, and reloads the worker—replacing the removed reload() method per Migrating from Version 2. Miniflare starts an HTTP server automatically; await mf.ready before assuming a custom port is listening (Get Started).
File watching is not built in for the API-first use case; use fs.watch() or chokidar and call setOptions() with your full config when files change (Get Started).
dispatchFetch: HTTP testing without a client
Section titled “dispatchFetch: HTTP testing without a client”What happens on each request
Section titled “What happens on each request”Whenever an HTTP request is handled, a Request is dispatched to your worker and the resulting Response is returned. The Request includes a cf object. Miniflare logs method, path, status, and timing. Uncaught errors yield an error page with a stack trace.
Whenever an HTTP request is made, a Request object is dispatched to your worker, then the generated Response is returned. The Request object will include a cf object. Miniflare will log the method, path, status, and the time it took to respond.
— Cloudflare, Fetch Events
Calling dispatchFetch and simulating cf
Section titled “Calling dispatchFetch and simulating cf”dispatchFetch mirrors the fetch API: URL plus optional RequestInit, or a Request instance (Fetch Events). When dispatching programmatically, you supply CF-* headers and the cf object so tests can fix country, TLS, or other metadata:
const res = await mf.dispatchFetch("http://localhost:8787", { headers: { "CF-IPCountry": "GB" }, cf: { country: "GB" },});Upstream when the worker does not respond
Section titled “Upstream when the worker does not respond”Miniflare invokes fetch listeners until a response is returned. If none is returned, or an exception is thrown after passThroughOnException(), the response is fetched from the configured upstream URL (Fetch Events).
Scheduled, queue, and worker dispatch via getWorker
Section titled “Scheduled, queue, and worker dispatch via getWorker”Besides dispatchFetch, getWorker() exposes scheduled() and queue() for cron and queue handlers—see the extended example in Get Started. Helper accessors such as getKVNamespace, getR2Bucket, getDurableObjectNamespace, getD1Database, and getQueueProducer are documented in the same page’s reference block.
Compatibility date and flags
Section titled “Compatibility date and flags”Set compatibilityDate to opt into runtime behaviour as of a given date, and use compatibilityFlags to toggle specific backwards-incompatible changes:
const mf = new Miniflare({ compatibilityFlags: [ "formdata_parser_supports_files", "durable_object_fetch_allows_relative_url", ],});— Cloudflare, Compatibility
Durable Objects in local tests
Section titled “Durable Objects in local tests”Bind classes in durableObjects (class name must be exported from the main script). For tests outside the worker entrypoint, use getDurableObjectNamespace:
const ns = await mf.getDurableObjectNamespace("TEST_OBJECT");const stub = ns.getByName("test");const doRes = await stub.fetch("http://localhost:8787/put");— pattern from Cloudflare, Durable Objects
For objects defined in another worker, use the script_name option and mount that worker as described under Multiple Workers.
Multiple Workers and service bindings
Section titled “Multiple Workers and service bindings”Miniflare v3 replaced parent/child mounts with a top-level workers array: named workers, shared persistence/HTTP options at the root, and serviceBindings between them (Migrating from Version 2). Service bindings can target another worker or a custom async function in Node for stubs.
Mock outbound fetch in tests
Section titled “Mock outbound fetch in tests”Use createFetchMock() (undici MockAgent) via the fetchMock option to return fixed responses for subrequests (Web Standards). Call fetchMock.disableNetConnect() to fail on unmocked outbound calls. The Vitest integration removed getMiniflareFetchMock(); migrate to @msw/cloudflare per Mock outbound requests.
Miniflare does not enforce subrequest limits locally—unlike production quotas (Web Standards).
Live reload during development
Section titled “Live reload during development”With liveReload: true, Miniflare injects a reload script only into responses whose Content-Type is text/html (Live Reload).
Miniflare v5 and cloudflare.config.ts
Section titled “Miniflare v5 and cloudflare.config.ts”Changelog entries describe Miniflare v5 aligning local tooling with the upcoming cf CLI and programmatic cloudflare.config.ts testing configuration (changelog).
Miniflare powers local Workers development behind wrangler dev, the Cloudflare Vite plugin, and @cloudflare/vitest-plugin. Most projects should use those tools instead of depending on Miniflare directly, and Miniflare v5 will not require any action.
— Cloudflare, Miniflare v5 prepares local development for the cf CLI
Notable breaking changes called out in the same posts (Workers changelog, developer platform changelog, changelog index):
| Change | Practical effect |
|---|---|
| New configuration shape | Aligns with cloudflare.config.ts for programmatic config |
| Removed legacy alpha D1 bindings | Use current D1 binding APIs |
Removed wrappedBindings |
Internal API; update custom Miniflare setups |
| No built-in module discovery | Wrangler / Vite plugin supply the module graph |
/cdn-cgi local routes |
Local-only routes under /cdn-cgi/local |
| Persistence options | Per-resource persist paths replaced by shared persistence root |
Full detail lives in Miniflare’s own changelog (linked from those posts).
Runtime fidelity and migration from Miniflare 2
Section titled “Runtime fidelity and migration from Miniflare 2”Miniflare v3+ uses workerd, the open-source Workers runtime, for close production parity (Migrating from Version 2). Many v2 options were removed or moved: Wrangler config loading (wranglerConfigPath), packagePath, built-in watch, HTTPS server options (may return later), crons option, metaProvider (use dispatchFetch cf/headers or top-level cf), and sandbox introspection methods (getGlobalScope, getBindings, getModuleExports). Packages under @miniflare/* merged into the single miniflare npm package.
If behaviour diverges from production and the docs do not document the gap, Cloudflare asks you to open a GitHub issue from the main Miniflare page.
Security: Miniflare is not a hardened sandbox
Section titled “Security: Miniflare is not a hardened sandbox”Miniflare is not a hardened sandbox … Miniflare starts a local workerd process. This warning applies to default local execution with wrangler dev, vite dev, vite preview, direct Miniflare use, and Miniflare-based testing tools. Setting remote: true on a binding changes which resource your code accesses, but your Worker code still executes locally.
— Cloudflare, Local development — Security considerations
workerd restricts Worker code to configured resources, but bugs could break isolation; the hosted platform adds defense in depth that local Miniflare does not replicate (Miniflare, Local development).
Common mistakes
Section titled “Common mistakes”- Depending on Miniflare directly when Wrangler or Vitest plugins already wrap it—extra breakage on v5 config changes.
- Passing partial objects to
setOptions()after migrating from v2 (full config required). - Expecting automatic file watch/reload without your own watcher.
- Treating local absence of subrequest limits as proof the Worker is within production quotas.
- Running third-party Worker code locally without isolating the host.
How to check this on your site
Section titled “How to check this on your site”-
Confirm your project uses Wrangler or the Vitest plugin for day-to-day dev, and reserve direct
Miniflareconstruction for tests documented in Get Started. -
Run a minimal test:
dispatchFetchto your worker URL, assert status and body, thenawait mf.dispose()so nothing keeps listening. -
If you mock outbound HTTP, wire
createFetchMock()per Web Standards and enabledisableNetConnect()so accidental real network calls fail loudly. -
After upgrading tooling, read the Miniflare v5 changelog post and verify local routes and persistence paths if you customised low-level Miniflare options.
-
You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page disagrees with the linked Cloudflare docs, or you need a clarification on Miniflare testing setup, email yimasynthetic@gmail.com with the page URL and the exact statement you are questioning.