Skip to content

fix(site): add tests for createMockWebSocket #19172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 7, 2025
Merged
Prev Previous commit
Next Next commit
refactor: clean up types
  • Loading branch information
Parkreiner committed Aug 7, 2025
commit b63ff10ffa4b6677b89f875b3861d08d0d5a40ed
8 changes: 2 additions & 6 deletions site/src/testHelpers/websockets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,12 @@ describe(createMockWebSocket.name, () => {
for (const jd of jsonData) {
socket.addEventListener("message", onMessage);
server.publishMessage(
new MessageEvent<string>("message", {
data: JSON.stringify(jd),
}),
new MessageEvent("message", { data: JSON.stringify(jd) }),
);

expect(onMessage).toHaveBeenCalledTimes(1);
expect(onMessage).toHaveBeenCalledWith(
new MessageEvent<string>("message", {
data: JSON.stringify(jd),
}),
new MessageEvent("message", { data: JSON.stringify(jd) }),
);

socket.removeEventListener("message", onMessage);
Expand Down
31 changes: 19 additions & 12 deletions site/src/testHelpers/websockets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { WebSocketEventType } from "utils/OneWayWebSocket";

type SocketSendData = string | ArrayBufferLike | Blob | ArrayBufferView;
type SocketSendData = Parameters<WebSocket["send"]>[0];

export type MockWebSocketServer = Readonly<{
publishMessage: (event: MessageEvent<string>) => void;
Expand All @@ -12,21 +12,28 @@ export type MockWebSocketServer = Readonly<{
readonly clientSentData: readonly SocketSendData[];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was one part I was a little torn on, and I'd be open to alternative ways to track data sent from the mock socket to the mock server

It feels like the "boy scout" way of doing this would be with subscription callbacks, but since we'd likely just be checking the contents for a single isolated test case, that'd add more boilerplate to all test cases when we really just care about what data got sent

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totally seems good enough to me

}>;

type EventMap = {
message: MessageEvent<string>;
error: Event;
close: CloseEvent;
open: Event;
type CallbackStore = {
[K in keyof WebSocketEventMap]: Set<(event: WebSocketEventMap[K]) => void>;
};

type CallbackStore = {
[K in keyof EventMap]: Set<(event: EventMap[K]) => void>;
export type MockWebSocket = Omit<WebSocket, "send"> & {
/**
* A version of the WebSocket `send` method that has been pre-wrapped inside
* a Jest mock. Most of the time, you should not be using the Jest mock
* features, and should instead be using the `clientSentData` property from
* the `MockWebSocketServer` type.
*
* The Jest mock functionality only exists to help make sure that the
* client-side socket method got called. It does nothing to make sure that
* the mock server actually received anything.
*/
send: jest.Mock<void, [SocketSendData], unknown>;
};

export function createMockWebSocket(
url: string,
protocol?: string,
): readonly [WebSocket, MockWebSocketServer] {
): readonly [MockWebSocket, MockWebSocketServer] {
if (!url.startsWith("ws://") && !url.startsWith("wss://")) {
throw new Error("URL must start with ws:// or wss://");
}
Expand All @@ -41,7 +48,7 @@ export function createMockWebSocket(

const sentData: SocketSendData[] = [];

const mockSocket: WebSocket = {
const mockSocket: MockWebSocket = {
CONNECTING: 0,
OPEN: 1,
CLOSING: 2,
Expand All @@ -59,12 +66,12 @@ export function createMockWebSocket(
onopen: null,
dispatchEvent: jest.fn(),

send: (data) => {
send: jest.fn((data) => {
if (!isOpen) {
return;
}
sentData.push(data);
},
}),

addEventListener: <E extends WebSocketEventType>(
eventType: E,
Expand Down
Loading