import WS from "../websocket"; import "../matchers"; let server: WS, client: WebSocket; beforeEach(async () => { server = new WS("ws://localhost:1234"); client = new WebSocket("ws://localhost:1234"); await server.connected; }); afterEach(() => { WS.clean(); }); describe(".toReceiveMessage", () => { it("passes when the websocket server receives the expected message", async () => { client.send("hello there"); await expect(server).toReceiveMessage("hello there"); }); it("passes when the websocket server receives the expected message with custom timeout", async () => { setTimeout(() => { client.send("hello there"); }, 2000); await expect(server).toReceiveMessage("hello there", { timeout: 3000 }); }); it("passes when the websocket server receives the expected JSON message", async () => { const jsonServer = new WS("ws://localhost:9876", { jsonProtocol: true }); const jsonClient = new WebSocket("ws://localhost:9876"); await jsonServer.connected; jsonClient.send(`{"answer":42}`); await expect(jsonServer).toReceiveMessage({ answer: 42 }); }); it("fails when called with an expected argument that is not a valid WS", async () => { expect.hasAssertions(); await expect(expect("boom").toReceiveMessage("hello there")).rejects .toThrowErrorMatchingInlineSnapshot(` "expect(WS).toReceiveMessage(expected) Expected the websocket object to be a valid WS mock. Received: string \\"boom\\"" `); }); it("fails when the WS server does not receive the expected message", async () => { expect.hasAssertions(); await expect(expect(server).toReceiveMessage("hello there")).rejects .toThrowErrorMatchingInlineSnapshot(` "expect(WS).toReceiveMessage(expected) Expected the websocket server to receive a message, but it didn't receive anything in 1000ms." `); }); it("fails when the WS server does not receive the expected message with custom timeout", async () => { expect.hasAssertions(); await expect( expect(server).toReceiveMessage("hello there", { timeout: 3000 }) ).rejects.toThrowErrorMatchingInlineSnapshot(` "expect(WS).toReceiveMessage(expected) Expected the websocket server to receive a message, but it didn't receive anything in 3000ms." `); }); it("fails when the WS server receives a different message", async () => { expect.hasAssertions(); client.send("hello there"); await expect(expect(server).toReceiveMessage("HI!")).rejects .toThrowErrorMatchingInlineSnapshot(` "expect(WS).toReceiveMessage(expected) Expected the next received message to equal: \\"HI!\\" Received: \\"hello there\\" Difference: - Expected + Received - HI! + hello there" `); }); it("fails when expecting a JSON message but the server is not configured for JSON protocols", async () => { expect.hasAssertions(); client.send(`{"answer":42}`); await expect(expect(server).toReceiveMessage({ answer: 42 })).rejects .toThrowErrorMatchingInlineSnapshot(` "expect(WS).toReceiveMessage(expected) Expected the next received message to equal: {\\"answer\\": 42} Received: \\"{\\\\\\"answer\\\\\\":42}\\" Difference: Comparing two different types of values. Expected object but received string." `); }); }); describe(".not.toReceiveMessage", () => { it("passes when the websocket server doesn't receive the expected message", async () => { client.send("hello there"); await expect(server).not.toReceiveMessage("What's up?"); }); it("fails when called with an expected argument that is not a valid WS", async () => { expect.hasAssertions(); await expect(expect("boom").not.toReceiveMessage("hello there")).rejects .toThrowErrorMatchingInlineSnapshot(` "expect(WS).not.toReceiveMessage(expected) Expected the websocket object to be a valid WS mock. Received: string \\"boom\\"" `); }); it("fails when the WS server doesn't receive any messages", async () => { expect.hasAssertions(); await expect(expect(server).not.toReceiveMessage("hello there")).rejects .toThrowErrorMatchingInlineSnapshot(` "expect(WS).not.toReceiveMessage(expected) Expected the websocket server to receive a message, but it didn't receive anything in 1000ms." `); }); it("fails when the WS server receives the un-expected message", async () => { expect.hasAssertions(); client.send("hello there"); await expect(expect(server).not.toReceiveMessage("hello there")).rejects .toThrowErrorMatchingInlineSnapshot(` "expect(WS).not.toReceiveMessage(expected) Expected the next received message to not equal: \\"hello there\\" Received: \\"hello there\\"" `); }); }); describe(".toHaveReceivedMessages", () => { it("passes when the websocket server received the expected messages", async () => { client.send("hello there"); client.send("how are you?"); client.send("good?"); await server.nextMessage; await server.nextMessage; await server.nextMessage; expect(server).toHaveReceivedMessages(["hello there", "good?"]); }); it("passes when the websocket server received the expected JSON messages", async () => { const jsonServer = new WS("ws://localhost:9876", { jsonProtocol: true }); const jsonClient = new WebSocket("ws://localhost:9876"); await jsonServer.connected; jsonClient.send(`{"type":"GREETING","payload":"hello there"}`); jsonClient.send(`{"type":"GREETING","payload":"how are you?"}`); jsonClient.send(`{"type":"GREETING","payload":"good?"}`); await jsonServer.nextMessage; await jsonServer.nextMessage; await jsonServer.nextMessage; expect(jsonServer).toHaveReceivedMessages([ { type: "GREETING", payload: "good?" }, { type: "GREETING", payload: "hello there" }, ]); }); it("fails when the websocket server did not receive the expected messages", async () => { client.send("hello there"); client.send("how are you?"); client.send("good?"); await server.nextMessage; await server.nextMessage; await server.nextMessage; expect(() => { expect(server).toHaveReceivedMessages(["hello there", "'sup?"]); }).toThrowErrorMatchingInlineSnapshot(` "expect(WS).toHaveReceivedMessages(expected) Expected the WS server to have received the following messages: [\\"hello there\\", \\"'sup?\\"] Received: [\\"hello there\\", \\"how are you?\\", \\"good?\\"] " `); }); it("fails when called with an expected argument that is not a valid WS", async () => { expect(() => { expect("boom").toHaveReceivedMessages(["hello there"]); }).toThrowErrorMatchingInlineSnapshot(` "expect(WS).toHaveReceivedMessages(expected) Expected the websocket object to be a valid WS mock. Received: string \\"boom\\"" `); }); }); describe(".not.toHaveReceivedMessages", () => { it("passes when the websocket server received none of the specified messages", async () => { client.send("hello there"); client.send("how are you?"); client.send("good?"); await server.nextMessage; await server.nextMessage; await server.nextMessage; expect(server).not.toHaveReceivedMessages(["'sup?", "U good?"]); }); it("fails when the websocket server received at least one unexpected message", async () => { client.send("hello there"); client.send("how are you?"); client.send("good?"); await server.nextMessage; await server.nextMessage; await server.nextMessage; expect(() => { expect(server).not.toHaveReceivedMessages([ "'sup?", "U good?", "hello there", ]); }).toThrowErrorMatchingInlineSnapshot(` "expect(WS).not.toHaveReceivedMessages(expected) Expected the WS server to not have received the following messages: [\\"'sup?\\", \\"U good?\\", \\"hello there\\"] But it received: [\\"hello there\\", \\"how are you?\\", \\"good?\\"]" `); }); it("fails when called with an expected argument that is not a valid WS", async () => { expect(() => { expect("boom").not.toHaveReceivedMessages(["hello there"]); }).toThrowErrorMatchingInlineSnapshot(` "expect(WS).not.toHaveReceivedMessages(expected) Expected the websocket object to be a valid WS mock. Received: string \\"boom\\"" `); }); });