Skip to content

Bug: Unhandled errors aren't translated to WebSocket error event #2446

@sfc-gh-jtjhin

Description

@sfc-gh-jtjhin

The impact is that it's not possible to write tests for websocket.onerror.

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Node.js version

v22

Reproduction repository

https://github.com/joshua-tj/msw-ws-onerror-test/

test("Connection should error", () =>
  new Promise((done, fail) => {
    const api = ws.link("wss://example.com/path");

    const server = setupServer(
      api.addEventListener("connection", () => {
        console.log("connecting");
        throw new Error("Connection error");
      })
    );
    server.listen();

    const connection = new WebSocket("wss://example.com/path");
    connection.onerror = (e) => {
      console.log("onerror", e);
      done();
    };

    connection.onclose = (e) => {
      console.log("onclose", e);
    };

    connection.onopen = () => {
      console.log("onopen");
      fail("Connection should not be opened");
    };
  }));

Reproduction steps

yarn
yarn test

Current behavior

Errors and fails tests as onerror handler is not called

Expected behavior

Should not fail since we handle errors and call done() in test

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions