From 3a5736c8313c296f6cd0228372df938b18e1182d Mon Sep 17 00:00:00 2001 From: onmax Date: Wed, 22 Jan 2025 16:23:46 +0100 Subject: [PATCH] docs: add WebSocket to `msw` features --- docs/guide/mocking.md | 80 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 11 deletions(-) diff --git a/docs/guide/mocking.md b/docs/guide/mocking.md index 64ec306623dd..fa65d16e14e7 100644 --- a/docs/guide/mocking.md +++ b/docs/guide/mocking.md @@ -430,17 +430,20 @@ it('can return a value multiple times', () => { ## Requests -Because Vitest runs in Node, mocking network requests is tricky; web APIs are not available, so we need something that will mimic network behavior for us. We recommend [Mock Service Worker](https://mswjs.io/) to accomplish this. It will let you mock both `REST` and `GraphQL` network requests, and is framework agnostic. +Because Vitest runs in Node, mocking network requests is tricky; web APIs are not available, so we need something that will mimic network behavior for us. We recommend [Mock Service Worker](https://mswjs.io/) to accomplish this. It allows you to mock `http`, `WebSocket` and `GraphQL` network requests, and is framework agnostic. Mock Service Worker (MSW) works by intercepting the requests your tests make, allowing you to use it without changing any of your application code. In-browser, this uses the [Service Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API). In Node.js, and for Vitest, it uses the [`@mswjs/interceptors`](https://github.com/mswjs/interceptors) library. To learn more about MSW, read their [introduction](https://mswjs.io/docs/) ### Configuration You can use it like below in your [setup file](/config/#setupfiles) -```js + +::: code-group + +```js [HTTP Setup] import { afterAll, afterEach, beforeAll } from 'vitest' import { setupServer } from 'msw/node' -import { graphql, http, HttpResponse } from 'msw' +import { http, HttpResponse } from 'msw' const posts = [ { @@ -458,28 +461,83 @@ export const restHandlers = [ }), ] +const server = setupServer(...restHandlers) + +// Start server before all tests +beforeAll(() => server.listen({ onUnhandledRequest: 'error' })) + +// Close server after all tests +afterAll(() => server.close()) + +// Reset handlers after each test for test isolation +afterEach(() => server.resetHandlers()) +``` + +```js [GrapQL Setup] +import { afterAll, afterEach, beforeAll } from 'vitest' +import { setupServer } from 'msw/node' +import { graphql, HttpResponse } from 'msw' + +const posts = [ + { + userId: 1, + id: 1, + title: 'first post title', + body: 'first post body', + }, + // ... +] + const graphqlHandlers = [ graphql.query('ListPosts', () => { - return HttpResponse.json( - { - data: { posts }, - }, - ) + return HttpResponse.json({ + data: { posts }, + }) }), ] -const server = setupServer(...restHandlers, ...graphqlHandlers) +const server = setupServer(...graphqlHandlers) // Start server before all tests beforeAll(() => server.listen({ onUnhandledRequest: 'error' })) -// Close server after all tests +// Close server after all tests afterAll(() => server.close()) -// Reset handlers after each test `important for test isolation` +// Reset handlers after each test for test isolation afterEach(() => server.resetHandlers()) ``` +```js [WebSocket Setup] +import { afterAll, afterEach, beforeAll } from 'vitest' +import { setupServer } from 'msw/node' +import { ws } from 'msw' + +const chat = ws.link('wss://chat.example.com') + +const wsHandlers = [ + chat.addEventListener('connection', ({ client }) => { + client.addEventListener('message', (event) => { + console.log('Received message from client:', event.data) + // Echo the received message back to the client + client.send(`Server received: ${event.data}`) + }) + }), +] + +const server = setupServer(...wsHandlers) + +// Start server before all tests +beforeAll(() => server.listen({ onUnhandledRequest: 'error' })) + +// Close server after all tests +afterAll(() => server.close()) + +// Reset handlers after each test for test isolation +afterEach(() => server.resetHandlers()) +``` +::: + > Configuring the server with `onUnhandledRequest: 'error'` ensures that an error is thrown whenever there is a request that does not have a corresponding request handler. ### More