70bf21e064
Handle Changesets / Handle Changesets (push) Waiting to run
Semgrep OSS scan / semgrep-oss (push) Waiting to run
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
4.5 KiB
4.5 KiB
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() examples 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 Workers developed with Wrangler.
- 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()andserver.clearLogs().
Run this example
To build the packages, run:
pnpm build --filter @fixture/create-test-harness-example
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 |
Example for testing with Vitest. |
tests/playwright.test.ts |
Example for testing 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. |