OpenAICloudflare Developer PlatformJul 27, 2026, 12:00 AM

Workers - Run integration tests against your Worker's production build

A condensed section focused on the key takeaways first.

Original Post

Quick Digest

Summary

A condensed section focused on the key takeaways first.

openaienmodel: gpt-5-mini-2025-08-07

Workers - Run integration tests against your Worker's production build

Key Points

  • createTestHarness() API
  • Local Worker test server
  • Replaces unstable_startWorker()/unstable_dev()

Summary

Wrangler introduces createTestHarness(), an API for running integration tests against Workers built with Wrangler or the Cloudflare Vite plugin. It launches a local Worker server with helpers to dispatch requests, reset storage, and inspect runtime logs so you can test production builds end-to-end from a Node.js test runner.

Key Points

  • createTestHarness({ workers: [...] }) starts a local test server; common lifecycle methods: server.listen(), server.fetch(url), server.reset(), server.close().
  • Specify Workers by wrangler configPath (routes are preserved), allowing routing across multiple Workers in one harness.
  • Integrates with Node.js test runners (Vitest, Jest), request-mocking libraries like MSW, and browser automation (Playwright).
  • Use MSW or other Node HTTP mocking libraries to stub outbound fetch() calls that your Worker makes.
  • createTestHarness() is the recommended approach for integration tests; prefer it over unstable_startWorker() or unstable_dev().
  • For programmatic dev servers, use Vite's createServer() with the Cloudflare Vite plugin instead of the test harness.

For full examples and API details, see the Integration test harness guide in the Wrangler docs.

Full Translation

Translations

A translation section that keeps the flow of the original article.

openaijamodel: gpt-5-mini-2025-08-07

Workers - Worker の本番ビルドに対して統合テストを実行する

2026年7月27日

Wrangler は createTestHarness() を提供するようになりました。これは、Wrangler または Cloudflare Vite plugin でビルドした Worker に対して、任意の Node.js テストランナーから統合テストを実行するための API です。テストハーネスは、リクエストの送信、ストレージのリセット、ランタイムログの検査用のヘルパーを備えたローカル Worker サーバを起動します。これは次のようなテストに便利です:

  • 複数の Worker 間でリクエストをルーティングするテスト
  • MSW のような Node.js リクエストモッキングライブラリを使って outbound fetch() をモックするテスト
  • Worker に対して Playwright テストを実行する場合

例えば、次のテストは 2 つの Worker を起動し、上流 API をモックします。

tests/vitest.test.js

import { 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.ts

import { 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 は統合テストにおいて、unstable_startWorker()unstable_dev() の代わりに createTestHarness() の使用を推奨します。プログラムから開発サーバを起動する場合は、Cloudflare Vite plugin と Vite の createServer() ↗ API を使用してください。createTestHarness() の詳細は Integration test harness guide を参照してください。