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
Original issue text (slightly edited, and with added mock-socket version):
I'm trying to use jest-websocket-mock and mock-socket 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 / mock-socket 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, mock-socket 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)
Hi, @vaughnkoch. According to the details, you are using 3 libraries and one of them has a conflict with the api. So right now it’s hard to say what’s the real reason of it.
Can you please create a minimal repo or share a snippet of how to reproduce it?
Hi, I opened an issue in
jest-websocket-mock
; the maintainer said I should reopen the issue inmock-socket
. See romgain/jest-websocket-mock#169 (comment).Original issue text (slightly edited, and with added
mock-socket
version):I'm trying to use
jest-websocket-mock
andmock-socket
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
/mock-socket
to send a message, I get this error: this error:TypeError: ports is not iterable
.Here's what I do in my tests:
Then,
mock-socket
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: