author avatar

syedsibtain

Thu Jul 06 2023

Mocking useSWR directly in test cases is a little complicated and even not recommended at some places. So the ideal way to do it is using a library Mock Service Worker. Here are the steps that helped me solve this.

  1. Create a mock server using setupServer from msw/node:
const server = setupServer(
  rest.get(`/api/channels/C04UPJ9243E/members`, (req, res, ctx) => {
    return res(
      ctx.json({
        members: [{ name: "Sibtain", image: "abcd.jsp", id: 123 }],
      })
    );
  })
);
  1. Start the server and after we run the test, close the server.
beforeAll(() => server.listen());
afterAll(() => server.close());
  1. To ensure clean and isolated tests, reset the request handlers.
afterEach(() => server.resetHandlers());

Now our API connections are taken care of and we can render components and run the test cases.

test('render stuff, () => {
}

Resources: https://mswjs.io/