Skip to content

chore: migrate tests to vitest 4 and the Cloudflare vitest plugin - #8

Merged
mroderick merged 4 commits into
mainfrom
test-stack-migration
Nov 1, 2026
Merged

mroderick merged 4 commits into
mainfrom
test-stack-migration

Conversation

@mroderick

Copy link
Copy Markdown
Contributor

Summary

Migrates the test suite from vitest 3 + @cloudflare/vitest-pool-workers to vitest 4 + @cloudflare/vitest-plugin plus MSW for outbound request mocking, ahead of the invite_outcome logging work (#1 in the stack, this is the dependency).

  • cloudflare:test loses fetchMock under the plugin; the mocking mechanics lived only in test/helpers.js, so the per-test files keep calling the same helpers.
  • Unmatched outbound requests fail the owning test again, via a setup-level guard that replicates the old fetchMock.disableNetConnect() loudness.

Review notes

Mechanics to check

  • One-shot semantics: old stacked undici interceptors are now { once: true } MSW handlers (first-in/first-out) in test/helpers.js.
  • Transport-level failure simulation: HttpResponse.error() leaked unhandled rejections and throw in handlers surfaced as 500 responses instead of rejected fetches, so unreachable-upstream mocks use an unparseable 599 reply; the two affected test names now say "fails before a JSON answer".
  • Assets: the plugin dispatches SELF/exports.default straight to the Worker, with no asset router in front. test/static.test.js asserts the join page through the ASSETS binding instead.
  • package-lock.json dominates the diff; dependency lines in package.json tell the story.

Not done here

  • No production code changes; the invite_outcome logging needs this stack first (next PR in the stack).

@cloudflare/vitest-pool-workers is replaced by @cloudflare/vitest-plugin
(claimed by its upstream codemod path). The plugin drops fetchMock from
cloudflare:test, so outbound mocks move to MSW (msw 3 + @msw/cloudflare).
helpers.js is the only place the mocking mechanics lived, so rewrite tests
stayed minimal; Semantics preserved via one-shot `once: true` handlers and a
setup-level guard that fails tests on unmocked outbound requests.

Two behavioural adaptations the new runtime requires:

- Static assets are no longer routed in front of the Worker under SELF; the
  static test now asserts the content through the ASSETS binding, matching
  what production serves.
- Transport-level failure simulation moved from HttpResponse.error to an
  unparseable 599 upstream body, because HttpResponse.error leaves unhandled
  rejections (red CI) and throwing handlers surface as 500 responses instead
  of rejected fetches. Both drive the same before-JSON branches the worker
  handles, and the affected test names now say so.
- Rewritten webhook-failure test now asserts the delivery attempt (one call,
  token-failure text kept) instead of passing with zero assertions; the test
  name matches the non-2xx branch it exercises, and the fetch-rejection
  branch is documented as not representable under MSW.
- GET / routing is pinned again at config level (vitest.node.config.js +
  test/config-pin.test.js): wrangler.jsonc must keep the join page on the
  asset router (directory -> public, no run_worker_first) and the files must
  exist, so a production routing regression cannot pass the suite.
- Unmatched outbound requests are refused by the network itself via
  network.configure({ onUnhandledFrame: 'error' }) instead of passing
  through to the real upstreams mid-test; afterEach stays as a backstop and
  the tracked list resets per test.
- mockUsersList reads the expected Authorization value from the same shared
  constant the configs bind as SLACK_TOKEN (test/tokens.js); the dead
  'headers' argument is gone from the call site.
- The 599 'fails before a JSON answer' cases duplicated existing non-JSON
  mocks under MSW; removed with their now-unused helpers.
@mroderick
mroderick marked this pull request as ready for review November 1, 2026 20:48
@mroderick
mroderick added this pull request to stack #10 November 1, 2026 20:48
Deploy.yml only tests pushes to main, so PRs had no check at all. This adds
a test-only CI workflow for pull requests that mirrors the deploy job's
steps (node 22, npm ci, npm test). Main keeps its gate from the deploy
workflow, so there is no duplicated main-branch run.
The migration lockfile was written by npm from the local Node 26 install;
npm ci under the CI's node 22 reported missing transitive entries. The
lock is now generated and verified under node 22 (npm 10.9.9), the same
pair the workflows pin.
@mroderick
mroderick merged commit a539956 into main Nov 1, 2026
1 check passed
@mroderick
mroderick deleted the test-stack-migration branch November 1, 2026 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant