Skip to content

[test] Stop leaking mocked performance clocks into later test files - #37379

Merged
eps1lon merged 1 commit into
mainfrom
sebbie/stop-leaking-performance-mocks
Aug 26, 2026
Merged

eps1lon merged 1 commit into
mainfrom
sebbie/stop-leaking-performance-mocks

Conversation

@eps1lon

@eps1lon eps1lon commented Aug 26, 2026 •

Copy link
Copy Markdown
Collaborator

Test files running in the Node.js Jest environment share the worker process's performance object, because jest-environment-node installs it by reference rather than by copy. When a test mocked the clock with Object.defineProperty(performance, 'now', ...), the mutation hit the shared object and was never undone, since Jest only restores jest.spyOn mocks when a file's runtime is torn down (jest-runtime's teardown() calls restoreAllMocks()). Every subsequent test file in the same worker then observed the fake clock, including jsdom-based files, whose performance.now() subtracts a window-creation timestamp from the shared object's now().

This change switches the six affected test files to jest.spyOn(performance, 'now') and jest.spyOn(performance, 'timeOrigin', 'get'), which Jest restores automatically at teardown. ReactFlightDOMEdge-test.js runs in jsdom and therefore did not leak, but it used the same pattern and is converted for consistency.

This change is mostly for test hygiene. Was discovered while investigating a flaky {"time":NaN} serialisation bug (e.g. https://github.com/react/react/actions/runs/32878999825/job/97903912119)

Test files running in the Node.js Jest environment share the worker
process's `performance` object, because `jest-environment-node` installs
it by reference rather than by copy. When a test mocked the clock with
`Object.defineProperty(performance, 'now', ...)`, the mutation hit the
shared object and was never undone, since Jest only restores
`jest.spyOn` mocks when a file's runtime is torn down. Every subsequent
test file in the same worker then observed the fake clock, including
jsdom-based files, whose `performance.now()` subtracts a window-creation
timestamp from the shared object's `now()`.

This change switches the six affected test files to
`jest.spyOn(performance, 'now')` and
`jest.spyOn(performance, 'timeOrigin', 'get')`, which Jest restores
automatically at teardown. `ReactFlightDOMEdge-test.js` runs in jsdom
and therefore did not leak, but it used the same pattern and is
converted for consistency. The leak is a suspect for the flaky
`{"time":NaN}` Flight serialization failure seen in CI, since a leaked
clock whose `now()` returns a non-number propagates NaN into the
timing rows the Flight server serializes.

Co-Authored-By: Claude Code (kimi-k3[1m]) <noreply@anthropic.com>
@meta-cla meta-cla Bot added the CLA Signed label Aug 26, 2026
@github-actions

github-actions Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

Comparing: 373d35e...c71633e

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 7.35 kB 7.35 kB = 1.96 kB 1.96 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 622.22 kB 622.22 kB = 110.01 kB 110.01 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 7.36 kB 7.36 kB +0.05% 1.96 kB 1.96 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 693.43 kB 693.43 kB = 121.46 kB 121.46 kB
facebook-www/ReactDOM-prod.classic.js = 713.60 kB 713.60 kB = 124.94 kB 124.94 kB
facebook-www/ReactDOM-prod.modern.js = 703.91 kB 703.91 kB = 123.36 kB 123.36 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by sizebot against c71633e

@github-actions github-actions Bot added the React Core Team Opened by a member of the React Core Team label Aug 26, 2026
@eps1lon eps1lon changed the title sebbie/stop leaking performance mocks Stop leaking mocked performance clocks into later test files Aug 26, 2026
@eps1lon eps1lon changed the title Stop leaking mocked performance clocks into later test files [ci] Stop leaking mocked performance clocks into later test files Aug 26, 2026
@eps1lon eps1lon changed the title [ci] Stop leaking mocked performance clocks into later test files [test] Stop leaking mocked performance clocks into later test files Aug 26, 2026
@eps1lon
eps1lon requested a review from unstubbable August 26, 2026 06:50
@eps1lon
eps1lon marked this pull request as ready for review August 26, 2026 06:50
@eps1lon
eps1lon merged commit 3c397fe into main Aug 26, 2026
476 of 477 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants