fix(browser): remove orphaned Playwright route when same module is mocked via multiple ids (fix #9957)#10267
Merged
sheremet-va merged 3 commits intoMay 8, 2026
Conversation
…s mocked via multiple ids (fix vitest-dev#9957)
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
sheremet-va
requested changes
May 7, 2026
| } | ||
| return rpc().onCollected(this.method, files) | ||
| const result = await rpc().onCollected(this.method, files) | ||
| if (this.method === 'collect') { |
Member
There was a problem hiding this comment.
I don't think there are any tests that cover test collection with mocking (this is collect only in vitest list command)
Contributor
Author
|
Good catch. Removed the |
sheremet-va
approved these changes
May 8, 2026
Member
|
Thanks! |
Contributor
Author
|
And you! 🙏 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #9957.
Description
If you call
vi.mock('~/modal')andvi.mock('./modal')in the same test file, and both aliases resolve to the same URL,createMocker()inplaywright.tsregisters two Playwright routes. The second call overwrites the predicate inidPredicateswithout removing the first route. Whenclear()runs at the end of the file, it only finds the second predicate -- the first route stays attached to the browser context.That orphaned route fires when the next test file loads the same module. Its handler calls
module.resolve()through a birpc channel that belongs to the previous session, which is already closed. That's where[birpc] rpc is closedcomes from.playwright.ts: before registering a route for a session+URL, checkidPredicatesfor an existing predicate andunroute()it first. SwitchedsessionIdsfromstring[]toSet<string>so URL entries can't duplicate andclear()removes each route exactly once.Testing
Added
test/browser/fixtures/mocking/src/dual-id/with a source module, a consumer that imports it via the alias, and two test files. The probe registers the module through both~/dual-id/modaland./dual-id/modal. The target (same browser session, runs after) asserts the real implementation is visible. Without this fix, the target crashes with[birpc] rpc is closed; with it, the mock is cleaned up between files.