Skip to content

[test-harness] Add createTestHarness() for testing a project's production build - #16798

Open
edmundhung wants to merge 2 commits into
edmundhung/miniflare-list-do-ids-by-exportfrom
edmundhung/rm-31812-test-harness
Open

edmundhung wants to merge 2 commits into
edmundhung/miniflare-list-do-ids-by-exportfrom
edmundhung/rm-31812-test-harness

Conversation

@edmundhung

@edmundhung edmundhung commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Part of DEVX-2726 (RM-31812). Stacked on #16879; #16884 adds an example on top.

Adds @cloudflare/test-harness, the cf equivalent of Wrangler's createTestHarness(). It runs a project's Build Output as vite preview does, so tests run the production build. The name is a placeholder. The first commit copies Wrangler's harness and tests unchanged, so the second commit shows what changed:

// Wrangler
createTestHarness({
	workers: [
		{
			configPath: "./web/wrangler.jsonc",
			vars: { GREETING: "Hi" },
			secrets: { API_TOKEN: "test" },
			bindingOverrides: { BROWSER: "mock-browser" },
		},
		{ configPath: "./api/wrangler.jsonc" },
	],
});

// cf
createTestHarness({
	workers: [
		{
			projectDir: "./web",
			// Written as in cloudflare.config.ts, and only for bindings the Worker declares
			envOverrides: {
				GREETING: bindings.text("Hi"),
				API_TOKEN: bindings.text("test"),
				BROWSER: bindings.worker({ worker: "mock-browser" }),
			},
		},
		{ projectDir: "./api" },
		{ projectDir: "./api", name: "admin" }, // another Worker in the same build
		{ config: mockBrowser }, // a test-only Worker, as Miniflare config
	],
});

Worker handles change the same way: types come from the Worker's config module, and the helpers take cf's names and options.

// Wrangler
const api = server.getWorker<Env, typeof import("./api/index.ts")>("api");
const env = await api.getEnv();
const rpc = await api.getExport();
await api.applyD1Migrations("DATABASE"); // migrations config from wrangler.jsonc
await api.getDurableObjectStorage("COUNTER", { name: "user-123" }); // export or binding name
await api.introspectWorkflow("MODERATOR"); // binding name

// cf
const api = server.getWorker<typeof import("./api/cloudflare.config.ts")>("api");
const env = await api.getEnv(); // or getEnv<Env>() to override the inferred type
const rpc = await api.getExport(); // or getExport<{ getMessage(): string }>() to override the inferred type
await api.applyD1Migrations("DATABASE", { migrationsDir: "./migrations" }); // as `cf d1 migrations apply`
await api.getDurableObjectStorage("Counter", { name: "user-123" }); // export name only
await api.introspectWorkflow("ModeratorWorkflow"); // export name only

  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: experimental package; docs and a migration guide come in a follow-up task

@changeset-bot

changeset-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8e3e252

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/test-harness Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ask-bonk

ask-bonk Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

LGTM

github run

@pkg-pr-new

pkg-pr-new Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
@cloudflare/autoconfig

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/autoconfig@16798

@cloudflare/build-output-utils

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/build-output-utils@16798

@cloudflare/codemods

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/codemods@16798

@cloudflare/config

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/config@16798

@cloudflare/containers-shared

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/containers-shared@16798

create-cloudflare

npm i https://pkg.pr.new/cloudflare/workers-sdk/create-cloudflare@16798

@cloudflare/deploy-helpers

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/deploy-helpers@16798

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/kv-asset-handler@16798

miniflare

npm i https://pkg.pr.new/cloudflare/workers-sdk/miniflare@16798

@cloudflare/pages-functions

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/pages-functions@16798

@cloudflare/pages-shared

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/pages-shared@16798

@cloudflare/runtime-types

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/runtime-types@16798

@cloudflare/test-harness

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/test-harness@16798

@cloudflare/unenv-preset

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/unenv-preset@16798

@cloudflare/vite-plugin

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/vite-plugin@16798

@cloudflare/vitest-plugin

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/vitest-plugin@16798

@cloudflare/workers-auth

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/workers-auth@16798

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/workers-editor-shared@16798

@cloudflare/workers-utils

npm i https://pkg.pr.new/cloudflare/workers-sdk/@cloudflare/workers-utils@16798

wrangler

npm i https://pkg.pr.new/cloudflare/workers-sdk/wrangler@16798

commit: 8e3e252

@edmundhung
edmundhung force-pushed the edmundhung/rm-31812-test-harness branch 2 times, most recently from 9c0777c to 325430c Compare November 12, 2026 00:16
@edmundhung
edmundhung force-pushed the edmundhung/rm-31812-test-harness branch from f631654 to a027213 Compare November 13, 2026 00:39
@edmundhung
edmundhung changed the base branch from main to edmundhung/share-cf-config-container-images November 13, 2026 00:39
@edmundhung
edmundhung added this pull request to stack #16885 November 13, 2026 00:51
@edmundhung
edmundhung force-pushed the edmundhung/rm-31812-test-harness branch from a027213 to 4ac6333 Compare November 13, 2026 10:20
@edmundhung
edmundhung force-pushed the edmundhung/rm-31812-test-harness branch from 4ac6333 to 7bdd341 Compare November 13, 2026 10:42
@edmundhung
edmundhung force-pushed the edmundhung/rm-31812-test-harness branch from 7bdd341 to 3a953a1 Compare November 17, 2026 12:18
@edmundhung
edmundhung force-pushed the edmundhung/rm-31812-test-harness branch from 3a953a1 to 3c72a19 Compare November 17, 2026 12:24
Base automatically changed from edmundhung/share-cf-config-container-images to main November 17, 2026 19:16
@edmundhung
edmundhung force-pushed the edmundhung/rm-31812-test-harness branch from 3c72a19 to d7c42a2 Compare November 17, 2026 19:16
@edmundhung
edmundhung force-pushed the edmundhung/rm-31812-test-harness branch from d7c42a2 to fbd3e5c Compare November 18, 2026 16:41
@edmundhung
edmundhung removed this pull request from stack #16885 November 18, 2026 16:44
@edmundhung
edmundhung changed the base branch from main to edmundhung/miniflare-introspect-exported-workflows November 18, 2026 16:44
@edmundhung
edmundhung added this pull request to stack #17018 November 18, 2026 16:44
@edmundhung
edmundhung force-pushed the edmundhung/rm-31812-test-harness branch 4 times, most recently from f311c03 to 6ba7149 Compare November 19, 2026 20:33
@edmundhung
edmundhung marked this pull request as ready for review November 19, 2026 20:35
@workers-devprod
workers-devprod requested review from a team and petebacondarwin and removed request for a team November 19, 2026 20:36
@workers-devprod

workers-devprod commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/new-test-harness-package.md: [@cloudflare/wrangler]
  • packages/test-harness/README.md: [@cloudflare/wrangler]
  • packages/test-harness/package.json: [@cloudflare/wrangler]
  • packages/test-harness/src/tests/auth.test.ts: [@cloudflare/wrangler]
  • packages/test-harness/src/tests/create-test-harness.test.ts: [@cloudflare/wrangler]
  • packages/test-harness/src/auth.ts: [@cloudflare/wrangler]
  • packages/test-harness/src/index.ts: [@cloudflare/wrangler]
  • packages/test-harness/src/runtime.ts: [@cloudflare/wrangler]
  • packages/test-harness/src/test-harness.ts: [@cloudflare/wrangler]
  • packages/test-harness/tsconfig.json: [@cloudflare/wrangler]
  • packages/test-harness/tsdown.config.ts: [@cloudflare/wrangler]
  • packages/test-harness/turbo.json: [@cloudflare/wrangler]
  • packages/test-harness/vitest.config.ts: [@cloudflare/wrangler]
  • pnpm-lock.yaml: [@cloudflare/wrangler]
  • tools/deployments/tests/publish-packages.test.ts: [@cloudflare/wrangler]
  • tools/deployments/tests/validate-changesets.test.ts: [@cloudflare/wrangler]

clanky-the-clanker[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

@edmundhung edmundhung changed the title [test-harness] Add a v0 package that runs Build Output with createTestHarness's API [test-harness] introduce createTestHarness() with build output support Nov 19, 2026
@edmundhung edmundhung changed the title [test-harness] introduce createTestHarness() with build output support [test-harness] Add createTestHarness() for testing a project's production build Nov 19, 2026
devin-ai-integration[bot]

This comment was marked as resolved.

clanky-the-clanker[bot]

This comment was marked as resolved.

@edmundhung
edmundhung marked this pull request as draft November 23, 2026 12:15
@edmundhung
edmundhung force-pushed the edmundhung/rm-31812-test-harness branch from ce905ac to 6452be5 Compare November 23, 2026 12:58
Base automatically changed from edmundhung/share-d1-migrations to main November 23, 2026 14:23
@edmundhung
edmundhung force-pushed the edmundhung/rm-31812-test-harness branch from 6452be5 to 28480c6 Compare November 23, 2026 14:23
…anged

From packages/wrangler/src/api/test-harness.ts and packages/wrangler/e2e/createTestHarness.test.ts, so that the next commit's diff shows what the cf test harness changes.
@edmundhung
edmundhung removed this pull request from stack #17058 November 23, 2026 17:43
@edmundhung
edmundhung changed the base branch from main to edmundhung/miniflare-list-do-ids-by-export November 23, 2026 17:44
@edmundhung
edmundhung force-pushed the edmundhung/rm-31812-test-harness branch from 28480c6 to deef5ab Compare November 23, 2026 17:44
@edmundhung
edmundhung added this pull request to stack #17134 November 23, 2026 17:45
…s API

A v0 package for testing a project's production build with cf. Like
Wrangler's createTestHarness(), it runs the project's Workers in tests, but
from its build output. This commit's diff shows what changes from Wrangler's
harness, and why:

- Each Worker runs from a project's Build Output (`projectDir`, and `name` for
  a build's other Workers) rather than a Wrangler config, as in
  `vite preview`: secrets come from the build mode's `.dev.vars` and `.env`
  files, remote bindings are on, and Containers run from the build's images.
- Test values go in one `envOverrides` option, written as in
  cloudflare.config.ts, instead of `vars`, `secrets` and `bindingOverrides`.
  As in production, it only replaces bindings the Worker declares, each with
  one that can stand in for it.
- applyD1Migrations() takes the `dir`, `pattern` and `table` options of
  `cf d1 migrations apply`, as Build Output has no migrations config, and
  applies them with workers-utils' readD1Migrations() and applyD1Migrations().
- The Durable Object and Workflow helpers take the export name, since a Worker
  reaches its own through `ctx.exports`. Workflows are introspected through
  Miniflare's unsafeIntrospectWorkflow(), so they need no binding.
- Test-only Workers can be inline, as Miniflare's Worker config, which
  Miniflare runs as given.
- getWorker() is typed only from a Worker's config module. Without one, its
  env and export are `any`, and getEnv() and getExport() take types directly.
- update() can change the number of Workers, as the harness updates one
  Miniflare instance, and remote sessions follow each Worker by name.
- As in production, an uncaught Worker error is a 500 response, and
  CF-Worker names the Worker's zone.
@edmundhung
edmundhung force-pushed the edmundhung/rm-31812-test-harness branch from deef5ab to 8e3e252 Compare November 23, 2026 20:19
@edmundhung
edmundhung marked this pull request as ready for review November 23, 2026 20:20

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No blocking, warning or open findings at this commit. The earlier suggestion was answered by the author, and the code matches the answer.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🚀 Release: ✅ LGTM

Overall risk: Low.

  • Changeset: The pull request adds an experimental, test-only package with a minor changeset, which matches the repository's guidance for new experimental features.
  • Publishing: The package is registered in the tests for package publishing and changeset validation.

🔒 Security: ✅ LGTM

The harness is a local test tool. It runs Miniflare on 127.0.0.1 against the developer's own build output.

  • Credentials: Auth uses the cf profile and fails closed in non-interactive or CI contexts. No secrets are logged.
  • Remote-bindings key: The key contains no secret values.
  • Dependencies: All dependencies are workspace packages or catalog entries.

📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found in the changed code.

  • Remote sessions: resolveWorkerInputs and updateConfig dispose remote sessions on every failure path I traced. These cover a failed resolve, a failed setOptions, and a Worker removed or replaced with an inline one.
  • Container images: The image-preparation cache belongs to each harness.
  • Tests: They now cover the remote-bindings auth hook, session disposal, envOverrides validation and the new handle APIs.
  • Eager profile resolution: Resolving the cf profile for every Build Output Worker is intended, as the author explained in the discussion.

⚡ Performance: ✅ LGTM

No performance problems found.

  • Worker resolution: Workers are resolved one at a time on purpose, so a failure leaves no remote session still starting. The cost is small for a test harness.
  • Image cache: Image preparation is cached per harness.
  • Session cleanup: Remote sessions are disposed on startup failure, on teardown and in update().

📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 8e3e2527

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Untriaged

Development

Successfully merging this pull request may close these issues.

2 participants