@@ -65,17 +65,26 @@ test('loads and displays greeting', async () => {
65
65
66
66
See the following sections for a detailed breakdown of the test
67
67
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
+
68
77
``` jsx title="__tests__/fetch.test.jsx"
69
78
import React from ' react'
70
- import {rest } from ' msw'
79
+ import {http , HttpResponse } from ' msw'
71
80
import {setupServer } from ' msw/node'
72
81
import {render , fireEvent , screen } from ' @testing-library/react'
73
82
import ' @testing-library/jest-dom'
74
83
import Fetch from ' ../fetch'
75
84
76
85
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' })
79
88
}),
80
89
)
81
90
@@ -96,8 +105,8 @@ test('loads and displays greeting', async () => {
96
105
97
106
test (' handles server error' , async () => {
98
107
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 } )
101
110
}),
102
111
)
103
112
@@ -112,10 +121,6 @@ test('handles server error', async () => {
112
121
})
113
122
```
114
123
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
-
119
124
---
120
125
121
126
## Step-By-Step
@@ -127,7 +132,7 @@ test('handles server error', async () => {
127
132
import React from ' react'
128
133
129
134
// import API mocking utilities from Mock Service Worker
130
- import {rest } from ' msw'
135
+ import {http , HttpResponse } from ' msw'
131
136
import {setupServer } from ' msw/node'
132
137
133
138
// import react-testing methods
@@ -156,9 +161,9 @@ component makes.
156
161
// declare which API requests to mock
157
162
const server = setupServer (
158
163
// capture "GET /greeting" requests
159
- rest .get (' /greeting' , (req , res , ctx ) => {
164
+ http .get (' /greeting' , (req , res , ctx ) => {
160
165
// respond using a mocked JSON body
161
- return res ( ctx .json ({greeting: ' hello there' }) )
166
+ return HttpResponse .json ({greeting: ' hello there' })
162
167
}),
163
168
)
164
169
@@ -176,8 +181,8 @@ test('handles server error', async () => {
176
181
server .use (
177
182
// override the initial "GET /greeting" request handler
178
183
// 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 } )
181
186
}),
182
187
)
183
188
0 commit comments