Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions tests/websocket-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@
wsh.close();
});

test("WebSocketHelper handles non-Error objects thrown by handlers", async () => {
const wsh = new WebSocketHelper("ws://localhost");
await wsh.connect();

let caughtError: Error | null = null;
wsh.onError((err) => {
caughtError = err;
});

wsh.on("string-error", () => {
throw "something went wrong";

Check warning on line 82 in tests/websocket-helper.test.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Expected an error object to be thrown.

See more on https://sonarcloud.io/project/issues?id=sebamar88_bytekit&issues=AZ0A7jkzI2M8eUiJU8Ka&open=AZ0A7jkzI2M8eUiJU8Ka&pullRequest=10
});

// @ts-expect-error - Test type override
wsh.ws._receive({ type: "string-error", data: {} });

assert.ok(caughtError instanceof Error);
assert.equal(caughtError?.message, "something went wrong");

wsh.close();
});

test("WebSocketHelper receives messages and notifies subscribers", async () => {
const wsh = new WebSocketHelper("ws://localhost");
await wsh.connect();
Expand Down Expand Up @@ -283,3 +305,31 @@
wsh.close();
console.error = originalError;
});

test("WebSocketHelper continues to next handler if one fails", async () => {
const wsh = new WebSocketHelper("ws://localhost");
await wsh.connect();

let handler2Called = false;
let errorNotified = false;

wsh.onError(() => {
errorNotified = true;
});

wsh.on("multi", () => {
throw new Error("First handler failed");
});

wsh.on("multi", () => {
handler2Called = true;
});

// @ts-expect-error - Test type override
wsh.ws._receive({ type: "multi", data: { ok: true } });

assert.equal(errorNotified, true);
assert.equal(handler2Called, true);

wsh.close();
});
Loading