Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move WebSocketCtor reference inside createSocket method to support WebSocket mocking #5315

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ccpu
Copy link

@ccpu ccpu commented Mar 14, 2025

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behavior

The WebSocketCtor is defined at module scope which captures WebSocket reference before mocking libraries like MSW can intercept it. Since bundlers (webpack/vite) typically load engine.io-client before MSW, the original WebSocket implementation is used even when mocks are applied later.

New behavior

WebSocketCtor reference is moved inside the createSocket method, ensuring it always uses the current value of globalThis.WebSocket at call time. This allows mocking libraries to properly intercept WebSocket connections in Socket.IO.

Other information (e.g. related issues)

Fixes mswjs/socket.io-binding#12
I've tested this solution with both webpack and vite, confirming it resolves the issue in both bundlers.

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.

MSW intercepts plain WebSocket but fails to intercept Socket.IO connections
1 participant