-
-
Notifications
You must be signed in to change notification settings - Fork 529
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
feat: support mocking WebSocket APIs #2011
Conversation
📦
|
📦
|
This works really well for our needs; thanks for all the hard work! Is there a timeline for when it might be included in the main release branch? I noticed that none of this functionality is included in release |
@kesupile, happy to hear that! The WebSocket support is a release candidate right now. I'm planning on publishing it this year after I wrap up the remaining bits. You can use the release candidate for now, it will land on
Let me know if you have any feedback while using the new API! |
Released: v2.6.0 🎉This has been released in v2.6.0! Make sure to always update to the latest version ( Predictable release automation by @ossjs/release. |
Hi @kettanaito |
Hi, @AlexDroll. Thanks for sharing this. Adding a new type to the union isn't a breaking change. I will update the docs to mention that the method now returns all handlers. PR: mswjs/mswjs.io#427 |
Hi, I just updated from MSW 2.3.5 to 2.6.5. and there is effectively a breaking change on the listHandlers() method.
Otherwise, you'll have this issue :
|
You can avoid a cast by narrowing it: worker.listHandlers().forEach(handler => {
if ('isUsed' in handler) {
// Narrowed to a `RequestHandler`
} else {
// Narrowed to a `WebSocketHandler`
}
}); Or in a helper: function isRequestHandler(handler: RequestHandler | WebSocketHandler): handler is RequestHandler {
return 'isUsed' in handler;
} This works because |
The answer from @pleunv is almost there. Don't rely on properties, check the class instance: import { RequestHandler } from 'msw'
const httpHandlers = worker.listHandlers().filter((handler) => {
return handler instanceof RequestHandler
})
console.log(httpHandlers) |
I generally avoid importing from |
Could you update the documentation regarding this issue ? |
@pleunv, unless you are running a build-less app, I'd expect your bundler to tree-shake anything you don't use from @spawnrider, where do you think this update belongs best? It's hard to assume everyone needs to do the That being said, I agree this needs to be mentioned. I can propose a new section to |
That's seems pretty good to me. I just managed to print the handlers list using this code (and not using a cast) :
|
@spawnrider, please review mswjs/mswjs.io#431. |
Roadmap
Extend.Handler
inRequestHandler
. This wayHttpHandler
andGraphQLHandler
will be assignable to typeHandler
, andArray<Handler>
can annotate both HTTP and WebSocket handlersRequestHandler
relies on caching to clone requests, parsed result, etc. Not enough common ground to reuse between it andWebSocketHandler
. WS handler is better off as a standalone class (doesn't supportonce
anyway).handleWebSocketEvent()
inSetupWorkerApi
andSetupServerApi
.instanceof RequestHandler
to skip WebSocket handlers.@mswjs/interceptors
that shipsWebSocketInterceptor
.data
argument of message listeners must be annotated across the board (includes annotating theMessageEvent
in the Interceptors).data
you receive is stillstring | Blob | ArrayBuffer
. You cannot just cast it to something else. Instead, introduce a custom parser that does that casting, as well as the runtimetypeof
checks. That's the way.pnpm test:modules:node
hangs forever onsetupServer()
without the explicitprocess.exit()
in the generatedruntime.*
scripts..use()
override tests (i.e.event.stopImmediatePropagation()
)..start()
/.listen()
calls.server.close()
API (a task for Interceptors, here just add tests).onUnhandledRequest()
(or similar) for unhandled WebSocket connections. Technically, there are no requests, so the existing method reads weird. WebSocket Support Beta #2010 (comment).broadcast()
not broadcasting from N(1) -> N+ but working from N+ -> N+. (discovered in add WebSocket + ws example examples#111) (fix(WebSocketClientManager): use localStorage for clients persistence #2127).this: WebSocket
in theclient
/server
event listeners.localStorage
(fix: fix: purge persisted clients on page reload #2133)console.log
(msw/src/core/ws/WebSocketClientManager.ts
Line 71 in f692222
event.preventDefault()
. (proof)WebSocketClientManager
still has the "first-tab-only" issue with multiple tabs.wss://localhost:*
(*
as port throws onpath-to-regexp
). Quite likely fixed by Support URLPattern as request predicate #1921DOMException: Key already exists in the object store.
error from IndexedDB (concurrent writes to the db).(node:15076) MaxListenersExceededWarning: Possible EventTarget memory leak detected
inpnpm test:unit src/core/ws/WebSocketClientManager.test.ts