Skip to content

fix(browser): reject duplicate setupWorker calls#2681

Open
trknhr wants to merge 2 commits intomswjs:mainfrom
trknhr:fix-setup-worker-singleton-guard
Open

fix(browser): reject duplicate setupWorker calls#2681
trknhr wants to merge 2 commits intomswjs:mainfrom
trknhr:fix-setup-worker-singleton-guard

Conversation

@trknhr
Copy link
Copy Markdown
Contributor

@trknhr trknhr commented Mar 23, 2026

Reject duplicate setupWorker() calls in the same page by guarding initialization with a page-global symbol. This makes the failure explicit before multiple worker instances can attach conflicting browser-side listeners.

The browser worker state is shared per page, so creating multiple setupWorker instances is ambiguous and leads to clashing request handling. Failing fast here keeps setupWorker() as a single-construction API and points users toward reusing the existing worker with worker.use() or worker.resetHandlers().

This also adds a browser regression test for the duplicate call path while asserting only on a stable error prefix so the developer-facing guidance can still evolve.

Fixes #2585

trknhr added 2 commits March 24, 2026 02:34
Guard setupWorker with a page-global symbol so repeated initialization fails fast instead of creating conflicting browser worker state. Add a browser regression test that covers the duplicate call path while asserting only on a stable error prefix.

Fixes mswjs#2585
Rename the page-global setupWorker guard symbol to reflect that it stores an applied marker, not a worker instance. This keeps the singleton check aligned with the existing key-style naming in the codebase.
@trknhr trknhr marked this pull request as ready for review March 23, 2026 17:50
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.

MSW not handling requests after setupWorker is called more than once

1 participant