You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
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?
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 withpartysocket
, 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:
Then, JWM creates the message from this function:
Then
partysocket
clones the event with this code (it actually uses cloneEventNode because Jest is running in the node environment).The above code fails on this line:
I looked at the
e
parameter ande.ports
, isnull
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:
Versions:
The text was updated successfully, but these errors were encountered: