sincecutoff.com
← all changes

createTestHarness() overrides bindings and accesses Worker exports

Introduced 2026-06-30 · score 0.96

HOW TO USE IT

When a config-backed Worker needs a mock for a service binding, set `bindingOverrides` to the mock Worker’s harness name. To control the mock, call `getExport()` on `server.getWorker(name)` and use its default export’s RPC methods.

import { createTestHarness } from "wrangler"; const server = createTestHarness({ workers: [ { configPath: "<path-to-worker-wrangler-config>", bindingOverrides: { <BINDING_NAME>: "<mock-worker-name>" }, }, { configPath: "<path-to-mock-worker-wrangler-config>" }, ], }); const mock = await server.getWorker("<mock-worker-name>").getExport(); await mock.<rpcMethod>(<arguments>);

WHY THIS HELPS

Test a Worker that depends on a platform binding by replacing it with a mock Worker and controlling the mock's exported methods.

REQUIREMENTS AND LIMITATIONS

  • Version: wrangler@4.106.0
  • bindingOverrides: A record mapping binding names in this Worker's environment to Worker names in this test harness.
  • configPath (required): Path to a Wrangler configuration file; relative paths resolve from `root`.
  • server.getWorker(name): Returns a Worker handle; omit the name to select the primary Worker.
  • getExport(): Returns the default Worker export, including RPC methods.
  • Prerequisite: Import `createTestHarness` from `wrangler`; use Node.js test runner environment. The harness runs production build output from Wrangler config files, Vite-generated configs, or inline Wrangler config objects.
  • This applies to test harness Workers loaded from Wrangler config files.
  • Binding overrides can target a Worker in the same harness.
  • `getExport()` is called on a Worker returned by `server.getWorker(name)` and accesses JSRPC methods on its default Worker export.

FIRST-PARTY EVIDENCE

Check the installed version and the source before using it. Entry JSON

Report a problem with this card