Get started (58)
Last updated Jul 27, 2026
This guide shows how to write a basic integration test for a Worker with createTestHarness(). The example uses Vitest as the test runner and exercises a Worker built with Wrangler.
Prerequisites
You need:
- A Worker project with a Wrangler configuration file
- A Node.js test runner such as Vitest ↗︎
wranglerinstalled as a development dependency
Create a test harness
Import createTestHarness() from wrangler. Point the test harness at your Worker configuration file.
test/index.test.jsjs
import { createTestHarness } from "wrangler";
const server = createTestHarness({
workers: [{ configPath: "./wrangler.jsonc" }],
});
test/index.test.tsts
import { createTestHarness } from "wrangler";
const server = createTestHarness({
workers: [{ configPath: "./wrangler.jsonc" }],
});
Manage the test harness lifecycle
For simplicity, we will reuse a single server for the test suite and reset it after each test. You can also start a new server for each test if the tests do not share the same configuration.
test/index.test.jsjs
import { afterAll, afterEach, beforeAll } from "vitest";
beforeAll(async () => {
// Start the server before all tests
await server.listen();
});
afterEach(async () => {
// Recreates storage and restores the original Worker options after each test
await server.reset();
});
afterAll(async () => {
// Close the server after all tests
await server.close();
});
test/index.test.tsts
import { afterAll, afterEach, beforeAll } from "vitest";
beforeAll(async () => {
// Start the server before all tests
await server.listen();
});
afterEach(async () => {
// Recreates storage and restores the original Worker options after each test
await server.reset();
});
afterAll(async () => {
// Close the server after all tests
await server.close();
});
Write your first test
Use the helpers provided by the test harness to interact with the Worker and assert its behavior. For example, you can call server.fetch() to send a request to the Worker and assert against its response.
test/index.test.jsjs
import { test } from "vitest";
test("responds", async ({ expect }) => {
const response = await server.fetch("/");
expect(await response.text()).toBe("Hello World");
});
test/index.test.tsts
import { test } from "vitest";
test("responds", async ({ expect }) => {
const response = await server.fetch("/");
expect(await response.text()).toBe("Hello World");
});
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/workers/testing/test-harness/get-started/#page","headline":"Get started","description":"Write your first integration test for a Cloudflare Worker with createTestHarness.","url":"https://developers.cloudflare.com/workers/testing/test-harness/get-started/","inLanguage":"en","image":"https://developers.cloudflare.com/workers/testing/test-harness/get-started/og.png?v=2eb6967b7d478d0d","dateModified":"2026-07-27","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
description: Use createTestHarness with Mock Service Worker and Playwright. title: Integrations image: https://developers.cloudflare.com/workers/testing/test-harness/integrations/og.png?v=d180095af1637ae1
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/workers/llms.txt
Use this file to discover all available pages before exploring further.
Docs