mswjs / interceptors

Low-level network interception library.
https://npm.im/@mswjs/interceptors
MIT License
565 stars 127 forks source link

Proper documentation on how to use it #580

Closed nemonemi closed 5 months ago

nemonemi commented 5 months ago

Hello, I understand that this is a low-level library, but does that mean that we shouldn't be using it at all?

Looking at the documentation it is not clear on how to actually mock the webSocket subscription.

Could you please provide a complete solution?

kettanaito commented 5 months ago

Hi, @nemonemi.

You can certainly use this library standalone, if you so wish. Just be mindful of what this library doesn't do (things you'd have to build yourself, like request matching). For general purpose mocking, I highly recommend using MSW, which uses Interceptors for mocking requests in Node.js.

WebSocket interception is described as-is. The README mentions everything you need to intercept a WebSocket connection and mock incoming/outgoing events.

The more "complete" solution is the ws API coming to MSW. Learn more and also how to use it right here: https://github.com/mswjs/msw/discussions/2010.