Skip to content

Commit c163fc2

Browse files
authored
docs(cn): update guide (#976)
1 parent 4213bb0 commit c163fc2

14 files changed

Lines changed: 560 additions & 559 deletions

.vitepress/config.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -723,53 +723,53 @@ export default ({ mode }: { mode: string }) => {
723723
],
724724
},
725725
{
726-
text: 'Learn',
726+
text: '学习',
727727
collapsed: false,
728728
items: [
729729
{
730-
text: 'Writing Tests',
730+
text: '编写测试用例',
731731
link: '/guide/learn/writing-tests',
732-
docFooterText: 'Writing Tests | Learn',
732+
docFooterText: '编写测试用例 | 学习',
733733
},
734734
{
735-
text: 'Using Matchers',
735+
text: '使用匹配器',
736736
link: '/guide/learn/matchers',
737-
docFooterText: 'Using Matchers | Learn',
737+
docFooterText: '使用匹配器 | 学习',
738738
},
739739
{
740-
text: 'Testing Async Code',
740+
text: '测试异步代码',
741741
link: '/guide/learn/async',
742-
docFooterText: 'Testing Async Code | Learn',
742+
docFooterText: '测试异步代码 | 学习',
743743
},
744744
{
745-
text: 'Setup and Teardown',
745+
text: '初始化与清理',
746746
link: '/guide/learn/setup-teardown',
747-
docFooterText: 'Setup and Teardown | Learn',
747+
docFooterText: 'Setup and Teardown | 学习',
748748
},
749749
{
750-
text: 'Mock Functions',
750+
text: '模拟函数',
751751
link: '/guide/learn/mock-functions',
752-
docFooterText: 'Mock Functions | Learn',
752+
docFooterText: '模拟函数 | 学习',
753753
},
754754
{
755-
text: 'Snapshot Testing',
755+
text: '快照测试',
756756
link: '/guide/learn/snapshots',
757-
docFooterText: 'Snapshot Testing | Learn',
757+
docFooterText: '快照测试 | 学习',
758758
},
759759
{
760-
text: 'Testing in Practice',
760+
text: '测试实践',
761761
link: '/guide/learn/testing-in-practice',
762-
docFooterText: 'Testing in Practice | Learn',
762+
docFooterText: '测试实践 | 学习',
763763
},
764764
{
765-
text: 'Debugging Tests',
765+
text: '调试测试',
766766
link: '/guide/learn/debugging-tests',
767-
docFooterText: 'Debugging Tests | Learn',
767+
docFooterText: '调试测试 | 学习',
768768
},
769769
{
770-
text: 'Writing Tests with AI',
770+
text: '使用 AI 编写测试',
771771
link: '/guide/learn/writing-tests-with-ai',
772-
docFooterText: 'Writing Tests with AI | Learn',
772+
docFooterText: '使用 AI 编写测试 | 学习',
773773
},
774774
],
775775
},

guide/learn/async.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
---
2-
title: Testing Asynchronous Code | Guide
2+
title: 测试异步代码 | 指南
33
prev:
4-
text: Using Matchers
4+
text: 使用匹配器
55
link: /guide/learn/matchers
66
next:
7-
text: Setup and Teardown
7+
text: 初始化与清理
88
link: /guide/learn/setup-teardown
99
---
1010

11-
# Testing Asynchronous Code
11+
# 测试异步代码 {#testing-asynchronous-code}
1212

13-
JavaScript code frequently runs asynchronously. Whether you're fetching data, reading files, or waiting on timers, Vitest needs to know when the code it is testing has completed before moving on to the next test. Here are the patterns you'll use most often.
13+
JavaScript 代码经常以异步方式运行。无论是获取数据、读取文件还是等待定时器,Vitest 都需要知道它正在测试的代码何时完成,然后才能继续执行下一个测试。下面是最常见的几种写法。
1414

1515
## Async/Await
1616

17-
The most straightforward approach is to make your test function `async`. Vitest will automatically wait for the returned promise to resolve before considering the test complete. If the promise rejects, the test fails with the rejection reason.
17+
最直接的方法是让你的测试函数变为 `async`Vitest 会自动等待返回的 Promise resolve,然后才认为测试完成。如果 Promise 被 reject,测试将失败,并显示拒绝原因。
1818

1919
```js
2020
import { expect, test } from 'vitest'
@@ -29,11 +29,11 @@ test('fetches user by id', async () => {
2929
})
3030
```
3131

32-
This is the pattern you'll use the vast majority of the time. It reads just like synchronous code, and errors propagate naturally through `await`.
32+
这是你绝大多数时候会使用的形式。它读起来就像同步代码一样,错误也会通过 `await` 自然地传播。
3333

34-
## Resolves and Rejects
34+
## Resolves Rejects {#resolves-and-rejects}
3535

36-
Sometimes you'd rather assert on a promise directly instead of `await`-ing it into a variable first. The [`.resolves`](/api/expect#resolves) and [`.rejects`](/api/expect#rejects) helpers let you do this. They unwrap the promise and then apply the matcher to the resolved or rejected value:
36+
有时你可能更愿意直接对 Promise 进行断言,而不是先将其通过 `await` 赋值给变量。[`.resolves`](/api/expect#resolves) [`.rejects`](/api/expect#rejects) 工具函数让你能够做到这一点。它们会解开 Promise,然后将匹配器应用到 resolved rejected 值上:
3737

3838
```js
3939
test('resolves to Alice', async () => {
@@ -46,12 +46,12 @@ test('rejects with an error', async () => {
4646
```
4747

4848
::: warning
49-
Don't forget the `await` before `expect`. Vitest will detect unawaited assertions and print a warning at the end of the test, but it's best to always include `await` explicitly. Vitest will also wait for all pending promises in `Promise.all` before starting the next test, but relying on this behavior makes tests harder to understand.
49+
不要忘记在 `expect` 前面加上 `await`Vitest 会检测未等待的断言,并在测试结束时打印警告,但最好始终显式地添加 `await`Vitest 还会在启动下一个测试之前,等待 `Promise.all` 中所有仍在进行的 Promise 完成,不过依赖这种行为会让测试更难理解。
5050
:::
5151

52-
## Assertion Counting
52+
## 断言计数 {#assertion-counting}
5353

54-
With async code, there's a subtle risk: an assertion inside a callback or `.then()` chain might never execute, and the test would still pass because no assertion failed. [`expect.hasAssertions()`](/api/expect#hasassertions) guards against this by verifying that at least one assertion ran during the test:
54+
对于异步代码,存在一个不易察觉的风险:回调函数或 `.then()` 链中的断言可能根本没有执行,而测试仍然会通过,因为没有断言失败。[`expect.hasAssertions()`](/api/expect#hasassertions) 正是用来防范这种情况的,它会检查该测试在执行期间是否至少运行过一条断言:
5555

5656
```js
5757
test('callback is invoked', async () => {
@@ -61,11 +61,11 @@ test('callback is invoked', async () => {
6161
data.items.forEach((item) => {
6262
expect(item.id).toBeDefined()
6363
})
64-
// if data.items is empty, the test fails instead of silently passing
64+
// 如果 data.items 为空,测试会失败而不是静默通过
6565
})
6666
```
6767

68-
When you know exactly how many assertions should run, [`expect.assertions(n)`](/api/expect#assertions) is more precise:
68+
当你知道应该运行的确切断言数量时,[`expect.assertions(n)`](/api/expect#assertions) 会更加精确:
6969

7070
```js
7171
test('both callbacks are called', async () => {
@@ -78,15 +78,15 @@ test('both callbacks are called', async () => {
7878
})
7979
```
8080

81-
In most cases, `async`/`await` with direct assertions is clear enough and you don't need assertion counting. It's most useful when assertions are inside callbacks, loops, or conditional branches where you want to guarantee they actually executed.
81+
在大多数情况下,使用直接断言的 `async`/`await` 已经足够清晰,无需额外进行断言计数。断言计数最适用于当断言位于回调函数、循环或条件分支中,而你希望确保它们确实已经执行。
8282

8383
::: tip
84-
If you want every test in your project to require at least one assertion, enable [`expect.requireAssertions`](/config/expect#expect-requireassertions) in your config instead of adding `expect.hasAssertions()` to each test manually.
84+
如果你希望项目中的每个测试都至少需要一个断言,可以在配置中启用 [`expect.requireAssertions`](/config/expect#expect-requireassertions),而不是手动为每个测试添加 `expect.hasAssertions()`
8585
:::
8686

87-
## Callbacks
87+
## 回调函数 {#callbacks}
8888

89-
Some older APIs use callbacks instead of promises. Since Vitest works with promises, the simplest approach is to wrap the callback in a `Promise`:
89+
一些较旧的 API 使用回调函数而非 Promise。由于 Vitest 与 Promise 协同工作,最简单的方法是将回调函数包装在 `Promise` 中:
9090

9191
```js
9292
function fetchData(callback) {
@@ -101,25 +101,25 @@ test('the data is peanut butter', async () => {
101101
})
102102
```
103103

104-
This pattern works for any callback-based API. Pass `resolve` as the success callback, and the test will wait until the callback is invoked.
104+
这种形式适用于任何基于回调的 API。将 `resolve` 作为成功回调传递进去,测试就会一直等待,直到该回调被调用。
105105

106106
::: tip
107-
Most modern Node.js APIs (such as `fs/promises` and `fetch`) support promises natively, so you can use `async`/`await` directly. The callback wrapping pattern above is mainly useful for older libraries that haven't adopted promises yet.
107+
大多数现代 Node.js API(例如 `fs/promises` `fetch`)原生支持 Promise,因此你可以直接使用 `async`/`await`。上述的回调包装形式主要适用于尚未采用 Promise 的旧版库。
108108
:::
109109

110-
## Timeouts
110+
## 超时 {#timeouts}
111111

112-
By default, each test has a 5-second timeout. If a test takes longer than that (perhaps because a promise never resolves, or a network request hangs), it will fail with a timeout error. This prevents your test suite from getting stuck indefinitely.
112+
默认情况下,每个测试有 5 秒的超时时间。如果某个测试耗时超过此限制(可能是因为 Promise 从未 resolve,或网络请求挂起),它将因超时错误而失败。这可以防止你的测试套件无限期地卡住。
113113

114-
You can set a [custom timeout](/api/test#timeout) as the third argument to `test`, which is useful for tests that legitimately need more time:
114+
你可以将 [自定义超时时间](/api/test#timeout) 作为 `test` 的第三个参数进行设置,这适用于确实需要更多时间的测试:
115115

116116
```js
117117
test('long-running operation', async () => {
118118
await someSlowOperation()
119-
}, 10_000) // 10 seconds
119+
}, 10_000) // 10
120120
```
121121

122-
If you find yourself needing longer timeouts across many tests, you can change the default for all tests with the [`testTimeout`](/config/testtimeout) config option:
122+
如果你发现自己需要在许多测试中使用更长的超时时间,可以通过 [`testTimeout`](/config/testtimeout) 配置选项更改所有测试的默认值:
123123

124124
```js [vitest.config.js]
125125
import { defineConfig } from 'vitest/config'
@@ -131,27 +131,27 @@ export default defineConfig({
131131
})
132132
```
133133

134-
## Unhandled Rejections
134+
## 未处理的 Rejection {#unhandled-rejections}
135135

136-
By default, Vitest reports unhandled promise rejections as errors in the test run. If a promise rejects somewhere in your code and nothing catches it, the test run will fail, even if all your assertions passed. This is intentional: unhandled rejections usually indicate real bugs, like a forgotten `await` or a fire-and-forget promise that silently fails.
136+
默认情况下,Vitest 会将未处理的 Promise reject 报告为测试运行中的错误。如果你的代码中某个 Promise 被 reject 且未被捕获,即使所有断言都通过,测试运行也会失败。这是有意为之的:未处理的 reject 通常表示存在真正的 bug,例如忘记的 `await` 或者一个 “发出后不再等待” 的 Promise 在静默中失败了。
137137

138138
```js
139139
test('this causes an unhandled rejection error', () => {
140-
// This promise rejects but is never awaited or caught
140+
// 这个 Promise 会 reject 但从未被 await 或 catch
141141
Promise.reject(new Error('oops'))
142142
})
143143
```
144144

145-
To fix this, make sure you `await` all promises or catch expected rejections:
145+
要修复此问题,请确保对所有 Promise 使用 `await`,或捕获那些预期会发生的 reject:
146146

147147
```js
148148
test('handle the rejection', async () => {
149-
// Either await the promise
149+
// 要么等待 Promise
150150
await expect(Promise.reject(new Error('oops'))).rejects.toThrow('oops')
151151

152-
// Or catch it explicitly if you don't need to assert on it
152+
// 如果不需要对其断言,可以显式 catch
153153
Promise.reject(new Error('expected')).catch(() => {})
154154
})
155155
```
156156

157-
If your code intentionally produces unhandled rejections, you can filter specific errors with [`onUnhandledError`](/config/onunhandlederror) or disable the check entirely with [`dangerouslyIgnoreUnhandledErrors`](/config/dangerouslyignoreunhandlederrors).
157+
如果你的代码有意产生未处理的 reject,可以使用 [`onUnhandledError`](/config/onunhandlederror) 过滤特定的错误,或者通过 [`dangerouslyIgnoreUnhandledErrors`](/config/dangerouslyignoreunhandlederrors) 完全禁用此检查。

0 commit comments

Comments
 (0)