An integration test may need data in local storage before it runs. It may also depend on external services that you do not want to call during the test. Use the test server to prepare this state and replace those dependencies.
worker.getEnv() returns the variables, secrets, and bindings configured for a Worker. You can specify types for server.getWorker() so these values are typed. Then use the returned storage bindings to seed data directly from a test:
const apiWorker = server.getWorker("api-worker");
const env = await apiWorker.getEnv();
await env.USERS.put("123", JSON.stringify({ name: "Ada" }));const apiWorker = server.getWorker<
typeof import("../workers/api/cloudflare.config.ts")
>("api-worker");
const env = await apiWorker.getEnv();
await env.USERS.put("123", JSON.stringify({ name: "Ada" }));const apiWorker = server.getWorker("api-worker");
const env = await apiWorker.getEnv();
await env.USERS.put("123", JSON.stringify({ name: "Ada" }));const apiWorker = server.getWorker<ApiEnv>("api-worker");
const env = await apiWorker.getEnv();
await env.USERS.put("123", JSON.stringify({ name: "Ada" }));Use worker.applyD1Migrations(bindingName) to apply migration files to a D1 binding. By default, it applies the .sql files in the project's migrations directory. To use another directory, file name pattern, or table for recording applied migrations, pass the dir, pattern, or table option. It throws for a remote D1 database. To migrate a local database instead, replace the binding in envOverrides, such as DATABASE: bindings.d1().
Call it after storage is reset to apply migrations that have not already run. Then access the database with worker.getEnv() and seed the required rows.
const apiWorker = server.getWorker("api-worker");
beforeEach(async () => {
await apiWorker.applyD1Migrations("DATABASE");
const env = await apiWorker.getEnv();
await env.DATABASE.prepare(
"INSERT INTO daily_reports (date, user_ids) VALUES (?, ?)",
)
.bind("2026-05-29", JSON.stringify(["123", "456"]))
.run();
});const apiWorker = server.getWorker<
typeof import("../workers/api/cloudflare.config.ts")
>("api-worker");
beforeEach(async () => {
await apiWorker.applyD1Migrations("DATABASE");
const env = await apiWorker.getEnv();
await env.DATABASE.prepare(
"INSERT INTO daily_reports (date, user_ids) VALUES (?, ?)",
)
.bind("2026-05-29", JSON.stringify(["123", "456"]))
.run();
});Use worker.applyD1Migrations(bindingName) to read the migration settings for a D1 binding from the Wrangler configuration. It uses the configured migrations_dir and migrations_pattern. Without these options, it reads .sql files from the migrations directory relative to the configuration file.
Call it after storage is reset to apply migrations that have not already run. Then access the database with worker.getEnv() and seed the required rows.
const apiWorker = server.getWorker("api-worker");
beforeEach(async () => {
await apiWorker.applyD1Migrations("DATABASE");
const env = await apiWorker.getEnv();
await env.DATABASE.prepare(
"INSERT INTO daily_reports (date, user_ids) VALUES (?, ?)",
)
.bind("2026-05-29", JSON.stringify(["123", "456"]))
.run();
});const apiWorker = server.getWorker<ApiEnv>("api-worker");
beforeEach(async () => {
await apiWorker.applyD1Migrations("DATABASE");
const env = await apiWorker.getEnv();
await env.DATABASE.prepare(
"INSERT INTO daily_reports (date, user_ids) VALUES (?, ?)",
)
.bind("2026-05-29", JSON.stringify(["123", "456"]))
.run();
});worker.getDurableObjectStorage() gives you access to the storage of a SQLite-backed Durable Object instance. Pass the name of the Durable Object class that the Worker exports, such as Counter. Then select the instance by name or ID.
The returned handle executes SQL inside the Durable Object. Use it to seed an instance before a test or inspect its state after the Worker runs.
const worker = server.getWorker("api-worker");
const storage = await worker.getDurableObjectStorage("Counter", {
name: "user-123",
});
await storage.exec(
"INSERT INTO counters (id, value) VALUES (?, ?)",
"user-123",
0,
);
await worker.fetch("/counter/user-123");
const rows = await storage.exec(
"SELECT value FROM counters WHERE id = ?",
"user-123",
);
expect(rows).toEqual([{ value: 1 }]);const worker = server.getWorker<
typeof import("../workers/api/cloudflare.config.ts")
>("api-worker");
const storage = await worker.getDurableObjectStorage("Counter", {
name: "user-123",
});
await storage.exec(
"INSERT INTO counters (id, value) VALUES (?, ?)",
"user-123",
0,
);
await worker.fetch("/counter/user-123");
const rows = await storage.exec<{ value: number }>(
"SELECT value FROM counters WHERE id = ?",
"user-123",
);
expect(rows).toEqual([{ value: 1 }]);worker.listDurableObjectIds(), which lists the IDs of instances with stored data, and worker.evictDurableObject(), which restarts an instance and keeps its storage, also take the export name.
worker.getDurableObjectStorage() gives you access to the storage of a SQLite-backed Durable Object instance. Pass its binding name or exported class name. Then select the instance by name or ID.
The returned handle executes SQL inside the Durable Object. Use it to seed an instance before a test or inspect its state after the Worker runs.
const worker = server.getWorker("api-worker");
const storage = await worker.getDurableObjectStorage("COUNTER", {
name: "user-123",
});
await storage.exec(
"INSERT INTO counters (id, value) VALUES (?, ?)",
"user-123",
0,
);
await worker.fetch("/counter/user-123");
const rows = await storage.exec(
"SELECT value FROM counters WHERE id = ?",
"user-123",
);
expect(rows).toEqual([{ value: 1 }]);const worker = server.getWorker<ApiEnv>("api-worker");
const storage = await worker.getDurableObjectStorage("COUNTER", {
name: "user-123",
});
await storage.exec(
"INSERT INTO counters (id, value) VALUES (?, ?)",
"user-123",
0,
);
await worker.fetch("/counter/user-123");
const rows = await storage.exec<{ value: number }>(
"SELECT value FROM counters WHERE id = ?",
"user-123",
);
expect(rows).toEqual([{ value: 1 }]);The test server proxies outbound fetch() requests from your Workers through the globalThis.fetch() function in your Node environment. This allows you to intercept these requests and return a predictable response in your tests.
Here is an example using vi.spyOn() to mock a single request. But you can also use Mock Service Worker (MSW) to intercept these requests based on your preferences.
import { afterEach, test, vi } from "vitest";
afterEach(() => {
vi.restoreAllMocks();
});
test("loads a user profile", async ({ expect }) => {
vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => {
const request = new Request(input, init);
if (request.url === "http://identity.example.com/profile/123") {
return Response.json({ id: "123", name: "Ada" });
}
throw new Error(`Unexpected request: ${request.method} ${request.url}`);
});
const worker = server.getWorker();
const response = await worker.fetch("/users/123");
expect(await response.json()).toEqual({ id: "123", name: "Ada" });
});import { afterEach, test, vi } from "vitest";
afterEach(() => {
vi.restoreAllMocks();
});
test("loads a user profile", async ({ expect }) => {
vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => {
const request = new Request(input, init);
if (request.url === "http://identity.example.com/profile/123") {
return Response.json({ id: "123", name: "Ada" });
}
throw new Error(`Unexpected request: ${request.method} ${request.url}`);
});
const worker = server.getWorker();
const response = await worker.fetch("/users/123");
expect(await response.json()).toEqual({ id: "123", name: "Ada" });
});Use envOverrides with bindings.worker() when you want to control the behavior of a binding. It routes the binding to a test Worker running inside the test server. For example, a test Worker can replace the Browser Rendering binding and return a known screenshot without starting a browser.
The test Worker can also expose JSRPC methods that configure its behavior. Use worker.getExport() to access the default export from your test. The test Worker in this example is a project of its own, so build it with cf build too.
import { createTestServer } from "@cloudflare/test-server";
import { bindings } from "cf/config";
const server = createTestServer({
workers: [
{
projectDir: "./workers/web",
envOverrides: { BROWSER: bindings.worker({ worker: "mock-browser" }) },
},
// The test Worker (mock-browser) that replaces the Browser Rendering binding
{ projectDir: "./workers/mock-browser" },
],
});
// Access the test Worker to configure its behavior or assert its state.
const mockBrowser = await server.getWorker("mock-browser").getExport();
// Configure the screenshot to be returned through the overridden binding.
await mockBrowser.setScreenshot([137, 80, 78, 71]);
const response = await server.fetch("/reports/2026-05-29.png");
expect(await response.bytes()).toEqual(Uint8Array.from([137, 80, 78, 71]));import { createTestServer } from "@cloudflare/test-server";
import { bindings } from "cf/config";
const server = createTestServer({
workers: [
{
projectDir: "./workers/web",
envOverrides: { BROWSER: bindings.worker({ worker: "mock-browser" }) },
},
// The test Worker (mock-browser) that replaces the Browser Rendering binding
{ projectDir: "./workers/mock-browser" },
],
});
// Access the test Worker to configure its behavior or assert its state.
const mockBrowser = await server
.getWorker<
typeof import("../workers/mock-browser/cloudflare.config.ts")
>("mock-browser")
.getExport();
// Configure the screenshot to be returned through the overridden binding.
await mockBrowser.setScreenshot([137, 80, 78, 71]);
const response = await server.fetch("/reports/2026-05-29.png");
expect(await response.bytes()).toEqual(Uint8Array.from([137, 80, 78, 71]));For a small mock, write the test Worker's code inline with miniflareConfig, so the test shows what the mock does. It takes a Miniflare Worker configuration: put the module's JavaScript in manifest, and give each binding its value directly. If the mock is complex or shared across tests, make it a Worker project instead, like mock-browser in the example above. The following test Worker replaces an AI binding:
const server = createTestServer({
workers: [
{
projectDir: "./",
envOverrides: { AI: bindings.worker({ worker: "mock-ai" }) },
},
{
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(model, options) {
return { response: "Hello from a mock model" };
}
}
`,
},
},
},
},
},
],
});const server = createTestServer({
workers: [
{
projectDir: "./",
envOverrides: { AI: bindings.worker({ worker: "mock-ai" }) },
},
{
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(model, options) {
return { response: "Hello from a mock model" };
}
}
`,
},
},
},
},
},
],
});Use bindingOverrides when you want to control the behavior of a binding. It routes the binding to a test Worker running inside the harness. For example, a test Worker can replace the Browser Rendering binding and return a known screenshot without starting a browser.
The test Worker can also expose JSRPC methods that configure its behavior. Use worker.getExport() to access the default export from your test.
const server = createTestHarness({
workers: [
{
configPath: "./workers/web/wrangler.jsonc",
bindingOverrides: { BROWSER: "mock-browser" },
},
// The test Worker (mock-browser) that replaces the Browser Rendering binding
{ configPath: "./workers/mock-browser/wrangler.jsonc" },
],
});
// Access the test Worker to configure its behavior or assert its state.
const mockBrowser = await server.getWorker("mock-browser").getExport();
// Configure the screenshot to be returned through the overridden binding.
await mockBrowser.setScreenshot([137, 80, 78, 71]);
const response = await server.fetch("/reports/2026-05-29.png");
expect(await response.bytes()).toEqual(Uint8Array.from([137, 80, 78, 71]));const server = createTestHarness({
workers: [
{
configPath: "./workers/web/wrangler.jsonc",
bindingOverrides: { BROWSER: "mock-browser" },
},
// The test Worker (mock-browser) that replaces the Browser Rendering binding
{ configPath: "./workers/mock-browser/wrangler.jsonc" },
],
});
// Access the test Worker to configure its behavior or assert its state.
const mockBrowser = await server
.getWorker<unknown, typeof import("../workers/mock-browser")>("mock-browser")
.getExport();
// Configure the screenshot to be returned through the overridden binding.
await mockBrowser.setScreenshot([137, 80, 78, 71]);
const response = await server.fetch("/reports/2026-05-29.png");
expect(await response.bytes()).toEqual(Uint8Array.from([137, 80, 78, 71]));