Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions ui/test/setup.ts
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion ui/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'] },
})
Loading