Repository navigation
Description
Problem
bun run test (full suite) intermittently fails with 1 failed test file / 1 failed test, reported as an unhandled error originating in BrowserSyncService.scheduleReconnect:
❯ BrowserSyncService.scheduleReconnect src/lib/services/browser-sync-service.ts:364:29
364| this.reconnectTimeout = setTimeout(() => {
❯ BrowserSyncService.connect src/lib/services/browser-sync-service.ts:181:12
❯ src/lib/services/browser-sync-service.ts:366:12
Test Files 1 failed | 246 passed (247)
Tests 1 failed | 5084 passed (5085)
src/tests/services/browser-sync-service.test.ts passes 47/47 when run in isolation (bun run test -- browser-sync-service), so this is a cross-file timing leak, not a genuine assertion failure.
Root cause
BrowserSyncService retries reconnection indefinitely by design (scheduleReconnect, browser-sync-service.ts:356-367) — connect()'s catch block (:180-181) arms a setTimeout that calls connect() again.
Two things combine to let that timer escape the test that created it:
- The top-level
afterEach(browser-sync-service.test.ts:100-106) resets stores and mocks but never callsbrowserSyncService.disconnect()— which is the only thing that runsclearTimeout(this.reconnectTimeout)(browser-sync-service.ts:393). A timer armed during a test therefore survives teardown. - The
Reconnection Logicdescribe block installs fake timers (:947-949), but one test inside it swaps back to real timers and restores fake ones only in afinally(:1029-1035). A reconnect armed during that real-timer window is a realsetTimeoutwith a >=1000ms backoff delay.
So a real timer can outlive its test, fire connect() while an unrelated test file is running, and surface as an unhandled error attributed to whichever file happens to be executing then. Whether it lands inside the suite's remaining runtime depends on scheduling — hence the intermittency and the shifting attribution.
Suggested fix
Call browserSyncService.disconnect() in the top-level afterEach so no reconnect timer can outlive its test. Worth also tightening the real-timer window at :1029-1035 so a reconnect armed there is cleared before fake timers are restored.
Acceptance criteria
-
browser-sync-service.test.tscleans up any armed reconnect timer in teardown - Full-suite
bun run testruns clean across repeated runs (no strayscheduleReconnectunhandled error) -
browser-sync-service.test.tsstill passes 47/47 in isolation
Notes
Found while establishing the test baseline for #1698. Pre-existing on main (commit 8961f7e) and unrelated to that work.
Activity
mstomar125 commentedon Sep 17, 2026
Frontend baseline (unfixed, single run): 5085 passed (5085), 247 test files passed (247). Duration 107.88s. Investigating the cross-file reconnect-timer flake per the issue's root-cause diagnosis; will run the suite repeatedly to try to reproduce before/after the fix.