From 074c795d16009e781ddc68b1a89e61ecfb7c3122 Mon Sep 17 00:00:00 2001 From: usul <419201+fdussert@users.noreply.github.com> Date: Tue, 20 Oct 2026 11:22:26 +0200 Subject: [PATCH] Keep the UI tests off the real WebSocket useSocket() is a page-wide singleton that reconnects forever, and many component tests open it without asking: useAdminStore() subscribes to the config channel and useHealth() to the health one. Under jsdom that dialled ws://localhost:3000/ws, was refused, and armed a retry timer the test never knew about. One CI run on main saw that timer fire after marketplace-modal.test.ts's environment was torn down, with `location` gone, and fail the job with an unhandled ReferenceError although every test had passed. Each close also sent the session check through whatever fetch the test had stubbed. Fourteen jsdom test files reached the real socket this way, so the fix is one setup file rather than a vi.mock in each: a WebSocket that stays connecting, never opens and never closes, so the reconnect loop never starts. The two tests that drive the wire still stub their own with vi.stubGlobal, and vi.unstubAllGlobals() brings the silent one back. useSocket() itself is left as it is: it belongs to the page, not to a component scope, so there is no owner whose disposal should stop it. Co-Authored-By: Claude Opus 5.5 --- ui/test/setup.ts | 30 ++++++++++++++++++++++++++++++ ui/vitest.config.ts | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 ui/test/setup.ts diff --git a/ui/test/setup.ts b/ui/test/setup.ts new file mode 100644 index 00000000..c660d683 --- /dev/null +++ b/ui/test/setup.ts @@ -0,0 +1,30 @@ +/** + * No test reaches a real WebSocket. + * + * `useSocket()` is a page-wide singleton that reconnects forever, and plenty of component tests + * open it without asking: `useAdminStore()` subscribes to `config`, `useHealth()` to the health + * channel. Under jsdom that dialled `ws://localhost:3000/ws`, was refused, and armed a retry + * timer the test never knew about — one CI run saw it fire after the environment was torn down, + * with `location` gone (`ReferenceError: location is not defined` in `socket.ts`), and fail a + * suite whose tests had all passed. Each close also sent the session check through whatever + * `fetch` the test had stubbed. + * + * This socket stays connecting: it never opens, never closes, so the reconnect loop never + * starts. A test that wants to drive the wire stubs its own with `vi.stubGlobal('WebSocket', …)` + * (`socket-cache-invalidation.test.ts`); `vi.unstubAllGlobals()` brings this one back. + */ +class SilentWebSocket { + static CONNECTING = 0 + static OPEN = 1 + static CLOSING = 2 + static CLOSED = 3 + readyState = 0 + onopen: (() => void) | null = null + onmessage: (() => void) | null = null + onclose: (() => void) | null = null + onerror: (() => void) | null = null + send(): void {} + close(): void {} +} + +globalThis.WebSocket = SilentWebSocket as unknown as typeof WebSocket diff --git a/ui/vitest.config.ts b/ui/vitest.config.ts index 525a8ea5..9934e285 100644 --- a/ui/vitest.config.ts +++ b/ui/vitest.config.ts @@ -11,5 +11,5 @@ import vue from '@vitejs/plugin-vue' */ export default defineConfig({ plugins: [vue()], - test: { include: ['test/**/*.test.ts'], environment: 'node' }, + test: { include: ['test/**/*.test.ts'], environment: 'node', setupFiles: ['test/setup.ts'] }, })