Skip to content

Commit 5e001ee

Browse files
authored
docs: add WebSocket to msw features (#7323)
1 parent ce6af70 commit 5e001ee

File tree

1 file changed

+69
-11
lines changed

1 file changed

+69
-11
lines changed

docs/guide/mocking.md

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -430,17 +430,20 @@ it('can return a value multiple times', () => {
430430

431431
## Requests
432432

433-
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.
433+
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.
434434

435435
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/)
436436

437437
### Configuration
438438

439439
You can use it like below in your [setup file](/config/#setupfiles)
440-
```js
440+
441+
::: code-group
442+
443+
```js [HTTP Setup]
441444
import { afterAll, afterEach, beforeAll } from 'vitest'
442445
import { setupServer } from 'msw/node'
443-
import { graphql, http, HttpResponse } from 'msw'
446+
import { http, HttpResponse } from 'msw'
444447

445448
const posts = [
446449
{
@@ -458,28 +461,83 @@ export const restHandlers = [
458461
}),
459462
]
460463

464+
const server = setupServer(...restHandlers)
465+
466+
// Start server before all tests
467+
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
468+
469+
// Close server after all tests
470+
afterAll(() => server.close())
471+
472+
// Reset handlers after each test for test isolation
473+
afterEach(() => server.resetHandlers())
474+
```
475+
476+
```js [GrapQL Setup]
477+
import { afterAll, afterEach, beforeAll } from 'vitest'
478+
import { setupServer } from 'msw/node'
479+
import { graphql, HttpResponse } from 'msw'
480+
481+
const posts = [
482+
{
483+
userId: 1,
484+
id: 1,
485+
title: 'first post title',
486+
body: 'first post body',
487+
},
488+
// ...
489+
]
490+
461491
const graphqlHandlers = [
462492
graphql.query('ListPosts', () => {
463-
return HttpResponse.json(
464-
{
465-
data: { posts },
466-
},
467-
)
493+
return HttpResponse.json({
494+
data: { posts },
495+
})
468496
}),
469497
]
470498

471-
const server = setupServer(...restHandlers, ...graphqlHandlers)
499+
const server = setupServer(...graphqlHandlers)
472500

473501
// Start server before all tests
474502
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
475503

476-
// Close server after all tests
504+
// Close server after all tests
477505
afterAll(() => server.close())
478506

479-
// Reset handlers after each test `important for test isolation`
507+
// Reset handlers after each test for test isolation
480508
afterEach(() => server.resetHandlers())
481509
```
482510

511+
```js [WebSocket Setup]
512+
import { afterAll, afterEach, beforeAll } from 'vitest'
513+
import { setupServer } from 'msw/node'
514+
import { ws } from 'msw'
515+
516+
const chat = ws.link('wss://chat.example.com')
517+
518+
const wsHandlers = [
519+
chat.addEventListener('connection', ({ client }) => {
520+
client.addEventListener('message', (event) => {
521+
console.log('Received message from client:', event.data)
522+
// Echo the received message back to the client
523+
client.send(`Server received: ${event.data}`)
524+
})
525+
}),
526+
]
527+
528+
const server = setupServer(...wsHandlers)
529+
530+
// Start server before all tests
531+
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
532+
533+
// Close server after all tests
534+
afterAll(() => server.close())
535+
536+
// Reset handlers after each test for test isolation
537+
afterEach(() => server.resetHandlers())
538+
```
539+
:::
540+
483541
> 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.
484542
485543
### More

0 commit comments

Comments
 (0)