[#451] added websocket connexion (#453)

* [#451] added websocket connexion

* [#451] added tests

* [#451] add missing cleanup for web socket causing memory leaks
This commit is contained in:
Camille Moussu
2026-01-14 15:17:29 +01:00
committed by GitHub
parent 8b3afc1737
commit 2fa8a7f16a
18 changed files with 1204 additions and 4 deletions
@@ -0,0 +1,35 @@
export function setupWebsocket() {
const originalWebSocket = global.WebSocket;
const webSocketInstances: any[] = [];
const mockWebSocket = jest.fn().mockImplementation((url: string) => {
const ws = {
url,
readyState: WebSocket.CONNECTING,
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
send: jest.fn(),
close: jest.fn(),
_listeners: {} as Record<string, Function[]>,
};
ws.addEventListener.mockImplementation((event, handler) => {
ws._listeners[event] ??= [];
ws._listeners[event].push(handler);
});
ws.removeEventListener.mockImplementation((event, handler) => {
ws._listeners[event] =
ws._listeners[event]?.filter((h) => h !== handler) ?? [];
});
webSocketInstances.push(ws);
return ws;
});
global.WebSocket = mockWebSocket as any;
const cleanup = () => {
global.WebSocket = originalWebSocket;
webSocketInstances.length = 0;
};
return { webSocketInstances, mockWebSocket, cleanup };
}