createTestHarness() API for integration testing Workers
Wrangler now provides createTestHarness(), an API for running integration tests against Workers from any Node.js test runner. The test harness starts a local Worker server with helpers for dispatching requests, resetting storage, and inspecting runtime logs, supporting multi-Worker routing, request mocking with libraries like MSW, and browser testing with Playwright.
Wrangler now provides createTestHarness(), an API for running integration tests against Workers built with Wrangler or the Cloudflare Vite plugin from any Node.js test runner.
The test harness starts a local Worker server with helpers for dispatching requests, resetting storage, and inspecting runtime logs.
This is useful for tests that need to:
- Route requests across multiple Workers
- Mock outbound
fetch()requests with Node.js request mocking libraries such as MSW ↗ - Run Playwright tests against a Worker
For example, this test starts two Workers and mocks an upstream API:
tests/vitest.test.jsjsimport { afterAll, afterEach, beforeAll, test } from "vitest";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import { createTestHarness } from "wrangler";
const network = setupServer();
const server = createTestHarness({
workers: [
/** Includes `"routes": ["example.com/*"]` */
{ configPath: "./workers/web/wrangler.jsonc" },
/** Includes `"routes": ["api.example.com/v1/*"]` */
{ configPath: "./workers/api/wrangler.jsonc" },
],
});
beforeAll(async () => {
network.listen({ onUnhandledRequest: "error" });
await server.listen();
});
afterEach(async () => {
network.resetHandlers();
await server.reset();
});
afterAll(async () => {
network.close();
await server.close();
});
test("routes requests to each Worker", async ({ expect }) => {
// Mock the outbound fetch used to load user profiles.
network.use(
http.get("http://identity.example.com/profile/123", ({ params }) => {
return HttpResponse.json({ id: 123, name: "Ada" });
}),
);
const apiWorkerResponse = await server.fetch(
"http://api.example.com/v1/users/123",
);
expect(await apiWorkerResponse.json()).toEqual({
id: 123,
name: "Ada",
});
const webWorkerResponse = await server.fetch("http://example.com/users/123");
expect(await webWorkerResponse.text()).toBe("Profile: Ada");
});tests/vitest.test.tstsimport { afterAll, afterEach, beforeAll, test } from "vitest";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import { createTestHarness } from "wrangler";
const network = setupServer();
const server = createTestHarness({
workers: [
/** Includes `"routes": ["example.com/*"]` */
{ configPath: "./workers/web/wrangler.jsonc" },
/** Includes `"routes": ["api.example.com/v1/*"]` */
{ configPath: "./workers/api/wrangler.jsonc" },
],
});
beforeAll(async () => {
network.listen({ onUnhandledRequest: "error" });
await server.listen();
});
afterEach(async () => {
network.resetHandlers();
await server.reset();
});
afterAll(async () => {
network.close();
await server.close();
});
test("routes requests to each Worker", async ({ expect }) => {
// Mock the outbound fetch used to load user profiles.
network.use(
http.get("http://identity.example.com/profile/123", ({ params }) => {
return HttpResponse.json({ id: 123, name: "Ada" });
}),
);
const apiWorkerResponse = await server.fetch(
"http://api.example.com/v1/users/123",
);
expect(await apiWorkerResponse.json()).toEqual({
id: 123,
name: "Ada",
});
const webWorkerResponse = await server.fetch("http://example.com/users/123");
expect(await webWorkerResponse.text()).toBe("Profile: Ada");
});
Cloudflare now recommends createTestHarness() for integration tests instead of unstable_startWorker() or unstable_dev(). To start a development server programmatically, use the Vite createServer() ↗ API with the Cloudflare Vite plugin.
For more information about createTestHarness(), refer to the Integration test harness guide.
Source: original entry ↗
More from Cloudflare
Follow Cloudflare to get its new changes in your feed and email digest.
Cloudflare One Client for macOS 2026.8.2100.0
GA release for macOS Cloudflare One Client with improved split tunnel handling that no longer briefly blocks traffic during reconnects, support for non-RFC 1918 local IPv4 networks, faster connects with lower memory use, and numerous reliability fixes across DNS, reauthentication, and client stability.
Cloudflare One Client for Windows 2026.8.2100.0
This GA release improves split tunnel reliability, adds support for non-RFC 1918 local networks, optimizes connection performance with faster reconnections and lower memory usage, and includes numerous bug fixes for DNS, registration, and network handling. The client now features a service recovery mechanism that automatically restarts on system unlock and better handles large hosts files without blocking traffic.
Cloudflare One Client for Linux 2026.8.2100.0
New GA release for Linux with improved split tunnel handling that no longer briefly blocks traffic during reconnects, support for non-RFC 1918 local IPv4 networks, faster tunnel reconnections, and lower memory usage. Includes numerous stability and reliability fixes for DNS, reconnection behavior, and crash issues.