dev-test: JSDOM's WebSocket in the browser environment

The browser environment replaces Node's Event with JSDOM's. Node's WebSocket
dispatches through Node's EventTarget, which rejects that Event, so no open,
message or error reached a listener and no connection ever came up: an
ApiPromise against a live node never became ready. JSDOM's WebSocket now
replaces Node's, as Event and CustomEvent already do. A spec completes a
handshake with a local server and receives a frame.
This commit is contained in:
Satoshi Qazi Muhammed committed 2026-10-11 06:06:19 +04:00
1 parent f0ba63750f
commit 13a6acf80d
2 files changed
+62 -1

No files matched your search

+14 -1
View File
@@ -9,8 +9,13 @@ import { JSDOM } from 'jsdom';
* Constructors that Node defines as well. A DOM node only accepts an event
* created in its own realm, so in the browser environment the JSDOM ones
* replace Node's, as they do under jest-environment-jsdom.
*
* WebSocket follows from that: Node's own dispatches its events through
* Node's EventTarget, which rejects the JSDOM Event that replaced Node's, so
* no open, message or error ever reaches a listener and a connection never
* comes up. JSDOM's WebSocket dispatches events from its own realm.
*/
export const BROWSER_OVERRIDES: readonly string[] = ['CustomEvent', 'Event'];
export const BROWSER_OVERRIDES: readonly string[] = ['CustomEvent', 'Event', 'WebSocket'];
/**
* JSDOM does no layout, so no element ever changes size and the callback
@@ -62,6 +67,13 @@ function events (window: DOMWindow) {
};
}
/** Network, see BROWSER_OVERRIDES for why WebSocket is JSDOM's */
function network (window: DOMWindow) {
return {
WebSocket: window.WebSocket
};
}
/** Styles, geometry and the observers */
function layout (window: DOMWindow) {
return {
@@ -90,6 +102,7 @@ export function browser () {
...nodes(window),
...events(window),
...layout(window),
...network(window),
// All HTML Elements that are defined on the JSDOM window object.
// (we copied as-is from the types definition). We cannot get this
// via Object.keys(window).filter(...) so we have to specify explicitly