-
-
Notifications
You must be signed in to change notification settings - Fork 518
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
@@ -18,9 +18,13 @@ import { createFallbackStop } from './stop/createFallbackStop' | |||
import { devUtils } from '~/core/utils/internal/devUtils' | |||
import { SetupApi } from '~/core/SetupApi' | |||
import { mergeRight } from '~/core/utils/internal/mergeRight' | |||
import { LifeCycleEventsMap } from '~/core/sharedOptions' | |||
import type { LifeCycleEventsMap } from '~/core/sharedOptions' | |||
import type { WebSocketHandler } from '~/core/handlers/WebSocketHandler' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Do you want to bundle the handlers together or should these handler placed in the ~/core/ws
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't understand what you mean. Can you elaborate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the related imports to WebSockets are coming from the core/ws
. So i'm only suggesting that the WebSocketHandler
should we imported from this path as well.
Something like this;
import type { WebSocketHandler } from '~/core/ws/handlers/WebSocketHandler'
...
import { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'
import { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'
import { attachWebSocketLogger } from '~/core/ws/utils/attachWebSocketLogger'
Or do you want to separate these functionalities and types?
📦
|
📦
|
📦
|
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 |
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