Skip to content

Commit fa49ad1

Browse files
authored
Merge pull request #778 from lxKylin/mocking-request
docs: update mocking-request
2 parents 8a796c4 + 604e468 commit fa49ad1

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

guide/mocking/requests.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Mock Service Worker (MSW) 通过拦截你的测试发出的请求来工作,允
66

77
## 配置 {#configuration}
88

9-
You can use it like below in your [setup file](/config/#setupfiles)
9+
你可以像下面这样在你的 [setup 文件](/config/#setupfiles) 中使用它:
1010

1111
::: code-group
1212

@@ -33,13 +33,13 @@ export const restHandlers = [
3333

3434
const server = setupServer(...restHandlers)
3535

36-
// Start server before all tests
36+
// 在所有测试开始前启动服务器
3737
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
3838

39-
// Close server after all tests
39+
// 在所有测试结束后关闭服务器
4040
afterAll(() => server.close())
4141

42-
// Reset handlers after each test for test isolation
42+
// 在每次测试后重置处理器以实现测试隔离
4343
afterEach(() => server.resetHandlers())
4444
```
4545

@@ -68,13 +68,13 @@ const graphqlHandlers = [
6868

6969
const server = setupServer(...graphqlHandlers)
7070

71-
// Start server before all tests
71+
// 在所有测试开始前启动服务器
7272
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
7373

74-
// Close server after all tests
74+
// 在所有测试结束后关闭服务器
7575
afterAll(() => server.close())
7676

77-
// Reset handlers after each test for test isolation
77+
// 在每次测试后重置处理器以实现测试隔离
7878
afterEach(() => server.resetHandlers())
7979
```
8080

@@ -89,26 +89,26 @@ const wsHandlers = [
8989
chat.addEventListener('connection', ({ client }) => {
9090
client.addEventListener('message', (event) => {
9191
console.log('Received message from client:', event.data)
92-
// Echo the received message back to the client
92+
// 将接收到的消息回显给客户端
9393
client.send(`Server received: ${event.data}`)
9494
})
9595
}),
9696
]
9797

9898
const server = setupServer(...wsHandlers)
9999

100-
// Start server before all tests
100+
// 在所有测试开始前启动服务器
101101
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
102102

103-
// Close server after all tests
103+
// 在所有测试结束后关闭服务器
104104
afterAll(() => server.close())
105105

106-
// Reset handlers after each test for test isolation
106+
// 在每次测试后重置处理器以实现测试隔离
107107
afterEach(() => server.resetHandlers())
108108
```
109109
:::
110110

111-
> Configuring the server with `onUnhandledRequest: 'error'` ensures that an error is thrown whenever there is a request that does not have a corresponding request handler.
111+
> 配置服务器时设置 `onUnhandledRequest: 'error'` 可以确保当存在没有对应请求处理器的请求时抛出错误。
112112
113113
## 更多信息 {#more}
114-
There is much more to MSW. You can access cookies and query parameters, define mock error responses, and much more! To see all you can do with MSW, read [their documentation](https://mswjs.io/docs).
114+
MSW 的功能远不止这些。你可以访问 cookie 和查询参数,定义模拟错误响应,以及更多其他功能!要了解 MSW 的所有功能,请阅读[它们的文档](https://mswjs.io/docs)

0 commit comments

Comments
 (0)