Skip to content

test: make swr focus/reconnect revalidation deterministic - #741

Merged
trueadm merged 1 commit into
mainfrom
claude/eloquent-hamilton-538fa8
Aug 12, 2026
Merged

trueadm merged 1 commit into
mainfrom
claude/eloquent-hamilton-538fa8

Conversation

@trueadm

@trueadm trueadm commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Fixes the intermittent failure in packages/swr/tests/upstream/root/root.test.ts — "revalidates on focus and reconnect through the pinned environment adapters" — where expect(fetcher).toHaveBeenCalledTimes(3) sporadically got 2 (observed ~1-in-3 standalone on macOS, and inside scripts/react-parity/check.mjs batches).
  • The lost call is the focus revalidation. useSWR's mount effect arms nextFocusRevalidatedAt = Date.now() + focusThrottleInterval and a focus event revalidates only at a strictly later timestamp, so even at focusThrottleInterval: 0 a focus event whose setTimeout-deferred adapter callback (initProvider in src/_internal/utils/cache.ts wraps the window listeners in setTimeout) fires in the same Date.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).
  • This is byte-faithful pinned upstream SWR 2.4.2 behavior (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

  • An intrinsic same-millisecond race in a parity test flakes local runs and check.mjs parity 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 only Date (vi.useFakeTimers({ toFake: ['Date'] })) and steps the clock with vi.setSystemTime(Date.now() + 1) before dispatching focus, 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 real setTimeout at 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-3 rendered) is unchanged.

Validation

  • pnpm format:check
  • pnpm typecheck
  • pnpm test — 20,920/20,921; the single failure was the known unrelated browser-load flake (octane-events-browser native-change, Vite "504 Outdated Optimize Dep" under full parallelism), which passes standalone 12/12
  • targeted tests: fixed file 20/20 in a loop (previously flaky); --project swr 35/35; mechanism proven by a frozen-Date.now spy reproducing the exact reported failure deterministically (3/3), and an adversarial variant of the fixed test (clock step removed) failing deterministically with the same symptom

Risk / follow-ups

  • Test-only; no runtime or published-package change, so no changeset.
  • Pre-existing, unrelated: pnpm typecheck:files fails on swr test files because the tests use undeclared window.__SWR_DEVTOOLS_* globals and packages/swr/tsconfig.json includes only src (repo-wide pnpm typecheck never checks swr tests). Being handled in a separate session.

Provenance

  • An agent produced this diff (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 dispatching focus, so revalidation runs after the mount-time focus throttle even when focusThrottleInterval: 0. Real timers still run so deferred environment adapter callbacks behave as before. Assertions are unchanged; vi.useRealTimers() runs in finally.

Reviewed by Cursor Bugbot for commit 839eb75. Bugbot is set up for automated code reviews on this repo. Configure here.

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]>
@vercel

vercel Bot commented Aug 12, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated (UTC)
octane-website Skipped Skipped Aug 12, 2026 7:28pm
octane-website-mcp Skipped Skipped Aug 12, 2026 7:28pm

Request Review

@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent test PR: tests only labels Aug 12, 2026
@trueadm
trueadm marked this pull request as ready for review August 12, 2026 19:32
@trueadm
trueadm merged commit dd80ac0 into main Aug 12, 2026
24 of 25 checks passed
@trueadm
trueadm deleted the claude/eloquent-hamilton-538fa8 branch August 12, 2026 19:34

This branch was previously deployed

2 inactive deployments
Preview – octane-website-mcp — 839eb75c Deployed Aug 12, 2026 by vercel[bot]
Preview – octane-website — 839eb75c Deployed Aug 12, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent test PR: tests only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant