One test server can run several Workers. List each one in the workers array.
Set each entry's projectDir to a project's root directory, and run cf build in each project first. Relative paths resolve from the current working directory, or from the top-level root option if you set it:
const server = createTestServer({
workers: [{ projectDir: "./workers/web" }, { projectDir: "./workers/api" }],
});const server = createTestServer({
workers: [
{ projectDir: "./workers/web" },
{ projectDir: "./workers/api" },
],
});A project's Build Output can contain more than one Worker. The test server runs the build's default Worker, unless you set name to the name of another Worker in the same build:
const server = createTestServer({
workers: [
{ projectDir: "./" },
// Another Worker from the same build
{ projectDir: "./", name: "admin-worker" },
],
});const server = createTestServer({
workers: [
{ projectDir: "./" },
// Another Worker from the same build
{ projectDir: "./", name: "admin-worker" },
],
});Remote bindings use the same credentials as cf, so a Wrangler login does not carry over. Run cf auth login, or set CLOUDFLARE_API_TOKEN in CI.
Each Worker can come from a Wrangler project or a Vite project that uses the Cloudflare Vite plugin. Point each entry in the workers array to the Wrangler configuration file for a project:
const server = createTestHarness({
workers: [{ configPath: "./wrangler.jsonc" }],
});const server = createTestHarness({
workers: [{ configPath: "./wrangler.jsonc" }],
});For Workers built by the Cloudflare Vite plugin, run vite build first so tests use the production build output:
npx vite buildyarn vite buildpnpm vite buildThe generated Wrangler configuration works like any other configPath. Each Worker is configured independently, so one harness can run both project types:
const server = createTestHarness({
workers: [
// Wrangler project
{ configPath: "./workers/api/wrangler.jsonc" },
// Vite project (built output from the Cloudflare Vite plugin)
{ configPath: "./dist/web_worker/wrangler.json" },
],
});const server = createTestHarness({
workers: [
// Wrangler project
{ configPath: "./workers/api/wrangler.jsonc" },
// Vite project (built output from the Cloudflare Vite plugin)
{ configPath: "./dist/web_worker/wrangler.json" },
],
});The test server runs the Build Output in the mode it was built with. To test a mode, build the project in that mode:
cf build --mode stagingSecrets then read their local values from the .dev.vars and .env files for that mode, as in cf dev. Without --mode, the mode that a build records depends on the build tool.
By default, the test harness loads the top-level Wrangler configuration. Set env if you want to load a specific environment from the configuration.
const server = createTestHarness({
workers: [{ configPath: "./wrangler.jsonc", env: "test" }],
});const server = createTestHarness({
workers: [{ configPath: "./wrangler.jsonc", env: "test" }],
});Use envOverrides to replace a Worker's variables and secrets with test values, so that you do not need a separate mode for testing. Write each value with the bindings helpers from cf/config that you use in cloudflare.config.ts:
import { createTestServer } from "@cloudflare/test-server";
import { bindings } from "cf/config";
const server = createTestServer({
workers: [
{
projectDir: "./",
envOverrides: {
API_HOST: bindings.text("http://identity.example.com"),
API_TOKEN: bindings.text("test-token"),
},
},
],
});import { createTestServer } from "@cloudflare/test-server";
import { bindings } from "cf/config";
const server = createTestServer({
workers: [
{
projectDir: "./",
envOverrides: {
API_HOST: bindings.text("http://identity.example.com"),
API_TOKEN: bindings.text("test-token"),
},
},
],
});envOverrides can only replace bindings that the Worker declares, and each replacement must be able to stand in for the original:
- A variable or secret:
bindings.text()orbindings.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 with a test Worker
A secret without an override reads its local value from the project's .dev.vars or .env files, but only if cloudflare.config.ts declares it with bindings.secret(). Other keys in those files are ignored.
You can override vars and secrets for each Worker in the harness if you want to avoid creating a separate Wrangler environment for testing.
const server = createTestHarness({
workers: [
{
configPath: "./wrangler.jsonc",
vars: { API_HOST: "http://identity.example.com" },
secrets: { API_TOKEN: "test-token" },
},
],
});const server = createTestHarness({
workers: [
{
configPath: "./wrangler.jsonc",
vars: { API_HOST: "http://identity.example.com" },
secrets: { API_TOKEN: "test-token" },
},
],
});If part of the Worker configuration depends on the test setup, you can create the test server without options, then pass them to server.update() before server.listen().
const server = createTestServer();
let upstream;
beforeAll(async () => {
upstream = await startLocalApi();
await server.update({
workers: [
{
projectDir: "./",
envOverrides: { API_HOST: bindings.text(upstream.url) },
},
],
});
await server.listen();
});
afterAll(async () => {
await server.close();
await upstream.close();
});const server = createTestServer();
let upstream: { url: string; close(): Promise<void> };
beforeAll(async () => {
upstream = await startLocalApi();
await server.update({
workers: [
{
projectDir: "./",
envOverrides: { API_HOST: bindings.text(upstream.url) },
},
],
});
await server.listen();
});
afterAll(async () => {
await server.close();
await upstream.close();
});const server = createTestHarness();
let upstream;
beforeAll(async () => {
upstream = await startLocalApi();
await server.update({
workers: [
{
configPath: "./wrangler.jsonc",
vars: { API_HOST: upstream.url },
},
],
});
await server.listen();
});
afterAll(async () => {
await server.close();
await upstream.close();
});const server = createTestHarness();
let upstream: { url: string; close(): Promise<void> };
beforeAll(async () => {
upstream = await startLocalApi();
await server.update({
workers: [
{
configPath: "./wrangler.jsonc",
vars: { API_HOST: upstream.url },
},
],
});
await server.listen();
});
afterAll(async () => {
await server.close();
await upstream.close();
});When reusing a server across tests, call server.reset() after each test. It recreates local storage and restores Workers to the options used when the current session started.
afterEach(async () => {
await server.reset();
});afterEach(async () => {
await server.reset();
});After a reset, apply any required schema migrations and seed data again. For examples, refer to Prepare test state.
server.debug() prints the server timeline and captured Workers runtime logs. Call it when a test throws an exception or fails and you need more information to debug it.
The following example uses a cleanup hook from Vitest:
afterEach(({ task }) => {
if (task.result?.state === "fail") {
server.debug();
}
});afterEach(({ task }) => {
if (task.result?.state === "fail") {
server.debug();
}
});Pass the type of a Worker's cloudflare.config.ts module to server.getWorker(). The handle infers the Worker's environment and exports from it, including the default export's RPC methods:
const apiWorker = server.getWorker("api-worker");const apiWorker = server.getWorker<
typeof import("../workers/api/cloudflare.config.ts")
>("api-worker");Without a config type, the handle's environment and exports are any. To type them directly, pass the types to getEnv() and getExport():
const apiWorker = server.getWorker("api-worker");
const env = await apiWorker.getEnv();
const api = await apiWorker.getExport();const apiWorker = server.getWorker("api-worker");
const env = await apiWorker.getEnv<{ API_HOST: string }>();
const api = await apiWorker.getExport<{ getMessage(): string }>();Resource bindings, such as D1 and KV, use the global types from @cloudflare/workers-types. Include them in the TypeScript configuration for your tests, or these bindings are typed as any:
{
"compilerOptions": {
"types": ["@cloudflare/workers-types", "node"]
}
}server.getWorker() accepts types for the Worker environment and module exports. You can define these types manually. But to keep them aligned with your Worker, you can generate the env type from the Wrangler configuration and derive the exports from its source module.
Give each Worker a distinct environment interface so the generated declarations can be used together:
npx wrangler types ./workers/api/worker-configuration.d.ts --config ./workers/api/wrangler.jsonc --env-interface ApiEnvyarn wrangler types ./workers/api/worker-configuration.d.ts --config ./workers/api/wrangler.jsonc --env-interface ApiEnvpnpm wrangler types ./workers/api/worker-configuration.d.ts --config ./workers/api/wrangler.jsonc --env-interface ApiEnvRepeat this command for each Worker and include the generated files in the TypeScript configuration for your tests:
{
"include": ["./workers/*/worker-configuration.d.ts", "./tests/**/*.ts"]
}Pass the generated environment interface to server.getWorker(). Use typeof import() to derive the Worker exports from its source module:
const apiWorker = server.getWorker("api-worker");const apiWorker = server.getWorker<
ApiEnv,
typeof import("../workers/api/index")
>("api-worker");In this example, ApiEnv comes from worker-configuration.d.ts. The module type includes the default export and its RPC methods. Re-run wrangler types when the Worker configuration changes.