Skip to content

fix(browser): scope request interception to mock-route lifetime (fix #8339) - #11112

Closed
greedyivan wants to merge 2 commits into
vitest-dev:mainfrom
greedyivan:fix/browser-interception-scoped
Closed

greedyivan wants to merge 2 commits into
vitest-dev:mainfrom
greedyivan:fix/browser-interception-scoped

Conversation

@greedyivan

Copy link
Copy Markdown

Summary

Mock routes are playwright context.route() handlers; the provider unroutes them when a
file ends, so the route count crosses zero between files and playwright toggles CDP
Fetch.disable / Fetch.enable. Chromium acks Fetch.enable before interception is live,
so the first module request of a mocking file can be served unmocked and cached by the ES
registry for the file's lifetime.

This PR scopes the interception to the mock-route lifetime:

  • arm: on the first mock registration of a context, complete one probe roundtrip before
    the mocker releases the file's imports (probe mechanism from fix(browser): close the mock route interception race (fix #8339) #11083, @cplieger);
  • disarm: when the last mock route of the context is removed (the existing per-file
    teardown), unroute the probe and drop the memo — files without mocks run with zero
    interception;
  • register/delete/clear are serialized per context — no stale armed memo or route-count
    drift from parallel sessions;
  • arm must converge within interceptionArmTimeout = 2 s (ceil(3 × p99) over 74 measured
    arms: 13–30 ms sequential, p99 604 ms including a 32-parallel-sessions run); otherwise it
    fails loud.

Non-chromium providers are untouched.

Measurements

chromium, DEBUG=pw:protocol, 11-file suite (10 mocking files + 1 plain):

enable disable paused requests non-mock file paused
unpatched 10 10 29 0 — but the race reproduced: a mock silently didn't apply (1 failure in 2 logged cold baseline runs)
always-on interception 1 0 194 yes
this PR 10 10 40 0

The +10 pauses are one probe roundtrip per arm.

Tests

  • new test/browser/specs/mocking-interception.test.ts (chromium): one Fetch.enable/
    Fetch.disable pair per mocking file; one paused probe per arm; zero paused requests
    attributable to the non-mocking file; all files green. firefox/webkit: run health.
  • existing mocking.test.ts unchanged, green (cross-file leak, rerun invalidation).

Fixes #8339. Probe-arming mechanism follows #11083 — this PR narrows its lifetime and adds
the disarm path, per-context serialization, and deadline calibration.

@netlify

netlify Bot commented Sep 1, 2026

Copy link
Copy Markdown

✅ Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit cd2401f
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6a96561a898a7400089ae6ba
😎 Deploy Preview https://deploy-preview-11112--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.

@greedyivan

Copy link
Copy Markdown
Author

CI triage for the three failed checks — none trace to this PR's diff:

  • Test: unit, node-24, windows-latest — the same job fails on main (last main run: https://github.com/vitest-dev/vitest/actions/runs/33386072594), so it's pre-existing.
  • Browsers: shard 2/2 (ubuntu) — bail-out > exits gracefully when the browser connection is closed while cancelling received an unhandled The chromium page crashed while running tests… out of memory error. That spec uses no mocks, so the interception code added here never executes in it; the file passes 3/3 on this branch locally.
  • Browsers: rest (windows) — oxc.test.ts > oxc config on browser snapshot mismatch; the diff touches only the playwright provider and mock fixtures.

Retriggering with an empty commit for a fresh sample.

…dows unit, OOM page-crash flake, oxc snapshot)
@sheremet-va sheremet-va closed this Sep 1, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 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

2 participants