[Workers] integration testing guide with createTestHarness()#31377
[Workers] integration testing guide with createTestHarness()#31377edmundhung wants to merge 1 commit into
createTestHarness()#31377Conversation
|
This pull request requires reviews from CODEOWNERS as it changes files that match the following patterns:
|
There was a problem hiding this comment.
Posted 5 inline suggestions and flagged 1 issue requiring maintainer action.
HIGH — Style guide violations:
write-your-first-test.mdxline 26: Baretsfence for a Workers example. Must useTypeScriptExample. (inline suggestion provided)recipes.mdxlines 22–184: Multiple baretsfences for Workers examples. Must useTypeScriptExample. (inline suggestions provided for representative blocks)recipes.mdxlines 112–129: Barejsoncfences for Wrangler config. Must useWranglerConfigwith TOML input and$todayforcompatibility_date. (inline suggestions provided)
MEDIUM — Missing changelog:
- This PR introduces a new major feature with 3 new docs pages. Consider adding a changelog entry in
src/content/changelog/workers/.
Out of scope — migrate-from-unstable-dev.mdx has a pre-existing invalid pcx_content_type: migration (not in the allowed values list).
| products: | ||
| - workers | ||
| --- | ||
|
|
There was a problem hiding this comment.
Add the required component import:
| import { TypeScriptExample } from "~/components"; |
| ```ts title="test/index.test.ts" | ||
| import { afterAll, afterEach, beforeAll, test } from "vitest"; | ||
| import { createTestHarness } from "wrangler"; | ||
|
|
||
| const server = createTestHarness({ | ||
| workers: [{ configPath: "./wrangler.jsonc" }], | ||
| }); | ||
|
|
||
| beforeAll(async () => { | ||
| await server.listen(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await server.reset(); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await server.close(); | ||
| }); | ||
|
|
||
| test("responds", async ({ expect }) => { | ||
| const response = await server.fetch("/"); | ||
| expect(await response.text()).toBe("Hello World"); | ||
| }); | ||
| ``` |
There was a problem hiding this comment.
Use TypeScriptExample for all Workers JS/TS examples:
| ```ts title="test/index.test.ts" | |
| import { afterAll, afterEach, beforeAll, test } from "vitest"; | |
| import { createTestHarness } from "wrangler"; | |
| const server = createTestHarness({ | |
| workers: [{ configPath: "./wrangler.jsonc" }], | |
| }); | |
| beforeAll(async () => { | |
| await server.listen(); | |
| }); | |
| afterEach(async () => { | |
| await server.reset(); | |
| }); | |
| afterAll(async () => { | |
| await server.close(); | |
| }); | |
| test("responds", async ({ expect }) => { | |
| const response = await server.fetch("/"); | |
| expect(await response.text()).toBe("Hello World"); | |
| }); | |
| ``` | |
| <TypeScriptExample filename="test/index.test.ts"> | |
| ```ts | |
| import { afterAll, afterEach, beforeAll, test } from "vitest"; | |
| import { createTestHarness } from "wrangler"; | |
| const server = createTestHarness({ | |
| workers: [{ configPath: "./wrangler.jsonc" }], | |
| }); | |
| beforeAll(async () => { | |
| await server.listen(); | |
| }); | |
| afterEach(async () => { | |
| await server.reset(); | |
| }); | |
| afterAll(async () => { | |
| await server.close(); | |
| }); | |
| test("responds", async ({ expect }) => { | |
| const response = await server.fetch("/"); | |
| expect(await response.text()).toBe("Hello World"); | |
| }); |
| products: | ||
| - workers | ||
| --- | ||
|
|
There was a problem hiding this comment.
Add the required component imports:
| import { TypeScriptExample, WranglerConfig } from "~/components"; |
| ```ts | ||
| const server = createTestHarness({ | ||
| workers: [ | ||
| { configPath: "./wrangler.api.jsonc" }, | ||
| { configPath: "./dist/web_worker/wrangler.json" }, | ||
| ], | ||
| }); | ||
| ``` |
There was a problem hiding this comment.
Use TypeScriptExample instead of a bare ts fence:
| ```ts | |
| const server = createTestHarness({ | |
| workers: [ | |
| { configPath: "./wrangler.api.jsonc" }, | |
| { configPath: "./dist/web_worker/wrangler.json" }, | |
| ], | |
| }); | |
| ``` | |
| <TypeScriptExample> | |
| ```ts | |
| const server = createTestHarness({ | |
| workers: [ | |
| { configPath: "./wrangler.api.jsonc" }, | |
| { configPath: "./dist/web_worker/wrangler.json" }, | |
| ], | |
| }); |
| ```jsonc title="wrangler.web.jsonc" | ||
| { | ||
| "name": "web-worker", | ||
| "main": "src/web.ts", | ||
| "compatibility_date": "2026-06-01", | ||
| "routes": ["example.com/*"], | ||
| "services": [{ "binding": "API", "service": "api-worker" }] | ||
| } | ||
| ``` |
There was a problem hiding this comment.
Use WranglerConfig with TOML input and $today for Wrangler configuration examples:
| ```jsonc title="wrangler.web.jsonc" | |
| { | |
| "name": "web-worker", | |
| "main": "src/web.ts", | |
| "compatibility_date": "2026-06-01", | |
| "routes": ["example.com/*"], | |
| "services": [{ "binding": "API", "service": "api-worker" }] | |
| } | |
| ``` | |
| <WranglerConfig> | |
| ```toml | |
| name = "web-worker" | |
| main = "src/web.ts" | |
| compatibility_date = "$today" | |
| routes = ["example.com/*"] | |
| [[services]] | |
| binding = "API" | |
| service = "api-worker" |
| ```jsonc title="wrangler.api.jsonc" | ||
| { | ||
| "name": "api-worker", | ||
| "main": "src/api.ts", | ||
| "compatibility_date": "2026-06-01", | ||
| "routes": ["example.com/api/*"] | ||
| } | ||
| ``` |
There was a problem hiding this comment.
Use WranglerConfig with TOML input and $today for Wrangler configuration examples:
| ```jsonc title="wrangler.api.jsonc" | |
| { | |
| "name": "api-worker", | |
| "main": "src/api.ts", | |
| "compatibility_date": "2026-06-01", | |
| "routes": ["example.com/api/*"] | |
| } | |
| ``` | |
| <WranglerConfig> | |
| ```toml | |
| name = "api-worker" | |
| main = "src/api.ts" | |
| compatibility_date = "$today" | |
| routes = ["example.com/api/*"] |
|
I have completed the review of PR #31377. Actions taken:
What the PR changes: Issues flagged:
I posted inline suggestions for the most representative violations. The author should apply the |
Summary
Adding some docs with regards to the new
createTestHarness()API, which is now our recommendation for integration tests. Whilevitest-pool-workersremains recommended for unit tests.Screenshots (optional)
Documentation checklist