Skip to content

test(Switch): detach the touch controllers each test attaches - #13017

Merged
cixzhang merged 3 commits into
meta:mainfrom
vjeux:switch-touch-test-detaches
Nov 20, 2026
Merged

cixzhang merged 3 commits into
meta:mainfrom
vjeux:switch-touch-test-detaches

Conversation

@vjeux

@vjeux vjeux commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Maintainer impact

On main, a full vitest --project ui run under load can report one unhandled error while every test passes. That error turns a local pnpm preflight red. It is TypeError: view.removeEventListener is not a function, thrown from browserClickWait.ts:90 through stop into inputStream.ts:244. Vitest says it originated in Switch/useSwitchTouch.test.ts, and the file passes when it runs alone.

Intended invariant

The file's tests and assertions do not change. A test leaves no listener or timer running after it ends. browserClickWait.test.ts already ends each wait that it starts in its afterEach.

Change

The test reads no box when it attaches, and reads the track once at each press lifts a tap on a SwitchTouchController that it never detaches. The lift starts the 300 ms awaitBrowserClick wait. No click, later lift or detach() ends it in that test, so it runs until its timer fires.

Vitest's jsdom environment makes document.defaultView the Node global, so view.setTimeout is Node's own timer. At teardown, Vitest deletes jsdom's keys from that global, removeEventListener among them. When the file ends inside the 300 ms and its worker is still alive, the timer fires into that global and throws.

The file now records each controller that its tests attach, and its afterEach detaches them. Lint-staged's eslint --fix also adds braces to an existing curly warning in the file. The test file is the only change, so no Changeset is needed.

Evidence

  • Failure before the change: 9 of 10 rounds on main a19a517 report unhandled errors, 33 in all. Each round runs 32 copies of the file with 16 workers.
  • Success after the change: 0 of 10 rounds report one, with each side run back to back in every round.
  • Leaked timer: a local probe records the file's timers of 100 ms or more. One timer is still pending at the last test on main, and none is pending with the change.
  • Product/runtime behavior verified unchanged: only the test file changes.
  • CI impact: no workflow, action or CI script changes.

Scope

Components: Switch

  • No intended public API, product behavior, visual direction, or policy change.
  • Product changes discovered during the work were removed or split.
  • Public text and artifacts contain no internal Meta context.

Testing

Each round runs this command on main a19a517, then on this branch, back to back:

vitest run --project ui --maxWorkers=16 <32 copies of packages/core/src/Switch/useSwitchTouch.test.ts>
  • main: 9 of 10 rounds report unhandled errors, 1 to 7 per round and 33 in all. Each one is TypeError: view.removeEventListener is not a function, thrown at inputStream.ts:244 from the timer at browserClickWait.ts:90. All 384 tests pass in each round.
  • This branch: 0 of 10 rounds report an unhandled error, and all 384 tests pass in each round.
  • The file alone on main passes 6 of 6 runs with no unhandled error, so the failure needs load.
  • The probe on main lists one pending timer: 300 ms, from awaitBrowserClick in SwitchTouchController.release, started by the touchUpInside call in that test. On this branch it lists none.
  • useSwitchTouch.test.ts passes 12 of 12. ESLint with --max-warnings=0, Prettier and tsc --noEmit -p packages/core/tsconfig.json pass.

A test drives a tap's lift on a controller that it never detaches.
The lift starts the 300 ms wait for the browser's click. When the file
ends first, the wait outlives the test environment. Its timer then
calls removeEventListener on a window that the environment has torn
down, and Vitest reports an unhandled error for the file.

The file's afterEach now detaches every controller that its tests
attach. Detach ends the wait and clears its timer.
@vercel

vercel Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 20, 2026 4:43am UTC

Request Review

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · 9e74a6b

Storybook · Sandbox · built from this PR's head

Modified components

Component Size LoC Visual · A11y · RTL
Switch · Storybook 110.7 kB (0) · 10.4 kB CSS (0) 1,784 (0) — · ✓ · ✓

Package size: no published package changed; no new dependencies.

Touched components checked; for the full suites, add ci:run-visual for Visual, ci:run-rtl for RTL.


Generated by PR Enrichment workflow | View full report

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 19, 2026
@astracat-bot

astracat-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 20, 2026, 5:00 AM UTC

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved pending CI. CI has not run on this head yet because the pull request is a draft from a fork: a maintainer adds the ci:run label so CI runs once it is marked ready for review.

The fix holds up: detaching every controller in afterEach ends the 300 ms browser-click wait that the tap-lift test starts, and with Vitest's teardown simulated after the last test, the reported TypeError appears without the change and not with it. No public API, shipped behaviour or release note changes.

Claims and files checked

Review mode: full; reason: no-earlier-review

Claims:

  • Under load, a full ui run on main can end with one unhandled error while every test passes: verified in mechanism by the simulated teardown; the round counts are the author's own measurement.
  • That error makes a local preflight fail: verified; preflight runs vitest run --project ui, and Vitest exits non-zero on an unhandled error.
  • The error comes from the wait's timer in browserClickWait.ts:90, through stop, to inputStream.ts:244: verified; the reproduction shows the same frames.
  • The file passes when it runs alone: verified, 12 of 12 with and without the change.
  • The file's tests and assertions do not change: verified.
  • browserClickWait.test.ts already ends each wait in its afterEach: verified (browserClickWait.test.ts:23-26).
  • The tap-lift test starts the 300 ms wait on a controller it never detaches: verified (useSwitchTouch.test.ts:341, useSwitchTouch.ts:728).
  • Vitest's jsdom environment points document.defaultView at the Node global and deletes removeEventListener at teardown, while setTimeout stays Node's own: verified in Vitest 4.1.11.
  • The file now records each controller and detaches them after each test: verified (useSwitchTouch.test.ts:93, :114, :545, :156-158).
  • Lint-staged added braces for an existing curly warning: verified; the warning shows on main and not on this branch.
  • Only a test changes, so no Changeset is needed: verified against CONTRIBUTING.md.
  • One timer is still pending at the end of the file on main and none with the change: verified (one 300 ms timer before, none after).
  • No product, API, workflow or CI change: verified; one test file changes and the built API surface is identical.
  • The file's tests, ESLint with zero warnings, Prettier and the Core typecheck pass: verified.

Files:

  • packages/core/src/Switch/useSwitchTouch.test.ts: clear.

Automated review (maintainer link)

@cixzhang cixzhang added ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) ci:run-a11y Run the accessibility suite on this PR; write access, next push (AST-030 FR12) labels Nov 20, 2026
@cixzhang

Copy link
Copy Markdown
Contributor

/approve-spec 15ed46b

@cixzhang

Copy link
Copy Markdown
Contributor

/approve-spec 8265376

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved pending CI: the pull request is still a draft, so CI has not run on this head; it already carries ci:run, so marking it ready for review starts the run.

The merge of main leaves the patch unchanged and touches neither this test file nor the modules it exercises. Detaching every controller in afterEach ends the 300 ms browser-click wait that the tap-lift test starts, and with Vitest's teardown simulated after the last test, the reported TypeError appears without the change and not with it. No public API, shipped behaviour or release note changes.

Claims and files checked

Review mode: carry-forward; reason: none

Claims:

  • Under load, a full ui run on main can end with one unhandled error while every test passes: verified in mechanism by the simulated teardown; the round counts are the author's own measurement.
  • That error makes a local preflight fail: verified; preflight runs vitest run --project ui, and Vitest exits non-zero on an unhandled error.
  • The error comes from the wait's timer in browserClickWait.ts:90, through stop, to inputStream.ts:244: verified; the reproduction shows the same frames, and those lines are unchanged at this head.
  • The file passes when it runs alone: verified, 12 of 12 with and without the change.
  • The file's tests and assertions do not change: verified.
  • browserClickWait.test.ts already ends each wait in its afterEach: verified (browserClickWait.test.ts:23-26).
  • The tap-lift test starts the 300 ms wait on a controller it never detaches: verified (useSwitchTouch.test.ts:341, useSwitchTouch.ts:728).
  • Vitest's jsdom environment points document.defaultView at the Node global and deletes removeEventListener at teardown, while setTimeout stays Node's own: verified in Vitest 4.1.11, which the merge of main does not change.
  • The file now records each controller and detaches them after each test: verified (useSwitchTouch.test.ts:93, :114, :545, :156-158).
  • Lint-staged added braces for an existing curly warning: verified; the warning shows without the change and not with it.
  • Only a test changes, so no Changeset is needed: verified against the contributing guide's Changeset rules.
  • One timer is still pending at the end of the file on main and none with the change: verified (one 300 ms timer before, none after).
  • No product, API, workflow or CI change: verified; one test file changes and the built API surface is identical.
  • Public text carries no internal context: verified.
  • The file's tests, ESLint with zero warnings, Prettier and the Core typecheck pass: verified before the merge of main, which changes none of the file's inputs.

Files:

  • packages/core/src/Switch/useSwitchTouch.test.ts: clear.

Automated review (maintainer link)

@cixzhang

Copy link
Copy Markdown
Contributor

/approve-spec 9e74a6b

@astracat-bot astracat-bot Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved pending CI. The fast checks pass on this head, and this review's pass released the heavy suites, which were still running as it was written.

Detaching every controller in afterEach ends the 300 ms browser-click wait that the tap-lift test starts. Re-measured on this head: with Vitest's teardown simulated after the last test, the base tree reports the same TypeError with the same frames and one 300 ms timer still pending at the end of the file, while the head tree reports neither and the file passes 12 of 12 on both. No public API, shipped behaviour or release note changes, and the merge of main leaves the patch unchanged.

Claims and files checked

Review mode: carry-forward; reason: none

Claims:

  • Under load a full ui run on main can end with one unhandled error while every test passes: the mechanism is verified by the simulated teardown on the base tree; the round counts are the author's own measurement, which the review sandbox cannot reproduce.
  • That error turns a local preflight red: verified; preflight runs vitest run --project ui, and Vitest exits non-zero on an unhandled error.
  • The error comes from the wait's timer at browserClickWait.ts:90, through stop, to inputStream.ts:244: verified; the base-tree reproduction prints exactly those frames.
  • The file passes when it runs alone: verified, 12 of 12 on both trees.
  • The file's tests and assertions do not change: verified against the diff.
  • browserClickWait.test.ts already ends each wait it starts in its afterEach: verified (browserClickWait.test.ts:20-27).
  • The tap-lift test starts the 300 ms wait on a controller it never detaches: verified (useSwitchTouch.test.ts:324, :341; useSwitchTouch.ts:592-611, :724-730).
  • Vitest's jsdom environment points document.defaultView at the Node global and deletes removeEventListener at teardown while setTimeout stays Node's own: verified by the teardown simulation, which fails on the base tree only; the pinned Vitest version is unchanged by the merge.
  • The file now records each controller its tests attach and detaches them after each test: verified (useSwitchTouch.test.ts:93, :114, :545, :156-158).
  • Lint-staged's eslint --fix also added braces for an existing curly warning: verified; ESLint reports that warning at :454 on the base tree and nothing on the head.
  • One timer is still pending at the end of the file without the change and none with it: verified, one 300 ms timer against none.
  • Only a test changes, so no Changeset is needed: verified against the contributing guide, which exempts tests.
  • No product, API, workflow or CI change: verified; one test file changes and no published package's surface moves.
  • The file's tests, ESLint with zero warnings, Prettier and the Core typecheck pass: verified on the head tree.
  • Public text carries no internal context: verified.

Files:

  • packages/core/src/Switch/useSwitchTouch.test.ts: clear.

Automated review (maintainer link)

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Engineering-owner approval at this exact head (9e74a6b), posted by Cindy's agent on her instruction. CodeBunny recommends approval at this head, and CI (all jobs including heavy) is green.

@cixzhang
cixzhang merged commit fbbcd2c into meta:main Nov 20, 2026
142 of 145 checks passed
@vjeux
vjeux deleted the switch-touch-test-detaches branch November 20, 2026 05:24

This branch was successfully deployed

1 active deployment
Preview — 9e74a6b3 Deployed Nov 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) ci:run-a11y Run the accessibility suite on this PR; write access, next push (AST-030 FR12) CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants