/** * Jest test setup */ import { beforeEach, afterEach, jest } from '@jest/globals'; // Mock WebSocket globally class MockWebSocket { static CONNECTING = 0; static OPEN = 1; static CLOSING = 2; static CLOSED = 3; public onopen: ((event: Event) => void) | null = null; public onclose: ((event: CloseEvent) => void) | null = null; public onerror: ((event: Event) => void) | null = null; public onmessage: ((event: MessageEvent) => void) | null = null; public readyState: number = MockWebSocket.CONNECTING; public url: string; constructor(url: string) { this.url = url; } send(data: string | ArrayBuffer): void { // Mock implementation } close(code?: number, reason?: string): void { this.readyState = MockWebSocket.CLOSED; if (this.onclose) { this.onclose(new CloseEvent('close', { code: code || 1000, reason: reason || '' })); } } } // Add to global scope (global as any).WebSocket = MockWebSocket; // Mock localStorage const localStorageMock = (() => { let store: Record = {}; return { getItem: (key: string) => store[key] || null, setItem: (key: string, value: string) => { store[key] = value.toString(); }, removeItem: (key: string) => { delete store[key]; }, clear: () => { store = {}; }, length: () => Object.keys(store).length, key: (index: number) => Object.keys(store)[index] || null }; })(); Object.defineProperty(global, 'localStorage', { value: localStorageMock }); // Mock console methods for cleaner test output const originalError = console.error; const originalWarn = console.warn; beforeEach(() => { console.error = jest.fn(); console.warn = jest.fn(); }); afterEach(() => { console.error = originalError; console.warn = originalWarn; }); // Mock performance.now() for consistent timing in tests Object.defineProperty(global.performance, 'now', { writable: true, value: jest.fn(() => Date.now()) }); // Mock requestAnimationFrame and cancelAnimationFrame (global as any).requestAnimationFrame = jest.fn((callback: Function) => { return setTimeout(callback, 16); }); (global as any).cancelAnimationFrame = jest.fn((id: number) => { clearTimeout(id); }); // Increase timeout for integration tests jest.setTimeout(30000);