Skip to content

Commit b695049

Browse files
authored
Merge pull request #745 from 984507092/sync-heming
fix: Optimize the multi-level title problem of the test context
2 parents e62a246 + 1aabc40 commit b695049

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

guide/environment.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: 测试环境 | 指南
33
---
44

5-
# 测试环境
5+
# 测试环境 {#test-environment}
66

77
Vitest 提供 [`environment`](/config/#environment) 选项以在特定环境中运行代码。你可以使用 [`environmentOptions`](/config/#environmentoptions) 选项修改环境的行为方式。
88

@@ -25,7 +25,7 @@ Vitest 提供 [`environment`](/config/#environment) 选项以在特定环境中
2525
Vitest 并不将 `browser` 视作一种测试环境。如果你想让部分测试在 [浏览器模式](/guide/browser/) 中执行,可以通过创建一个 [测试项目](/guide/browser/#projects-config) 来实现。
2626
:::
2727

28-
## 特定文件的环境
28+
## 特定文件的环境 {#environments-for-specific-files}
2929

3030
如果配置中设置 `environment` 选项时,它将应用于项目中的所有测试文件。要获得更细粒度的控制,你可以使用控制注释为特定文件指定环境。控制注释是以 `@vitest-environment` 开头,后跟环境名称的注释:
3131

@@ -39,7 +39,7 @@ test('test', () => {
3939
})
4040
```
4141

42-
## 自定义环境
42+
## 自定义环境 {#custom-environment}
4343

4444
你可以创建自己的包来扩展 Vitest 环境。为此,请创建一个名为 `vitest-environment-${name}` 的包,或者指定一个有效的 JS/TS 文件路径。该包应该导出一个形状为 `Environment` 的对象。
4545

guide/in-source.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
title: 源码内联测试 | 指南
33
---
44

5-
# 源码内联测试
5+
# 源码内联测试 {#in-source}
66

77
Vitest 还提供了一种方式,可以运行与你的代码实现放在一起的测试,就像是 [Rust 语言的模块测试一样](https://doc.rust-lang.org/book/ch11-03-test-organization.html#the-tests-module-and-cfgtest)
88

99
这允许测试与实现共享相同的闭包,并且能够在不导出的情况下针对私有状态进行测试。同时,它也使开发更加接近反馈循环。
1010

11-
## 指引
11+
## 指引 {#setup}
12+
1213
::: warning
1314
本指南介绍如何在源代码中编写测试。如果需要在单独的测试文件中编写测试,请参阅["编写测试"指南](/guide/#writing-tests)
1415
:::
@@ -50,7 +51,7 @@ export default defineConfig({
5051
$ npx vitest
5152
```
5253

53-
## 生产环境构建
54+
## 生产环境构建 {#production-build}
5455

5556
对于生产环境的构建,你需要设置配置文件内的 `define` 选项,让打包器清除无用的代码。例如,在 Vite 中
5657

@@ -123,7 +124,7 @@ export default {
123124

124125
完整的示例请参考 [`examples/in-source-test`](https://github.com/vitest-dev/vitest/tree/main/examples/in-source-test)
125126

126-
## 说明
127+
## 说明 {#notes}
127128

128129
此功能可用于:
129130

guide/test-context.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ title: 测试上下文 | 指南
33
outline: deep
44
---
55

6-
# 测试上下文
6+
# 测试上下文 {#test-context}
77

88
[Playwright Fixtures](https://playwright.dev/docs/test-fixtures) 的启发,Vitest 的测试上下文允许你定义可在测试中使用的工具(utils)、状态(states)和固定装置(fixtures)。
99

10-
## 用法
10+
## 用法 {#usage}
1111

1212
第一个参数或每个测试回调是一个测试上下文。
1313

1414
```ts
1515
import { it } from 'vitest'
1616

1717
it('should work', ({ task }) => {
18-
// prints name of the test
18+
// prints name of the testusage
1919
console.log(task.name)
2020
})
2121
```
2222

23-
## 内置测试上下文
23+
## 内置测试上下文 {#built-in-test-context}
2424

2525
#### `task`
2626

@@ -125,7 +125,7 @@ it('stop request when test times out', async ({ signal }) => {
125125

126126
[`onTestFinished`](/api/#ontestfailed) 与当前测试用例绑定。当你并发执行多个测试并希望只对某个特定测试进行特殊处理时,这个 API 会非常有帮助。
127127

128-
## 扩展测试上下文
128+
## 扩展测试上下文 {#extend-test-context}
129129

130130
Vitest 提供了两种不同的方式来帮助你扩展测试上下文。
131131

@@ -191,7 +191,7 @@ export const test = todosTest.extend({
191191
})
192192
```
193193

194-
#### 固定装置初始化
194+
#### 固定装置初始化 {#default-fixture}
195195

196196
Vitest 运行器将智能地初始化你的固定装置并根据使用情况将它们注入到测试上下文中。
197197

@@ -231,7 +231,7 @@ test('context must be destructured', ({ todos }) => { // [!code ++]
231231

232232
:::
233233

234-
#### 自动化装置
234+
#### 自动化装置 {#automated-fixture}
235235

236236
Vitest 还支持 fixture 的元组语法,允许你传递每个 fixture 的选项。例如,你可以使用它来显式初始化固定装置,即使它没有在测试中使用。
237237

@@ -253,7 +253,7 @@ const test = base.extend({
253253
test('works correctly')
254254
```
255255

256-
#### Default fixture
256+
#### 默认的装置 {#default-fixture}
257257

258258
Since Vitest 3, you can provide different values in different [projects](/guide/projects). To enable this feature, pass down `{ injected: true }` to the options. If the key is not specified in the [project configuration](/config/#provide), then the default value will be used.
259259

guide/testing-types.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: 类型测试 | 指南
33
---
44

5-
# 类型测试
5+
# 类型测试 {#testing-types}
66

77
::: tip Sample Project
88

@@ -41,7 +41,7 @@ test('my types work properly', () => {
4141

4242
你可以在 [API 部分](/api/#expecttypeof) 中查看可能的匹配器列表。
4343

44-
## 读取错误
44+
## 读取错误 {#reading-errors}
4545

4646
如果使用的是 `expectTypeOf` API,请参阅 [expect-type 关于其错误信息的文档](https://github.com/mmkal/expect-type#error-messages)
4747

@@ -122,7 +122,7 @@ assertType<string>(answr)
122122

123123
:::
124124

125-
## 运行类型检查
125+
## 运行类型检查 {#run-typechecking}
126126

127127
要启用类型检查,只需在 `package.json` 文件中的 Vitest 命令中添加 [`--typecheck`](/config/#typecheck) 标志:
128128

0 commit comments

Comments
 (0)