@@ -39,6 +39,10 @@ test('loads and displays greeting', async () => {
39
39
})
40
40
```
41
41
42
+ > We recommend using [ Mock Service Worker] ( https://github.com/mswjs/msw ) library
43
+ > to declaratively mock API communication in your tests instead of stubbing
44
+ > ` window.fetch ` , or relying on third-party adapters.
45
+
42
46
---
43
47
44
48
## Step-By-Step
@@ -50,7 +54,6 @@ test('loads and displays greeting', async () => {
50
54
import React from ' react'
51
55
52
56
// import API mocking utilities from Mock Service Worker
53
- // (see https://github.com/mswjs/msw)
54
57
import { rest } from ' msw'
55
58
import { setupServer } from ' msw/node'
56
59
@@ -61,7 +64,22 @@ import { render, fireEvent, waitFor, screen } from '@testing-library/react'
61
64
import ' @testing-library/jest-dom/extend-expect'
62
65
// the component to test
63
66
import Fetch from ' ../fetch'
67
+ ```
68
+
69
+ ``` jsx
70
+ test (' loads and displays greeting' , async () => {
71
+ // Arrange
72
+ // Act
73
+ // Assert
74
+ })
75
+ ```
64
76
77
+ ### Mock
78
+
79
+ Use the ` setupServer ` function from ` msw ` to mock an API request that our tested
80
+ component makes.
81
+
82
+ ``` js
65
83
// declare which API requests to mock
66
84
const server = setupServer (
67
85
// capture "GET /greeting" requests
@@ -77,14 +95,6 @@ beforeAll(() => server.listen())
77
95
afterAll (() => server .close ())
78
96
```
79
97
80
- ``` jsx
81
- test (' loads and displays greeting' , async () => {
82
- // Arrange
83
- // Act
84
- // Assert
85
- })
86
- ```
87
-
88
98
### Arrange
89
99
90
100
The [ ` render ` ] ( ./api#render ) method renders a React element into the DOM and
0 commit comments