Skip to content

Commit 028717e

Browse files
committed
docs: add a recommendation block for msw
1 parent 51a31d3 commit 028717e

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ test('loads and displays greeting', async () => {
3939
})
4040
```
4141

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+
4246
---
4347

4448
## Step-By-Step
@@ -50,7 +54,6 @@ test('loads and displays greeting', async () => {
5054
import React from 'react'
5155

5256
// import API mocking utilities from Mock Service Worker
53-
// (see https://github.com/mswjs/msw)
5457
import { rest } from 'msw'
5558
import { setupServer } from 'msw/node'
5659

@@ -61,7 +64,22 @@ import { render, fireEvent, waitFor, screen } from '@testing-library/react'
6164
import '@testing-library/jest-dom/extend-expect'
6265
// the component to test
6366
import Fetch from '../fetch'
67+
```
68+
69+
```jsx
70+
test('loads and displays greeting', async () => {
71+
// Arrange
72+
// Act
73+
// Assert
74+
})
75+
```
6476

77+
### Mock
78+
79+
Use the `setupServer` function from `msw` to mock an API request that our tested
80+
component makes.
81+
82+
```js
6583
// declare which API requests to mock
6684
const server = setupServer(
6785
// capture "GET /greeting" requests
@@ -77,14 +95,6 @@ beforeAll(() => server.listen())
7795
afterAll(() => server.close())
7896
```
7997

80-
```jsx
81-
test('loads and displays greeting', async () => {
82-
// Arrange
83-
// Act
84-
// Assert
85-
})
86-
```
87-
8898
### Arrange
8999

90100
The [`render`](./api#render) method renders a React element into the DOM and

0 commit comments

Comments
 (0)