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

Support custom WebSocket clients #2435

Open
1 task
kettanaito opened this issue Feb 20, 2025 · 0 comments
Open
1 task

Support custom WebSocket clients #2435

kettanaito opened this issue Feb 20, 2025 · 0 comments
Labels

Comments

@kettanaito
Copy link
Member

kettanaito commented Feb 20, 2025

Scope

Improves an existing behavior

Compatibility

  • This is a breaking change

Feature description

More context: #2414

Summary

There are cases when tests transitively rely on the WebSocket class that is specification-compliant but is not distributed globally (e.g. the ws package). It would be nice to handle this situation in MSW somehow so it's either supported or made apparent that it won't be.

I believe we should be able to do something like this:

import { ws } from 'msw'
import { WebSocket } from 'ws'

ws.link(url, {
  WebSocket
})

This approach does however have a few gotchas.

Gotchas

It's sensitive to import order. If your tested code gets imported first, it will grab the unpatched version of the client and there's nothing MSW can do about it. Since WebSocket links are created in test suites, perhaps they aren't the best area to support custom WebSocket clients.

This feature would also imply some sort of input validation so users wouldn't provide arbitrary classes hoping that mocking would just work. The WebSocket client must be WHATWG compliant.

@kettanaito kettanaito added this to the WebSocket support milestone Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant