Skip to content

Commit

Permalink
test: should report emitted errors to the console
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Oct 27, 2021
1 parent 6079818 commit aefbd7c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 13 deletions.
17 changes: 17 additions & 0 deletions src/__tests__/__snapshots__/use.ts.snap
Original file line number Diff line number Diff line change
@@ -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."`;
69 changes: 56 additions & 13 deletions src/__tests__/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
};
});
},
);
Expand All @@ -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<MessageType.ConnectionInit>({
Expand All @@ -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
};
});
});

Expand All @@ -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);
Expand All @@ -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
};
});
},
);
Expand All @@ -429,27 +462,37 @@ 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<MessageType.ConnectionInit>({
type: MessageType.ConnectionInit,
}),
);

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
};
});
});

Expand Down

0 comments on commit aefbd7c

Please sign in to comment.