diff --git a/src/__tests__/__snapshots__/use.ts.snap b/src/__tests__/__snapshots__/use.ts.snap new file mode 100644 index 00000000..af21f5a0 --- /dev/null +++ b/src/__tests__/__snapshots__/use.ts.snap @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`fastify-websocket should limit the server emitted error message size 1`] = `"Internal error emitted on the WebSocket server. Please check your implementation."`; + +exports[`fastify-websocket should limit the socket emitted error message size 1`] = `"Internal error emitted on a WebSocket socket. Please check your implementation."`; + +exports[`fastify-websocket should report server emitted errors to clients by closing the connection 1`] = `"Internal error emitted on the WebSocket server. Please check your implementation."`; + +exports[`fastify-websocket should report socket emitted errors to clients by closing the connection 1`] = `"Internal error emitted on a WebSocket socket. Please check your implementation."`; + +exports[`ws should limit the server emitted error message size 1`] = `"Internal error emitted on the WebSocket server. Please check your implementation."`; + +exports[`ws should limit the socket emitted error message size 1`] = `"Internal error emitted on a WebSocket socket. Please check your implementation."`; + +exports[`ws should report server emitted errors to clients by closing the connection 1`] = `"Internal error emitted on the WebSocket server. Please check your implementation."`; + +exports[`ws should report socket emitted errors to clients by closing the connection 1`] = `"Internal error emitted on a WebSocket socket. Please check your implementation."`; diff --git a/src/__tests__/use.ts b/src/__tests__/use.ts index 60812d82..bfa1775b 100644 --- a/src/__tests__/use.ts +++ b/src/__tests__/use.ts @@ -360,6 +360,10 @@ for (const { tServer, skipUWS, startTServer } of tServers) { async () => { const { url, server } = await startTServer(); + // errors musts be reported to the console + const consoleErrorFn = jest.fn(); + console.error = consoleErrorFn; + const client = await createTClient(url); const emittedError = new Error("I'm a teapot"); @@ -370,6 +374,14 @@ for (const { tServer, skipUWS, startTServer } of tServers) { expect(event.code).toBe(CloseCode.InternalServerError); // CloseCode.InternalServerError: Internal server error expect(event.reason).toBe(emittedError.message); expect(event.wasClean).toBeTruthy(); // because the server reported the error + + expect(consoleErrorFn).toBeCalledTimes(1); + expect(consoleErrorFn.mock.calls[0][0]).toMatchSnapshot(); + expect(consoleErrorFn.mock.calls[0][1]).toBe(emittedError); + + console.error = () => { + // silence again + }; }); }, ); @@ -378,6 +390,10 @@ for (const { tServer, skipUWS, startTServer } of tServers) { skipUWS('should limit the server emitted error message size', async () => { const { url, server, waitForClient } = await startTServer(); + // errors musts be reported to the console + const consoleErrorFn = jest.fn(); + console.error = consoleErrorFn; + const client = await createTClient(url); client.ws.send( stringifyMessage({ @@ -387,18 +403,24 @@ for (const { tServer, skipUWS, startTServer } of tServers) { await waitForClient(); - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - server!.emit( - 'error', - new Error( - 'i am exactly 124 characters long i am exactly 124 characters long i am exactly 124 characters long i am exactly 124 characte', - ), + const emittedError = new Error( + 'i am exactly 124 characters long i am exactly 124 characters long i am exactly 124 characters long i am exactly 124 characte', ); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + server!.emit('error', emittedError); await client.waitForClose((event) => { expect(event.code).toBe(CloseCode.InternalServerError); expect(event.reason).toBe('Internal server error'); expect(event.wasClean).toBeTruthy(); // because the server reported the error + + expect(consoleErrorFn).toBeCalledTimes(1); + expect(consoleErrorFn.mock.calls[0][0]).toMatchSnapshot(); + expect(consoleErrorFn.mock.calls[0][1]).toBe(emittedError); + + console.error = () => { + // silence again + }; }); }); @@ -408,10 +430,13 @@ for (const { tServer, skipUWS, startTServer } of tServers) { async () => { const { url, waitForClient } = await startTServer(); + // errors musts be reported to the console + const consoleErrorFn = jest.fn(); + console.error = consoleErrorFn; + const client = await createTClient(url); const emittedError = new Error("I'm a teapot"); - await waitForClient((client) => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion client.socket!.emit('error', emittedError); @@ -421,6 +446,14 @@ for (const { tServer, skipUWS, startTServer } of tServers) { expect(event.code).toBe(CloseCode.InternalServerError); // CloseCode.InternalServerError: Internal server error expect(event.reason).toBe(emittedError.message); expect(event.wasClean).toBeTruthy(); // because the server reported the error + + expect(consoleErrorFn).toBeCalledTimes(1); + expect(consoleErrorFn.mock.calls[0][0]).toMatchSnapshot(); + expect(consoleErrorFn.mock.calls[0][1]).toBe(emittedError); + + console.error = () => { + // silence again + }; }); }, ); @@ -429,6 +462,10 @@ for (const { tServer, skipUWS, startTServer } of tServers) { skipUWS('should limit the socket emitted error message size', async () => { const { url, waitForClient } = await startTServer(); + // errors musts be reported to the console + const consoleErrorFn = jest.fn(); + console.error = consoleErrorFn; + const client = await createTClient(url); client.ws.send( stringifyMessage({ @@ -436,20 +473,26 @@ for (const { tServer, skipUWS, startTServer } of tServers) { }), ); + const emittedError = new Error( + 'i am exactly 124 characters long i am exactly 124 characters long i am exactly 124 characters long i am exactly 124 characte', + ); await waitForClient((client) => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - client.socket!.emit( - 'error', - new Error( - 'i am exactly 124 characters long i am exactly 124 characters long i am exactly 124 characters long i am exactly 124 characte', - ), - ); + client.socket!.emit('error', emittedError); }); await client.waitForClose((event) => { expect(event.code).toBe(CloseCode.InternalServerError); expect(event.reason).toBe('Internal server error'); expect(event.wasClean).toBeTruthy(); // because the server reported the error + + expect(consoleErrorFn).toBeCalledTimes(1); + expect(consoleErrorFn.mock.calls[0][0]).toMatchSnapshot(); + expect(consoleErrorFn.mock.calls[0][1]).toBe(emittedError); + + console.error = () => { + // silence again + }; }); });