Skip to content

Commit 4e321ba

Browse files
MatanBobikettanaitotimdeschryver
authored
docs: udpate msw example (#1378)
Co-authored-by: Artem Zakharchenko <[email protected]> Co-authored-by: Tim Deschryver <[email protected]>
1 parent 6cf968b commit 4e321ba

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

docs/react-testing-library/example-intro.mdx

+19-14
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,26 @@ test('loads and displays greeting', async () => {
6565

6666
See the following sections for a detailed breakdown of the test
6767

68+
:::note
69+
We recommend using the
70+
[Mock Service Worker (MSW)](https://github.com/mswjs/msw) library to
71+
declaratively mock API communication in your tests instead of stubbing
72+
`window.fetch`, or relying on third-party adapters.
73+
74+
MSW requires Node.js 18 or later.
75+
:::
76+
6877
```jsx title="__tests__/fetch.test.jsx"
6978
import React from 'react'
70-
import {rest} from 'msw'
79+
import {http, HttpResponse} from 'msw'
7180
import {setupServer} from 'msw/node'
7281
import {render, fireEvent, screen} from '@testing-library/react'
7382
import '@testing-library/jest-dom'
7483
import Fetch from '../fetch'
7584

7685
const server = setupServer(
77-
rest.get('/greeting', (req, res, ctx) => {
78-
return res(ctx.json({greeting: 'hello there'}))
86+
http.get('/greeting', () => {
87+
return HttpResponse.json({greeting: 'hello there'})
7988
}),
8089
)
8190

@@ -96,8 +105,8 @@ test('loads and displays greeting', async () => {
96105

97106
test('handles server error', async () => {
98107
server.use(
99-
rest.get('/greeting', (req, res, ctx) => {
100-
return res(ctx.status(500))
108+
http.get('/greeting', () => {
109+
return new HttpResponse(null, {status: 500})
101110
}),
102111
)
103112

@@ -112,10 +121,6 @@ test('handles server error', async () => {
112121
})
113122
```
114123

115-
> We recommend using [Mock Service Worker](https://github.com/mswjs/msw) library
116-
> to declaratively mock API communication in your tests instead of stubbing
117-
> `window.fetch`, or relying on third-party adapters.
118-
119124
---
120125

121126
## Step-By-Step
@@ -127,7 +132,7 @@ test('handles server error', async () => {
127132
import React from 'react'
128133

129134
// import API mocking utilities from Mock Service Worker
130-
import {rest} from 'msw'
135+
import {http, HttpResponse} from 'msw'
131136
import {setupServer} from 'msw/node'
132137

133138
// import react-testing methods
@@ -156,9 +161,9 @@ component makes.
156161
// declare which API requests to mock
157162
const server = setupServer(
158163
// capture "GET /greeting" requests
159-
rest.get('/greeting', (req, res, ctx) => {
164+
http.get('/greeting', (req, res, ctx) => {
160165
// respond using a mocked JSON body
161-
return res(ctx.json({greeting: 'hello there'}))
166+
return HttpResponse.json({greeting: 'hello there'})
162167
}),
163168
)
164169

@@ -176,8 +181,8 @@ test('handles server error', async () => {
176181
server.use(
177182
// override the initial "GET /greeting" request handler
178183
// to return a 500 Server Error
179-
rest.get('/greeting', (req, res, ctx) => {
180-
return res(ctx.status(500))
184+
http.get('/greeting', (req, res, ctx) => {
185+
return new HttpResponse(null, {status: 500})
181186
}),
182187
)
183188

0 commit comments

Comments
 (0)