Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using JWM with partysocket causes Jest error: TypeError: ports is not iterable #169

Closed
vaughnkoch opened this issue Jan 29, 2024 · 2 comments

Comments

@vaughnkoch
Copy link

Hi, thanks for this useful library. I don't know whether this is an issue with JWM or partysocket, but I wanted to let you know about it.

I'm trying to use jest-websocket-mock in conjunction with partysocket, which seems like the most updated browser WebSocket library right now: https://docs.partykit.io/reference/partysocket-api/.

When I run my tests and I use jest-websocket-mock to send a message, I get this error: this error: TypeError: ports is not iterable.

Here's what I do in my tests:

server.send({ type: 'system' })

Then, JWM creates the message from this function:

function createMessageEvent(config) {
  var type = config.type;
  var origin = config.origin;
  var data = config.data;
  var target = config.target;
  var messageEvent = new MessageEvent(type, {
    data: data,
    origin: origin
  });

  if (target) {
    messageEvent.target = target;
    messageEvent.srcElement = target;
    messageEvent.currentTarget = target;
  }

  return messageEvent;
}

Then partysocket clones the event with this code (it actually uses cloneEventNode because Jest is running in the node environment).

function cloneEventBrowser(e) {
  return new e.constructor(e.type, e);
}

function cloneEventNode(e) {
  if ("data" in e) {
    const evt2 = new MessageEvent(e.type, e);
    return evt2;
  }
  if ("code" in e || "reason" in e) {
    const evt2 = new CloseEvent(
      // @ts-expect-error we need to fix event/listener types
      e.code || 1999,
      // @ts-expect-error we need to fix event/listener types
      e.reason || "unknown reason",
      e
    );
    return evt2;
  }
  if ("error" in e) {
    const evt2 = new ErrorEvent(e.error, e);
    return evt2;
  }
  const evt = new Event(e.type, e);
  return evt;
}

The above code fails on this line:

const evt2 = new MessageEvent(e.type, e);

I looked at the e parameter and e.ports, is null which explains the error. This may be because of a different environment, but do you have any suggestions on how to fix this?

Note: I also tried to force the environment to Node (instead of the browser), but got this cryptic error:

TypeError: The "event" argument must be an instance of Event. Received an instance of Event

  at WebSocket._handleOpen (node_modules/partysocket/dist/index.js:444:10)
  at node_modules/mock-socket/dist/mock-socket.js:859:16
      at Array.forEach (<anonymous>)
  at WebSocket.dispatchEvent (node_modules/mock-socket/dist/mock-socket.js:855:13)
  at WebSocket.delayCallback (node_modules/mock-socket/dist/mock-socket.js:1522:16)
  at Timeout._onTimeout (node_modules/mock-socket/dist/mock-socket.js:757:58)

Versions:

 "partysocket": "0.0.25",
 "jest-websocket-mock": "2.4.0",
@romgain
Copy link
Owner

romgain commented Jan 30, 2024

Hey @vaughnkoch , thanks a lot for reporting this!
It looks like this is a bug with mock-socket, the library we use to mock the ws apis in node.
Would you mind reporting this again on their bug tracker?

Thank you!

@romgain romgain closed this as completed Jan 30, 2024
@vaughnkoch
Copy link
Author

Will do, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants