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

Do not know how I can mock my RTK websocket with this lib #129

Open
kasir-barati opened this issue Dec 4, 2023 · 3 comments
Open

Do not know how I can mock my RTK websocket with this lib #129

kasir-barati opened this issue Dec 4, 2023 · 3 comments

Comments

@kasir-barati
Copy link

kasir-barati commented Dec 4, 2023

Hi dear reader

I was looking at this lib and could not find any clue on how you should mock your WebSocket when you are using a library like Redux and SocketIO. Let's say you have a RTK query like this:

endpoints: (build) => ({
  someSubscription: build.query<ResponseSchema[], void>({
    queryFn: async () => {
      return { data: null }
    },
    async onCacheEntryAdded(_, { updateCachedData, cacheDataLoaded, cacheEntryRemoved }) {
      const socket = io('ws://localhost:3000/', {
        withCredentials: true,
        transports: ['websocket'],
      })
      socket.on("message", () => {/* cache received msg in */})
      socket.close()
    },
  }),
})

How can I mock the cached data? is it even possible to do it with this lib? if not with what I can do that?

@kasir-barati
Copy link
Author

I think we need to leverage and utilise two things at the same time, the first one I think is this addon and the other one might be: https://github.com/thoov/mock-socket

After a few weeks I ended up asking same question and landing back to this page again but not luck still 😭

@yannbf
Copy link
Collaborator

yannbf commented May 16, 2024

Hey there! MSW didn't support mocking web sockets before but it seems like it does now! If you're willing to make a contribution to this repo with a reasonable, simple websocket example, we can try to set mocks for it and it can serve as reference for others!

cc @kettanaito

@kettanaito
Copy link
Member

Second what @yannbf is saying here! We are bringing a first-class WebSocket support to MSW, which is already available in a public beta.

npm i msw@next

I've got a few usage examples as well, feel free to reference those.

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

No branches or pull requests

3 participants