Skip to content

fix(browser): close the mock route interception race (fix #8339) - #11083

Merged
sheremet-va merged 10 commits into
vitest-dev:mainfrom
cplieger:fix/browser-anchor-interception
Oct 1, 2026
Merged

sheremet-va merged 10 commits into
vitest-dev:mainfrom
cplieger:fix/browser-anchor-interception

Conversation

@cplieger

@cplieger cplieger commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fix for #8339, the random vi.mocked(...).mockX is not a function / [Function x] is not a spy flake in browser mode. The root cause was traced in the issue thread.

Mock routes are added per test file and unrouted when the file ends, so the context's route count crosses zero between two mocking files and Playwright turns CDP request interception off and back on around it. Chromium acks Fetch.enable before interception is live, so a module request in that window is served the real module, and the ES module registry keeps it for the rest of the file. Only chromium is affected; the race was never reproduced on firefox or webkit.

The fix arms interception once per context, at the first vi.mock registration: one permanent probe route keeps the route count above zero, and the registration resolves only after a fetch() of the probe URL comes back with the route's header, so interception is provably live before the mocked import runs. If that cannot be verified within 5s the registration rejects with an error naming the issue instead of mocks silently not applying, and the next mocking file retries.

Suites without mocks get nothing: no route, no Fetch.enable, browser cache stays on. Firefox and webkit are unchanged.

Resolves #8339

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it's discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it. test/browser/fixtures/mocking/interception-probe.test.ts registers a mock in chromium and asserts the probe route answers.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to allow review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci. Typecheck, lint, the package build and the new chromium fixture pass locally; the full browser suite runs in CI.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command. (internal fix, no user-facing API to document)

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@netlify

netlify Bot commented Aug 28, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 7cb7ba8
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6abe19b7ba1b3600089b8e7b
😎 Deploy Preview https://deploy-preview-11083--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@sheremet-va

Copy link
Copy Markdown
Member

the thread measured 28s to 43s at 32 sessions

the performance implications are horrendous - and they punish those who don't even use mocking. We need a better solution.

@cplieger
cplieger force-pushed the fix/browser-anchor-interception branch from 84525ea to a6dd9cb Compare August 31, 2026 13:23
@cplieger cplieger changed the title fix(browser): keep request interception enabled between test files (fix #8339) fix(browser): close the request interception race between mock registration and module fetch (fix #8339) Aug 31, 2026
@cplieger

Copy link
Copy Markdown
Contributor Author

the thread measured 28s to 43s at 32 sessions

the performance implications are horrendous - and they punish those who don't even use mocking. We need a better solution.

@sheremet-va yeah fair comment, fixed it now by refining when the fix triggers. Now nothing is armed until the first vi.mock registration in a chromium context, so suites that don't mock get no route, no Fetch.enable, and keep the browser cache, and firefox / webkit are untouched.

Costs 4-15ms once per context, only when mocking, so the 28→43s number no longer applies (that code path is unreachable without mocks). OP is rewritten and the branch is rebased on main.

@cplieger
cplieger force-pushed the fix/browser-anchor-interception branch from a6dd9cb to d1ed5f4 Compare August 31, 2026 13:50
@cplieger cplieger changed the title fix(browser): close the request interception race between mock registration and module fetch (fix #8339) fix(browser): close the mock route interception race (fix #8339) Aug 31, 2026
@cplieger
cplieger force-pushed the fix/browser-anchor-interception branch from d1ed5f4 to 4d0d25f Compare August 31, 2026 13:54
Unrouting each file's mocks drops the context route count to zero, and chromium acks Fetch.enable before interception is live, so a module request right after the next registration is served unmocked.

Arm interception once per chromium context at the first registration: a permanent probe route keeps the count above zero afterwards, and the registration resolves only after a probe fetch() observably passes through interception. Suites without mocks install no route; firefox and webkit are unchanged.
@greedyivan

Copy link
Copy Markdown

Thanks for carrying this forward — the probe roundtrip is the right mechanism, and the PR
below keeps it at its core rather than replacing it.

I've opened #11112 as a separate PR because the change alters this one's design shape — the
probe's lifetime — rather than adding to it, and rewriting your branch in place didn't seem
right. Nothing here is incorrect; the difference is scope. As it stands, the probe route
holds the interception on for the whole context, and contexts are reused across files, so
files that never mock still pay a CDP roundtrip per request — that is exactly what the
review's performance concern traces to (measured on a mixed 11-file suite: 194 paused
requests vs 29 unpatched; the scoped variant: 40, with the non-mocking file back at zero).

#11112 keeps the probe, scopes it to the mock-route lifetime (arm on first registration,
disarm on the existing per-file teardown), serializes register/delete/clear per context,
and adds a deadline calibrated from measured arm latencies instead of a flat 5 s.

Happy either way — if you'd rather land the scoped variant here, I'll close mine and push
the commits over; the pieces are yours to take as-is.

@silverwind

silverwind commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Been exercising Claude on this race too until I saw this PR which looks to be on the right track. Here are two review points from it on this for your consideration:

  1. The probe route is never unrouted. One BrowserContext runs many files, so after the first mocking file interception stays armed for the whole run: every request paused, browser cache disabled, on files that never mock. Dropping it in clear keeps that cost off them.

  2. A rejected probe is cached forever. Nothing deletes from armedContexts, so one 5 s timeout fails every later mocking file on that worker instead of just the one that hit it.

My version of the fix is at silverwind@0e7e383, it also has a test you could copy.

@cplieger

Copy link
Copy Markdown
Contributor Author

Thanks @silverwind, made some fixes from your example

If the probe timed out once, it stayed broken for every later test file on that worker; now it just forgets and tries again next time. Swapped the hand-rolled timeout for the browser's own AbortSignal.timeout, same thing, less code. Added a small chromium test that mocks something and checks the probe route actually answers.

sheremet-va
sheremet-va previously approved these changes Sep 28, 2026

@sheremet-va sheremet-va left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but could you run formatter again?

@cplieger

cplieger commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

LGTM, but could you run formatter again?

done, ready to merge

@sheremet-va
sheremet-va merged commit d6620fa into vitest-dev:main Oct 1, 2026
29 checks passed
@cplieger
cplieger deleted the fix/browser-anchor-interception branch October 1, 2026 11:08
trueadm added a commit to octanejs/octane that referenced this pull request Oct 3, 2026
Backport vitest-dev/vitest#11083 into @vitest/browser-playwright@4.1.10 as
a pnpm patch. The provider mocks through context.route(); clearing mocks
between files removes every route, and Playwright 1.61.1 re-sends
Fetch.enable on the next route, which Chromium applies after its ack. A
module request issued right after vi.mock could load the real module
(vitest-dev/vitest#8339).

On a context's first mock registration the patched provider installs a
probe route, fetches it until interception is proven live, and keeps it
installed so interception stays on across files. The collection
regression now checks the probe after its mocked import, and the Base UI
browser lanes record the patch as evidence. Parity manifests and the evals
corpus pick up the new lockfile digest.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
ubugeeei added a commit to ubugeeei-reproductions/js-test-runner-competition that referenced this pull request Oct 9, 2026
…periment

The mixed suite mocks in every other file, the shape of a real suite. #11083 (merged
after 5.0.3) installs a permanent probe route at the first vi.mock of a browser context;
REPRO_EXP_PR11083=1 enables the backport, so its effect on the mock and mixed suites is
measured in the same job.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Browser] Inexplicable flakiness with spies and mocks in CI

4 participants