Repository navigation
fix(browser): close the mock route interception race (fix #8339) - #11083
Conversation
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
the performance implications are horrendous - and they punish those who don't even use mocking. We need a better solution. |
84525ea to
a6dd9cb
Compare
@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. |
a6dd9cb to
d1ed5f4
Compare
d1ed5f4 to
4d0d25f
Compare
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.
4d0d25f to
d4429c2
Compare
|
Thanks for carrying this forward — the probe roundtrip is the right mechanism, and the PR I've opened #11112 as a separate PR because the change alters this one's design shape — the #11112 keeps the probe, scopes it to the mock-route lifetime (arm on first registration, Happy either way — if you'd rather land the scoped variant here, I'll close mine and push |
|
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:
My version of the fix is at silverwind@0e7e383, it also has a test you could copy. |
|
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
left a comment
There was a problem hiding this comment.
LGTM, but could you run formatter again?
done, ready to merge |
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>
…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>
Description
Fix for #8339, the random
vi.mocked(...).mockX is not a function/[Function x] is not a spyflake 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.enablebefore 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.mockregistration: one permanent probe route keeps the route count above zero, and the registration resolves only after afetch()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:
test/browser/fixtures/mocking/interception-probe.test.tsregisters a mock in chromium and asserts the probe route answers.pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci. Typecheck, lint, the package build and the new chromium fixture pass locally; the full browser suite runs in CI.Documentation
pnpm run docscommand. (internal fix, no user-facing API to document)Changesets
feat:,fix:,perf:,docs:, orchore:.