Skip to content

Instantly share code, notes, and snippets.

@up1
Created August 3, 2026 07:07
Show Gist options
  • Select an option

  • Save up1/5bfcc67ad122e0e3514925729ec36736 to your computer and use it in GitHub Desktop.

Select an option

Save up1/5bfcc67ad122e0e3514925729ec36736 to your computer and use it in GitHub Desktop.
Cloudflare Test Harness
import { createTestHarness } from "wrangler";
import { beforeAll, afterEach, afterAll, test } from "vitest";
// Initialize the test harness pointing to your Cloudflare Worker config
const server = createTestHarness({
workers: [
{ configPath: "./wrangler.jsonc" } // or wrangler.toml
],
});
// Manage the server lifecycle across your test suite
beforeAll(async () => {
await server.listen();
});
afterEach(async () => {
// Resets storage bindings and restores initial state after each test
await server.reset();
});
afterAll(async () => {
await server.close();
});
// Write your actual test cases
test("responds with Hello World", async ({ expect }) => {
// server.fetch() sends a request straight to your built Worker code
const response = await server.fetch("/");
expect(response.status).toBe(200);
expect(await response.text()).toBe("Hello World");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment