Repository navigation
test: make swr focus/reconnect revalidation deterministic - #741
Merged
Merged
Conversation
The upstream-root suite's focus/reconnect case intermittently lost the focus revalidation (fetcher called 2 of 3 times). The mount effect arms the focus throttle at Date.now() + focusThrottleInterval and a focus event revalidates only at a strictly later timestamp, so even at interval 0 a focus whose setTimeout-deferred adapter callback lands in the mount millisecond is throttled. This is pinned upstream SWR 2.4.2 behavior (its suite sleeps a real 1ms before focusing), so the test now controls the clock instead: fake only Date and step it past the armed throttle before dispatching focus, leaving real timers to drive the environment adapters' deferred callbacks. Co-Authored-By: Claude Fable 5 <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
trueadm
marked this pull request as ready for review
August 12, 2026 19:32
This branch was previously deployed
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
packages/swr/tests/upstream/root/root.test.ts— "revalidates on focus and reconnect through the pinned environment adapters" — whereexpect(fetcher).toHaveBeenCalledTimes(3)sporadically got 2 (observed ~1-in-3 standalone on macOS, and insidescripts/react-parity/check.mjsbatches).useSWR's mount effect armsnextFocusRevalidatedAt = Date.now() + focusThrottleIntervaland a focus event revalidates only at a strictly later timestamp, so even atfocusThrottleInterval: 0a focus event whosesetTimeout-deferred adapter callback (initProviderinsrc/_internal/utils/cache.tswraps the window listeners insetTimeout) fires in the sameDate.now()millisecond as the mount effect is throttled. The reconnect leg has no timestamp check, which is why failures always showed exactly 2 calls (initial + reconnect).upstream/src/index/use-swr.ts:659-682); upstream's own suite dodges the identical race by sleeping a real 1ms before dispatching focus (nextTick = () => act(() => sleep(1))in its test utils). Per the repo rule, the runtime is untouched and the test is made deterministic instead.Why
check.mjsparity batches; a vitest-lane failure there short-circuits the non-vitest binding lanes, so one flake hides unrelated coverage.Changes
packages/swr/tests/upstream/root/root.test.ts: the focus/reconnect case now fakes onlyDate(vi.useFakeTimers({ toFake: ['Date'] })) and steps the clock withvi.setSystemTime(Date.now() + 1)before dispatchingfocus, guaranteeing the strictly-later timestamp regardless of machine timing. Real timers still drive the environment adapters' deferred event callbacks — full fake timers would break the test the other way (the adapters bound the realsetTimeoutat provider init, so faked-timer advances never fire the deferrals and both event revalidations are lost). Upstream citation comments are unchanged; the oracle (3 fetcher calls +request-3rendered) is unchanged.Validation
pnpm format:checkpnpm typecheckpnpm test— 20,920/20,921; the single failure was the known unrelated browser-load flake (octane-events-browsernative-change, Vite "504 Outdated Optimize Dep" under full parallelism), which passes standalone 12/12--project swr35/35; mechanism proven by a frozen-Date.nowspy reproducing the exact reported failure deterministically (3/3), and an adversarial variant of the fixed test (clock step removed) failing deterministically with the same symptomRisk / follow-ups
pnpm typecheck:filesfails on swr test files because the tests use undeclaredwindow.__SWR_DEVTOOLS_*globals andpackages/swr/tsconfig.jsonincludes onlysrc(repo-widepnpm typechecknever checks swr tests). Being handled in a separate session.Provenance
agent-authored)Note
Low Risk
Test-only change in root.test.ts; no runtime or package behavior is modified.
Overview
Fixes intermittent failures in the upstream root parity test where focus revalidation was sometimes skipped, leaving 2 fetcher calls instead of 3.
The test now uses
vi.useFakeTimers({ toFake: ['Date'] })and advances the clock by 1ms before dispatchingfocus, so revalidation runs after the mount-time focus throttle even whenfocusThrottleInterval: 0. Real timers still run so deferred environment adapter callbacks behave as before. Assertions are unchanged;vi.useRealTimers()runs infinally.Reviewed by Cursor Bugbot for commit 839eb75. Bugbot is set up for automated code reviews on this repo. Configure here.