Skip to content

Fix jsdom 28/29 WebSocket cross-realm Event incompatibility#1221

Open
MatthijsBurgh wants to merge 6 commits into
dependabot/npm_and_yarn/jsdom-29.1.1from
copilot/sub-pr-1157
Open

Fix jsdom 28/29 WebSocket cross-realm Event incompatibility#1221
MatthijsBurgh wants to merge 6 commits into
dependabot/npm_and_yarn/jsdom-29.1.1from
copilot/sub-pr-1157

Conversation

@MatthijsBurgh

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI and others added 6 commits June 2, 2026 13:10
Co-authored-by: MatthijsBurgh <18014833+MatthijsBurgh@users.noreply.github.com>
Add detection for jsdom environment in WebSocketTransportFactory to
prevent using jsdom's native WebSocket which has cross-realm Event
issues. This forces the use of the ws package in jsdom environments,
avoiding the ERR_INVALID_ARG_TYPE error when dispatching events.

Co-authored-by: MatthijsBurgh <18014833+MatthijsBurgh@users.noreply.github.com>
Update transport test to expect WsWebSocketTransport in jsdom
environments, as the fix intentionally uses ws package instead of
native WebSocket to avoid cross-realm Event issues.

Co-authored-by: MatthijsBurgh <18014833+MatthijsBurgh@users.noreply.github.com>
Enhanced jsdom environment detection with multiple checks:
- Try-catch for navigator.userAgent check (avoids circular lint rules)
- Window constructor name check
- jsdom-specific Symbol check in globalThis

Also simplified test name per code review feedback.

Co-authored-by: MatthijsBurgh <18014833+MatthijsBurgh@users.noreply.github.com>
Convert windowConstructorName to lowercase before comparison,
simplifying the check from two comparisons to one.

Co-authored-by: MatthijsBurgh <18014833+MatthijsBurgh@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 2, 2026 11:18
@MatthijsBurgh MatthijsBurgh changed the base branch from develop to dependabot/npm_and_yarn/jsdom-29.1.1 June 2, 2026 11:19
@MatthijsBurgh MatthijsBurgh requested a review from EzraBrooks June 2, 2026 11:19
@MatthijsBurgh

Copy link
Copy Markdown
Contributor Author

@EzraBrooks I have rebased the branch of #1161. Please review this one.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the WebSocket transport selection logic to avoid jsdom (v28/29) cross-realm WebSocket/Event incompatibilities by preferring the ws-based transport when running under jsdom, and aligns the unit test expectation with that behavior.

Changes:

  • Added jsdom environment detection in WebSocketTransportFactory to bypass the native WebSocket transport under jsdom.
  • Updated the WebSocketTransportFactory test to expect WsWebSocketTransport in the jsdom test environment.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
packages/roslib/src/core/transport/WebSocketTransportFactory.ts Adds jsdom detection and routes jsdom to ws transport instead of native WebSocket.
packages/roslib/test/transport.test.ts Updates the factory test to reflect jsdom now using WsWebSocketTransport.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +651 to +655
it("uses WsWebSocketTransport in jsdom environment", async () => {
vi.stubGlobal("WebSocket", WebSocket);
expect(typeof WebSocket).toBe("function");

// In jsdom environment, should use WsWebSocketTransport to avoid cross-realm Event issues
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

Successfully merging this pull request may close these issues.

3 participants