Skip to content

test: Start MSW worker once per file instead of per test#229

Open
Amund211 wants to merge 1 commit into
mainfrom
amund/msw-start-once-per-file
Open

test: Start MSW worker once per file instead of per test#229
Amund211 wants to merge 1 commit into
mainfrom
amund/msw-start-once-per-file

Conversation

@Amund211

Copy link
Copy Markdown
Owner

Summary

  • Move `worker.start()` out of the per-test `mswTest` fixture and into a `beforeAll` in the existing per-file setup file (`src/test/setup.ts`).
  • `resetHandlers()` still runs in `afterEach` so tests stay isolated.
  • This matches the pattern in the official MSW recipe for vitest browser mode and removes one service-worker registration per test (~175 tests × 2 browsers).

Expected to shave noticeable time off the UI test run, most visibly on webkit (which runs with `--no-file-parallelism` in CI and sits on the critical path).

Test plan

  • `pnpm run test:unit` — all 475 unit tests pass locally.
  • `pnpm run lint:check` and `pnpm run tsc` pass.
  • CI UI tests pass on chromium and webkit.
  • Compare webkit test step duration on this PR vs recent main runs.

🤖 Generated with Claude Code

Previously the mswTest fixture called worker.start() before every test.
Each call re-registers the service worker and round-trips through the
browser — cheap, but it runs once per test × ~175 UI tests × 2 browsers,
and adds measurable overhead on the critical path (webkit, which runs
serially in CI).

Move the start() call to a beforeAll hook in the existing per-file
vitest setup file. resetHandlers() in afterEach still isolates tests
from each other. Matches the pattern in the official MSW recipe for
vitest browser mode (https://mswjs.io/docs/recipes/vitest-browser-mode).

The mswTest fixture is kept so existing test imports still work and so
tests can still receive `worker` as a context param if they need it.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 23, 2026 07:55
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying rainbow with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2ed936c
Status: ✅  Deploy successful!
Preview URL: https://af4c93c3.rainbow-ctx.pages.dev
Branch Preview URL: https://amund-msw-start-once-per-fil.rainbow-ctx.pages.dev

View logs

Copilot AI 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.

Pull request overview

This PR optimizes the UI (Vitest browser/Playwright) test runtime by avoiding repeated MSW service worker startup per test, while keeping per-test handler isolation.

Changes:

  • Start the MSW worker in beforeAll via the UI test setup file (setupFiles) instead of inside the per-test mswTest fixture.
  • Reset MSW handlers in afterEach to preserve test isolation.
  • Simplify the mswTest fixture to only expose the shared worker in test context.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/test/setup.ts Starts MSW worker once via beforeAll and resets handlers via afterEach for UI tests.
src/test/msw-test.ts Removes per-test worker startup/teardown and keeps only the context fixture exposure.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

2 participants