Skip to content

Commit 8f1e779

Browse files
committed
docs: use express as node.js example
1 parent 2d76d2e commit 8f1e779

File tree

1 file changed

+25
-42
lines changed

1 file changed

+25
-42
lines changed

README.md

+25-42
Original file line numberDiff line numberDiff line change
@@ -117,57 +117,40 @@ There's no such thing as Service Workers in Node.js. Instead, MSW implements a [
117117

118118
### Usage example
119119

120-
Take a look at the example of an integration test in Vitest that uses [React Testing Library](https://github.com/testing-library/react-testing-library) and Mock Service Worker:
120+
Here's an example of using Mock Service Worker while developing your Express server:
121121

122-
```ts
123-
// test/Dashboard.test.tsx
124-
import * as React from 'react'
122+
```js
123+
import express from 'express'
125124
import { http, HttpResponse } from 'msw'
126125
import { setupServer } from 'msw/node'
127-
import { render, screen, waitFor } from '@testing-library/react'
128-
import { Dashboard } from '../src/components/dashboard'
129126

130-
type Post = {
131-
id: string
132-
title: string
133-
}
127+
const app = express()
128+
const server = setupServer()
129+
130+
app.get(
131+
'/checkout/session',
132+
server.boundary((req, res) => {
133+
// Describe the network for this Express route.
134+
server.use(
135+
http.get(
136+
'https://api.stripe.com/v1/checkout/sessions/:id',
137+
({ params }) => {
138+
return HttpResponse.json({
139+
id: params.id,
140+
mode: 'payment',
141+
status: 'open',
142+
})
143+
},
144+
),
145+
)
134146

135-
const server = setupServer(
136-
http.get<never, never, Post[]>('/posts', ({ request, params, cookies }) => {
137-
return HttpResponse.json([
138-
{
139-
id: 'f8dd058f-9006-4174-8d49-e3086bc39c21',
140-
title: `Avoid Nesting When You're Testing`,
141-
},
142-
{
143-
id: '8ac96078-6434-4959-80ed-cc834e7fef61',
144-
title: `How I Built A Modern Website In 2021`,
145-
},
146-
])
147+
// Continue with processing the checkout session.
148+
handleSession(req, res)
147149
}),
148150
)
149-
150-
beforeAll(() => server.listen())
151-
afterEach(() => server.resetHandlers())
152-
afterAll(() => server.close())
153-
154-
it('displays the list of recent posts', async () => {
155-
render(<Dashboard />)
156-
157-
// ✅ Assert that the posts are visible.
158-
await waitFor(() => {
159-
expect(
160-
screen.getByRole('link', { name: /Avoid Nesting When You're Testing/ }),
161-
).toBeVisible()
162-
163-
expect(
164-
screen.getByRole('link', { name: /How I Built A Modern Website In 2021/ }),
165-
).toBeVisible()
166-
})
167-
})
168151
```
169152

170-
Don't get overwhelmed! There's a step-by-step [**Getting started**](https://mswjs.io/docs/getting-started) tutorial that you can follow to learn how to integrate Mock Service Worker into your project.
153+
> This example showcases [`server.boundary()`](https://mswjs.io/docs/api/setup-server/boundary) to scope request interception to a particular closure, which is extremely handy!
171154
172155
## Sponsors
173156

0 commit comments

Comments
 (0)