Skip to content

Migrate from createTestHarness()

Wrangler's createTestHarness() only supports Wrangler configuration files. If you are moving a project to cf and cloudflare.config.ts, use @cloudflare/test-server for your integration tests instead. This guide shows how to update tests written for createTestHarness().

If you have not converted the project yet, start with Migrate a Wrangler project.

Every test suite needs the first two steps. The rest apply only if your tests use what the heading describes.

Install the package and update imports

npm i -D @cloudflare/test-server

Import createTestServer() from the new package in place of Wrangler's createTestHarness(), and rename the types. TestHarnessOptions also becomes TestServerOptions:

- import { createTestHarness, type TestHarness } from "wrangler";
+ import { createTestServer, type TestServer } from "@cloudflare/test-server";

Run each project's Build Output

Build each project with cf build before the tests run. Then replace configPath with projectDir, the project's root directory. If one build writes several Workers, such as a Vite project with auxiliary Workers, add an entry for each, and set name for the ones other than the default:

- const server = createTestHarness({
+ const server = createTestServer({
	workers: [
-		{ configPath: "./workers/web/wrangler.jsonc" },
-		{ configPath: "./workers/api/dist/api_worker/wrangler.json" },
-		{ configPath: "./workers/api/dist/admin_worker/wrangler.json" },
+		{ projectDir: "./workers/web" },
+		{ projectDir: "./workers/api" },
+		{ projectDir: "./workers/api", name: "admin-worker" },
	],
});

createTestServer() has no env or prebuiltWorkerDir option. To test a mode, build the project in that mode instead:

- { configPath: "./wrangler.jsonc", env: "staging" }
+ // After `cf build --mode staging`
+ { projectDir: "./" }

Replace test values with envOverrides

vars, secrets, and bindingOverrides become one envOverrides option. Write each value with the bindings helpers from cf/config that you use in cloudflare.config.ts:

+ import { bindings } from "cf/config";

{
-	configPath: "./workers/web/wrangler.jsonc",
-	vars: { API_HOST: "http://localhost:8787" },
-	secrets: { API_TOKEN: "test-token" },
-	bindingOverrides: { BROWSER: "mock-browser" },
+	projectDir: "./workers/web",
+	envOverrides: {
+		API_HOST: bindings.text("http://localhost:8787"),
+		API_TOKEN: bindings.text("test-token"),
+		BROWSER: bindings.worker({ worker: "mock-browser" }),
+	},
}

envOverrides can only replace bindings that the Worker declares. It cannot add a binding, as Wrangler's vars and secrets could, or remove one. Each replacement must be able to stand in for the original:

  • A variable or secret: bindings.text() or bindings.json()
  • A Durable Object: another Durable Object binding
  • Any other binding: a binding of the same type, such as a local KV namespace in place of a remote one, or bindings.worker() to mock it

Write inline Workers as Miniflare configuration

An inline Worker now takes its code inline: a Miniflare Worker configuration in miniflareConfig, instead of a Wrangler configuration that points at a file. Write the module's JavaScript in the test, so the test shows what the mock does. Give each binding its value directly, such as { type: "text", value }, because inline Workers do not read .dev.vars or .env:

{
-	config: {
-		name: "mock-ai",
-		main: "./mocks/ai.ts",
-		compatibility_date: "2026-06-01",
-	},
+	miniflareConfig: {
+		name: "mock-ai",
+		compatibilityDate: "2026-06-01",
+		manifest: {
+			mainModule: "index.js",
+			modules: {
+				"index.js": {
+					type: "esm",
+					contents: `
+						import { WorkerEntrypoint } from "cloudflare:workers";
+
+						export default class extends WorkerEntrypoint {
+							run() {
+								return { response: "Hello from a mock model" };
+							}
+						}
+					`,
+				},
+			},
+		},
+	},
}

If the mock is complex or shared across tests, make it a Worker project instead, build it with cf build, and list it with projectDir. An auxiliary Worker in another project's build works the same way.

Declare the secrets that tests read from files

Wrangler binds every key in .dev.vars and .env unless secrets.required is set. The test server reads only the secrets that cloudflare.config.ts declares with bindings.secret(). Other keys are ignored and do not override variables. Declare each secret that the Worker reads, or set its value in envOverrides:

cloudflare.config.tsts
env: {
	API_TOKEN: bindings.secret(),
},

Log in with cf if you use remote bindings

Remote bindings use the same credentials as cf, so a Wrangler login does not carry over. Log in with cf, or set CLOUDFLARE_API_TOKEN in CI:

cf auth login

If a Worker's only remote bindings are ones that always run remotely, such as AI, it starts without credentials and needs them only when a test calls one.

Pass export names to Durable Object and Workflow helpers

listDurableObjectIds(), evictDurableObject(), getDurableObjectStorage(), introspectWorkflow(), and introspectWorkflowInstance() take the class's export name, such as Counter, not its binding name, such as COUNTER. A Workflow no longer needs a binding to be introspected:

- const storage = await worker.getDurableObjectStorage("COUNTER", { name: "user-123" });
+ const storage = await worker.getDurableObjectStorage("Counter", { name: "user-123" });

- await using workflow = await worker.introspectWorkflow("MY_WORKFLOW");
+ await using workflow = await worker.introspectWorkflow("MyWorkflow");

Pass D1 migration options

applyD1Migrations() no longer reads migration settings from the Wrangler configuration. Pass the dir, pattern, and table options instead. dir defaults to ./migrations in the project directory:

- await worker.applyD1Migrations("DATABASE"); // migrations_dir from wrangler.jsonc
+ await worker.applyD1Migrations("DATABASE", { dir: "./db/migrations" });

It throws for a remote D1 database. To migrate a local database instead, replace the binding in envOverrides, such as DATABASE: bindings.d1().

Expect uncaught errors as 500 responses

As in production, an uncaught Worker error is a 500 response from fetch(), instead of a rejected promise:

- await expect(server.fetch("/boom")).rejects.toThrow();
+ const response = await server.fetch("/boom");
+ expect(response.status).toBe(500);

Update Worker handle types

getWorker() takes one type argument: the type of the Worker's cloudflare.config.ts module, from which it infers the Worker's environment and exports. The two-argument getWorker<Env, Module>() from createTestHarness() no longer type-checks:

- const api = server.getWorker<ApiEnv, typeof import("../workers/api/index")>("api-worker");
+ const api = server.getWorker<typeof import("../workers/api/cloudflare.config.ts")>("api-worker");

You no longer need to generate an environment interface for each Worker. Without a config type, a handle's environment and exports are any. To type them directly, pass the types to getEnv<Env>() and getExport<Export>(), for example getExport<{ getMessage(): string }>().

Other changes

  • Unlike createTestHarness(), server.update() can add or remove Workers.
  • The package is ESM-only.

Was this helpful?