Skip to content

fix(react): deterministic fake clock for frameIntervalMs cases - #8

Merged
stamp-build[bot] merged 2 commits into
mainfrom
agent/sindresorhus/react-clock-determinism
Nov 21, 2026
Merged

stamp-build[bot] merged 2 commits into
mainfrom
agent/sindresorhus/react-clock-determinism

Conversation

@stamp-build

@stamp-build stamp-build Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

main went red after merging (run 44882580287, job react). ui.progress.filling@light,phone was 3 CSS px short of its golden, which is 156 ms of the 320 ms fill instead of 160 ms. The same code had passed on the PR run.

Root cause

For frameIntervalMs cases, render.ts called page.clock.install({ time }) and then page.clock.pauseAt(time + 1000) before page.goto. Playwright (1.65.1, server/clock.ts and injected/src/clock.ts) records each clock call as an init-script log entry, along with the host's real Date.now() at the moment of the call. Each new document injects the clock and calls resume(), then replays the log. During the replay, the clock runs in real time between install and pauseAt. So the navigated page's performance.now() starts at the real number of milliseconds between those two API calls, not at 0.

The component mounts at performance.now() = d. Fake requestAnimationFrame stays on 16 ms boundaries of the fake clock, so after 10 x 16 ms the last frame is at 160 and the fill is (160 - d) / 320:

  • d = 1 on the recording run gives 119 px (the old golden).
  • d = 5 on main gives 116 px. That is the 3 CSS px (6 image px) diff at the fill's edge.
  • d = 0, the intended instant, gives 120 px: exactly half of the 240 px track, as the case's comment claims. Neither run produced it.

I reproduced this with Playwright's own ClockController in Node, driven by a fake embedder. With install+pauseAt and a gap of 0/1/4/9 ms, the page starts at performance.now() = 0/1/4/9. With pauseAt alone it is 0 every time.

Fix

  • Only page.clock.pauseAt(clock) is called before navigation. The page loads paused at performance.now() = 0 and Date.now() = clock however long load, network idle, readiness, fonts and images take. Settle frames then runFor(frameIntervalMs) in exact steps, so the capture is at settleFrames * frameIntervalMs.
  • The renderer reads the page clock before the first settle frame and again at capture. If either reading is off, the variant becomes an error instead of producing a slightly different image.
  • Date.now() during load is now clock, as it is for cases without a frame interval, instead of clock + 1000. No golden depends on it.
  • The guarantee is documented in the README and on frameIntervalMs in the meta type. frameClock and clockProblems have unit tests.
  • CI: after verify, the react job runs vcase-react verify --filter 'ui.progress.*' 5 more times, and any difference fails the job.

Golden

ui.progress.filling@light,phone was re-recorded by CI run 44883277063. The fill goes from 119 to 120 CSS px (measured: blue x 32..271 vs 32..269 at density 2), which is the documented instant: mount at 0, capture at 160 ms. No other golden and no manifest change. The first run on this PR passed both clock checks (0 before the first frame, 160 at capture) and failed only on this 1 px.

🤖 Generated with Claude Code

sindresorhus added 2 commits November 21, 2026 08:09
ui.progress.filling failed on main 3 CSS px short of its golden (fill at
156 ms instead of 160 ms) while the same code passed on the PR run.

Cause: render.ts called page.clock.install({ time }) and then
page.clock.pauseAt(time + 1000) before navigating. Playwright replays these
calls into each new document using the real wall time at which each was
made, and the clock runs between install and pauseAt, so the page's
performance.now() started at the real milliseconds between the two calls
(0 on the recording run, 4 on main). The component mounted at that value
while requestAnimationFrame stays on 16 ms boundaries, so the last frame
before the capture was 4 ms less into the animation.

Call only pauseAt(clock): the new document starts paused at
performance.now() = 0 and Date.now() = clock, whatever the load takes. The
renderer now reads the page clock before the first settle frame (0) and at
the capture (settleFrames * frameIntervalMs) and makes the variant an error
on any other value. Documented in README and the meta type; unit tests for
the pure parts; CI verifies the frameIntervalMs cases 5 more times.

Signed-off-by: sindresorhus <raft+sindresorhus@agents.stamp.build>
The old golden was recorded with the page clock starting 1 ms late (fill
119 px, 159 ms in); the flaky main run started 5 ms late (116 px). With
the clock starting at 0 the fill is 120 px: exactly 160 of 320 ms, half
of the 240 px track, as the case's comment says. Recorded by CI run
44883277063.

Signed-off-by: sindresorhus <raft+sindresorhus@agents.stamp.build>
@stamp-build
stamp-build Bot force-pushed the agent/sindresorhus/react-clock-determinism branch from c2e7694 to 038c76f Compare November 21, 2026 08:09
@stamp-build
stamp-build Bot merged commit 720c4f7 into main Nov 21, 2026
2 checks passed
@stamp-build stamp-build Bot mentioned this pull request Nov 21, 2026
stamp-build Bot added a commit that referenced this pull request Nov 21, 2026
Deterministic fake clock for frameIntervalMs cases (#8) and local
request routes (#6). Tagging v0.1.4 also exercises npm trusted
publishing from the npm environment for the first time.

Signed-off-by: sindresorhus <raft+sindresorhus@agents.stamp.build>
Co-authored-by: sindresorhus <raft+sindresorhus@agents.stamp.build>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants