Skip to content

Latest commit

 

History

History

README.md

Integration testing Workers with createTestHarness()

This fixture is the complete runnable example linked from the createTestHarness() docs. It shows how to test a realistic multi-Worker app's production build output from different test runners.

For the testing patterns demonstrated here, refer to the createTestHarness() guide.

Example app

The app has three Workers:

Worker Route Role
web-worker example.com/* Handles user-facing routes, calls the API Worker over a service binding, and uses Browser Rendering to generate previews.
api-worker api.example.com/v1/* Fetches upstream user data, caches it in KV, and stores scheduled reports in D1.
mock-browser none Test-only Worker used to replace the Web Worker's Browser Rendering binding.

What this fixture covers

  • Setup Vitest against prebuilt output from Wrangler projects.
  • Setup Playwright against Workers built by the Cloudflare Vite plugin.
  • Route dispatch across multiple Workers.
  • Direct Worker calls with server.getWorker(name).
  • D1 migration setup with worker.applyD1Migrations().
  • Scheduled handler dispatch.
  • Test-only binding overrides for platform bindings.
  • Calling a Worker's default export with server.getWorker(name).getExport().
  • Outbound request mocking with MSW.
  • Local storage reset between tests.
  • Debug output with server.debug().
  • Runtime log assertions with server.getLogs() and server.clearLogs().

Run this example

To build the Wrangler and Vite output, run:

pnpm build --filter @fixture/create-test-harness-example

This builds each Wrangler Worker once with wrangler deploy --dry-run --outdir, then the Vitest setup reuses that output with prebuiltWorkerDir. It also creates the Vite output used by Playwright.

Then run the tests with:

pnpm --filter @fixture/create-test-harness-example test:vitest
pnpm --filter @fixture/create-test-harness-example test:playwright

Files

File Purpose
tests/vitest.test.ts Tests prebuilt Wrangler output with Vitest.
tests/playwright.test.ts Tests Vite build output with Playwright.
vite.config.ts Builds Vite-generated Worker configs.
workers/web/index.ts User-facing Worker with service and Browser Rendering binding logic.
workers/web/wrangler.jsonc Web Worker config.
workers/web/worker-configuration.d.ts Generated Web Worker types.
workers/api/index.ts API Worker with KV, D1, and scheduled job logic.
workers/api/wrangler.jsonc API Worker config.
workers/api/worker-configuration.d.ts Generated API Worker types.
workers/mock-browser/index.ts Test-only mock Worker for the Browser Rendering binding.
workers/mock-browser/wrangler.jsonc Mock Browser Worker config.

Related docs