Fix jsdom 28/29 WebSocket cross-realm Event incompatibility#1221
Open
MatthijsBurgh wants to merge 6 commits into
Open
Fix jsdom 28/29 WebSocket cross-realm Event incompatibility#1221MatthijsBurgh wants to merge 6 commits into
MatthijsBurgh wants to merge 6 commits into
Conversation
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>
Contributor
Author
|
@EzraBrooks I have rebased the branch of #1161. Please review this one. |
There was a problem hiding this comment.
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
WebSocketTransportFactoryto bypass the nativeWebSockettransport under jsdom. - Updated the
WebSocketTransportFactorytest to expectWsWebSocketTransportin 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.