Skip to content

[vitest-pool-workers] Adopt @msw/cloudflare and remove MSW workarounds - #13830

Open
penalosa wants to merge 8 commits into
mainfrom
penalosa/msw-cloudflare
Open

[vitest-pool-workers] Adopt @msw/cloudflare and remove MSW workarounds#13830
penalosa wants to merge 8 commits into
mainfrom
penalosa/msw-cloudflare

Conversation

@penalosa

@penalosa penalosa commented May 6, 2026

Copy link
Copy Markdown
Contributor

Migrates the request-mocking example fixture to use the new official @msw/cloudflare integration, and drops two longstanding workarounds that were only there to make MSW work inside workerd:

  • The msw/lib/node/index.mjsmsw/native remap in packages/vitest-pool-workers/src/pool/plugin.ts (added back when setupServer() from msw/node couldn't run in workerd).
  • The globalThis.fetch = (...) => originalFetch.call(...) self-rebinding in packages/vitest-pool-workers/src/worker/fetch-mock.ts (added so MSW's FetchInterceptor could replace a configurable fetch global).

With MSW >= 2.14 and @mswjs/interceptors >= 0.41, neither workaround is required: the interceptor's applyPatch handles non-configurable-but-writable globals via the owner[key] = ... fallback, and the MSW Cloudflare integration provides a setupNetwork() API designed to run directly inside workerd.

Other changes:

  • Bumps the catalog msw version from 2.12.4 to ^2.14.3 to satisfy @msw/cloudflare's >=2.14.1 peer dep.
  • Adds @msw/cloudflare as a dev dep on the fixtures workspace (installed from pkg.pr.new since the package isn't yet on the npm registry).
  • Replaces request-mocking/test/imperative.test.ts (which used vi.spyOn(globalThis, "fetch") to mock GET/POST/WebSocket) with request-mocking/test/websocket.test.ts, which demonstrates declarative outbound-WebSocket mocking using MSW's ws.link API. The GET/POST cases are already covered by the updated declarative.test.ts.

The new request-mocking fixture exercises the full @msw/cloudflare surface area used in workerd:

Test Pattern
declarative.test.ts http.get / http.post handlers via setupNetwork().use(...)
websocket.test.ts Outbound new WebSocket(url) intercepted via ws.link(...) + addEventListener("connection", ...)

  • Tests
    • Tests included/updated
  • Public documentation
    • Documentation not necessary because: this PR removes internal hacks and updates an example fixture; no user-facing API changes. The @msw/cloudflare integration itself is documented at https://github.com/mswjs/cloudflare.

@changeset-bot

changeset-bot Bot commented May 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6a678d1

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

This PR includes changesets to release 1 package
Name Type
@cloudflare/vitest-pool-workers 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

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

✅ All changesets look good

@ask-bonk

ask-bonk Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

UnknownError: ProviderInitError

github run

@ask-bonk

ask-bonk Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

@penalosa Bonk workflow failed. Check the logs for details.

View workflow run · To retry, trigger Bonk again.

@pkg-pr-new

pkg-pr-new Bot commented May 6, 2026

Copy link
Copy Markdown
@cloudflare/autoconfig

npm i https://pkg.pr.new/@cloudflare/autoconfig@13830

@cloudflare/build-output-utils

npm i https://pkg.pr.new/@cloudflare/build-output-utils@13830

@cloudflare/config

npm i https://pkg.pr.new/@cloudflare/config@13830

create-cloudflare

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

@cloudflare/deploy-helpers

npm i https://pkg.pr.new/@cloudflare/deploy-helpers@13830

@cloudflare/kv-asset-handler

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

miniflare

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

@cloudflare/pages-functions

npm i https://pkg.pr.new/@cloudflare/pages-functions@13830

@cloudflare/pages-shared

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

@cloudflare/unenv-preset

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

@cloudflare/vite-plugin

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

@cloudflare/vitest-pool-workers

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

@cloudflare/workers-auth

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

@cloudflare/workers-editor-shared

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

@cloudflare/workers-utils

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

wrangler

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

commit: 6a678d1

@penalosa
penalosa force-pushed the penalosa/msw-cloudflare branch 3 times, most recently from 29932c6 to 8555c1a Compare May 12, 2026 15:16
@penalosa

penalosa commented Jun 1, 2026

Copy link
Copy Markdown
Contributor Author

/bonk Please merge in origin/main and resolve the conflicts in this PR. Also update to the latest version of @msw/cloudflare

@ask-bonk

ask-bonk Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

To https://github.com/cloudflare/workers-sdk.git
! [rejected] penalosa/msw-cloudflare -> penalosa/msw-cloudflare (non-fast-forward)
error: failed to push some refs to 'https://github.com/cloudflare/workers-sdk.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

github run

@ask-bonk

ask-bonk Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

@penalosa Bonk workflow failed. Check the logs for details.

View workflow run · To retry, trigger Bonk again.

@penalosa
penalosa force-pushed the penalosa/msw-cloudflare branch from 8555c1a to 300d0c9 Compare June 1, 2026 16:18
penalosa added 2 commits June 2, 2026 17:52
Remove the internal msw/node → msw/native remap and globalThis.fetch
rebinding hacks from vitest-pool-workers. Recent MSW versions (2.14+)
no longer need either workaround.

The request-mocking fixture now uses the official @msw/cloudflare
integration via setupNetwork() and covers three call patterns: direct
fetch from the runner DO, exports.default.fetch (which dispatches into
a fresh request I/O context), and WebSocket interception.

Dependencies:
  - msw bumped to ^2.14.6 (stable npm release; the cross-context
    AbortSignal fix landed in 2.14.4)
  - @msw/cloudflare tracks the pkg.pr.new @beta tag, which adds
    exports.default.fetch support
…tion

Vitest 4.1's bundled type chunks broke the previous `declare module "vitest" { interface ProvidedContext {} }` pattern when the file only contained the augmentation block. Adding an explicit `import "vitest"` makes the augmentation reach the chunk's interface declaration.
@penalosa
penalosa force-pushed the penalosa/msw-cloudflare branch from 300d0c9 to 8f04aba Compare June 2, 2026 17:20
@penalosa
penalosa marked this pull request as ready for review June 4, 2026 18:05
@penalosa
penalosa requested a review from workers-devprod as a code owner June 4, 2026 18:05
@workers-devprod
workers-devprod requested review from a team and petebacondarwin and removed request for a team June 4, 2026 18:05
@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/msw-cloudflare-integration.md: [@cloudflare/wrangler]
  • fixtures/vitest-pool-workers-examples/hyperdrive/env.d.ts: [@cloudflare/wrangler]
  • fixtures/vitest-pool-workers-examples/package.json: [@cloudflare/wrangler]
  • fixtures/vitest-pool-workers-examples/request-mocking/README.md: [@cloudflare/wrangler]
  • fixtures/vitest-pool-workers-examples/request-mocking/src/index.ts: [@cloudflare/wrangler]
  • fixtures/vitest-pool-workers-examples/request-mocking/test/declarative.test.ts: [@cloudflare/wrangler]
  • fixtures/vitest-pool-workers-examples/request-mocking/test/direct.test.ts: [@cloudflare/wrangler]
  • fixtures/vitest-pool-workers-examples/request-mocking/test/exports.test.ts: [@cloudflare/wrangler]
  • fixtures/vitest-pool-workers-examples/request-mocking/test/imperative.test.ts: [@cloudflare/wrangler]
  • fixtures/vitest-pool-workers-examples/request-mocking/test/server.ts: [@cloudflare/wrangler]
  • fixtures/vitest-pool-workers-examples/request-mocking/test/setup.ts: [@cloudflare/wrangler]
  • fixtures/vitest-pool-workers-examples/request-mocking/test/websocket.test.ts: [@cloudflare/wrangler]
  • packages/vitest-pool-workers/src/pool/plugin.ts: [@cloudflare/wrangler]
  • packages/vitest-pool-workers/src/worker/fetch-mock.ts: [@cloudflare/wrangler]
  • packages/vitest-pool-workers/src/worker/lib/cloudflare/test-internal.ts: [@cloudflare/wrangler]
  • pnpm-lock.yaml: [@cloudflare/wrangler]
  • pnpm-workspace.yaml: [@cloudflare/wrangler]

devin-ai-integration[bot]

This comment was marked as resolved.

@petebacondarwin petebacondarwin 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.

The Devin issue appears to be valid? Do you just need to update the README if they bug is resolved?

@github-project-automation github-project-automation Bot moved this from Untriaged to In Review in workers-sdk Jun 5, 2026
@workers-devprod

workers-devprod commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • ✅ @cloudflare/wrangler
Show detailed file reviewers

devin-ai-integration[bot]

This comment was marked as resolved.

@penalosa
penalosa requested review from a team and petebacondarwin August 4, 2026 21:09

@workers-devprod workers-devprod 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.

Codeowners reviews satisfied

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

Labels

None yet

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

4 participants