Skip to content

[vitest-pool-workers] Add regression test for WorkerEntrypoint env mocking#13084

Merged
penalosa merged 1 commit intomainfrom
penalosa/vitest-7077
Mar 31, 2026
Merged

[vitest-pool-workers] Add regression test for WorkerEntrypoint env mocking#13084
penalosa merged 1 commit intomainfrom
penalosa/vitest-7077

Conversation

@penalosa
Copy link
Copy Markdown
Contributor

@penalosa penalosa commented Mar 27, 2026

Fixes #7077.

The underlying issue — WorkerEntrypoint's native constructor rejecting a user-provided ctx object — was fixed in workerd via cloudflare/workerd#3782, which relaxed the constructor parameter from jsg::Ref<ExecutionContext> to jsg::JsObject. This has been shipping since workerd@1.20250324.0.

This PR adds a regression test that constructs a WorkerEntrypoint subclass with createExecutionContext() and a mocked KVNamespace, confirming the pattern works.


  • 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: no user-facing change, the fix already shipped in workerd

Open with Devin

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 27, 2026

⚠️ No Changeset found

Latest commit: 3d614ab

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

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

@ask-bonk
Copy link
Copy Markdown
Contributor

ask-bonk bot commented Mar 27, 2026

OK, the WorkerEntrypoint constructor takes (ctx, env) — this is the standard workerd convention. Let me verify by looking at the PR description and the workerd fix more carefully.

The PR description says the issue (#7077) was about WorkerEntrypoint's native constructor rejecting a user-provided ctx object, and that it was fixed in workerd. The test constructs:

const worker = new TestDefaultEntrypoint(ctx, { ...env, KV_NAMESPACE: mockKv } as Env);

And TestDefaultEntrypoint extends TestSuperEntrypoint extends WorkerEntrypoint<Env>. The WorkerEntrypoint constructor takes (ctx, env). This looks correct.

Let me verify the test logic is sound:

  • Creates a fake KV with "mocked-key" -> "mocked-value"
  • Creates a mock KVNamespace with get and put
  • Gets ctx from createExecutionContext()
  • Creates a TestDefaultEntrypoint with the mocked env
  • Calls worker.read("mocked-key") which calls this.env.KV_NAMESPACE.get(key) — this should return "mocked-value" from the mock

Everything checks out. The test is clean, minimal, correctly structured, and properly tests the regression. No logic bugs, no security issues, no backward compatibility concerns.

LGTM

github run

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 27, 2026

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@13084

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@13084

miniflare

npm i https://pkg.pr.new/miniflare@13084

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@13084

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@13084

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@13084

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@13084

@cloudflare/workers-editor-shared

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

wrangler

npm i https://pkg.pr.new/wrangler@13084

commit: 3d614ab

@penalosa penalosa force-pushed the penalosa/vitest-7077 branch from 327c4ed to f8b1db5 Compare March 27, 2026 00:16
@edmundhung edmundhung self-requested a review March 30, 2026 12:03
Comment thread fixtures/vitest-pool-workers-examples/rpc/test/unit.test.ts Outdated
…ypoint with mocked env

Closes #7077. The underlying fix landed in workerd via
cloudflare/workerd#3782, which relaxed the WorkerEntrypoint
constructor to accept any object as ctx rather than requiring
a native ExecutionContext.
@penalosa penalosa force-pushed the penalosa/vitest-7077 branch from f8b1db5 to 3d614ab Compare March 31, 2026 15:16
Copy link
Copy Markdown
Member

@dario-piotrowicz dario-piotrowicz left a comment

Choose a reason for hiding this comment

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

LGTM

@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Mar 31, 2026
@penalosa penalosa merged commit 7b2ecc6 into main Mar 31, 2026
51 checks passed
@penalosa penalosa deleted the penalosa/vitest-7077 branch March 31, 2026 17:14
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Mar 31, 2026
petebacondarwin pushed a commit to nekoze1210/workers-sdk that referenced this pull request Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

🐛 BUG: Can't mock env when authoring a WorkerEntrypoint

4 participants