Skip to content

Commit ad73ea0

Browse files
NoiseFanWuMingDaothinkasany984507092
authored
docs(anchor): document add custom anchor (#742)
* docs(anchor): guide/index * typo(anchor): guide/index * docs(anchor): guide/why * docs(anchor): guide/features * docs(anchor): guide/cli * docs(anchor): guide/filtering * docs(anchor): guide/projects * docs(anchor): guide/projects typo * docs(anchor): guide/reporters * docs(anchor): guide/coverage * docs(anchor): guide/snapshot * docs(guide): Add some translate comments in index.md to Chinese * docs(/guide/features) Add some translate comments to Chinese Translate comments in the features guide from English to Chinese. * docs(/api/index): Add translate comments to Chinese Translate comments and explanations from English to Chinese. * docs(sideBar): Translate sidebar menu and page config * docs(config): modify /guide/why * docs(anchor): guide/mocking/dates * docs(anchor): guide/mocking * docs(anchor): guide/parallelism * docs(anchor): guide/in-source * docs(anchor): /guide/test-context * docs(anchor): /guide/environment * docs(anchor): /guide/extending-matchers * docs(anchor): /guide/ide * docs(anchor): /guide/ * docs(/config/index): Add tranalse to chinese * reporter translate to 报告器 * docs * docs(/advanced/api/index): Add some comments translate Translated comments in the code from English to Chinese. * Update advanced/api/index.md * Update advanced/api/index.md * docs(/advanced/runner): Add some comments translate * docs(/advanced/api/test-project): Add translate * docs(/advanced/api/plugin): Add comments translate * docs(/advanced/api/test-case): Add comments translate * docs(/advanced/api/test-suite): Add comments translate * docs(/advanced/api/test-specification): Add translate * Update advanced/api/test-specification.md * fix: Optimize the multi-level title problem of the test context * fix:Revise to be consistent with the English document * Update guide/test-context.md * fix User defined id attribute default-fixture is not uniqu * rm vitepress-plugin-llms * lint fix * Inspect the documentation using lint rules from the upstream repository. * update lint * fix: Optimize the multi-level title problem of the test context * fix:Revise to be consistent with the English document --------- Co-authored-by: WuMingDao <146366930+WuMingDao@users.noreply.github.com> Co-authored-by: thinkasany <480968828@qq.com> Co-authored-by: heming <984507092@qq.com>
1 parent 520e08f commit ad73ea0

25 files changed

+151
-146
lines changed

guide/cli.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ title: 命令行界面 | 指南
33
outline: deep
44
---
55

6-
# 命令行界面
6+
# 命令行界面 {#command-line-interface}
77

8-
## 命令
8+
## 命令 {#commands}
99

10-
### `vitest`
10+
### `vitest` {#vitest}
1111

1212
在当前目录启动 Vitest。在开发环境中会自动进入监听模式,而在 CI 环境(或非交互式终端)中会自动运行测试模式。
1313

@@ -122,7 +122,7 @@ tests/test1.test.ts
122122
tests/test2.test.ts
123123
```
124124

125-
## 选项
125+
## 选项 {#options}
126126

127127
::: tip
128128
Vitest 支持 CLI 参数的 both camel case 和 kebab case 。例如,`--passWithNoTests``--pass-with-no-tests` 都有效(`--no-color``--inspect-brk` 是例外)。

guide/common-errors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: 常见错误 | 指南
33
---
44

5-
# 常见错误
5+
# 常见错误 {#common-errors}
66

77
## Cannot find module './relative-path'
88

guide/coverage.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
title: 测试覆盖率 | 指南
33
---
44

5-
# 测试覆盖率
5+
# 测试覆盖率 {#coverage}
66

77
Vitest 通过 [`v8`](https://v8.dev/blog/javascript-code-coverage) 支持原生代码覆盖率,通过 [`istanbul`](https://istanbul.js.org/) 支持检测代码覆盖率。
88

9-
## 测试覆盖率提供者
9+
## 测试覆盖率提供者 {#coverage-providers}
1010

1111
`v8``istanbul` 的支持都是可选的。 默认情况下,启用 `v8`
1212

@@ -40,7 +40,7 @@ npm i -D @vitest/coverage-istanbul
4040

4141
:::
4242

43-
## V8 Provider
43+
## V8 Provider {#v8-provider}
4444

4545
::: info
4646
以下对 V8 覆盖率的说明仅适用于 Vitest,并不适用于其他测试工具。
@@ -77,7 +77,7 @@ Vitest 默认采用 'v8' 作为覆盖率提供器。
7777
<Box>Coverage report</Box>
7878
</div>
7979

80-
## Istanbul 覆盖率提供方案
80+
## Istanbul 覆盖率提供方案 {#istanbul-provider}
8181

8282
[Istanbul 代码覆盖率工具](https://istanbul.js.org/) 自 2012 年发布以来,已在各种场景中得到了充分验证。
8383
这种覆盖率提供器能在任何 JavaScript 运行环境中使用,因为它是通过在用户源码中插入额外的代码来跟踪执行情况。
@@ -133,7 +133,7 @@ globalThis.__VITEST_COVERAGE__[filename] = coverage // [!code ++]
133133
<Box>Coverage report</Box>
134134
</div>
135135

136-
## 覆盖率配置指南
136+
## 覆盖率配置指南 {#coverage-setup}
137137

138138
::: tip
139139
你可以在 [覆盖率配置参考](/config/#coverage) 中查看所有可用的覆盖率选项。
@@ -163,7 +163,7 @@ export default defineConfig({
163163
```
164164
:::
165165

166-
## 在覆盖率报告中设置需要统计或忽略的文件
166+
## 在覆盖率报告中设置需要统计或忽略的文件 {#including-and-excluding-files-from-coverage-report}
167167

168168
你可以通过设置 [`coverage.include`](/config/#coverage-include)[`coverage.exclude`](/config/#coverage-exclude) 来决定覆盖率报告中展示哪些文件。
169169

@@ -236,7 +236,7 @@ export default defineConfig({
236236
```
237237
:::
238238

239-
## 自定义覆盖率的报告器
239+
## 自定义覆盖率的报告器 {#custom-coverage-reporter}
240240

241241
我们可以通过在 `test.coverage.reporter` 中传递软件包名称或绝对路径来使用自定义覆盖报告器:
242242

@@ -283,7 +283,7 @@ module.exports = class CustomReporter extends ReportBase {
283283
}
284284
```
285285

286-
## 自定义覆盖率的提供者
286+
## 自定义覆盖率的提供者 {#custom-coverage-provider}
287287

288288
也可以通过将 `'custom'` 传递给 `test.coverage.provider` 来配置你的自定义覆盖率提供者:
289289

@@ -334,7 +334,7 @@ export default CustomCoverageProviderModule
334334

335335
请参阅类型定义查看有关详细信息。
336336

337-
## 代码忽略
337+
## 代码忽略 {#ignoring-code}
338338

339339
两个覆盖率提供商都有自己的方法来忽略覆盖率报告中的代码:
340340

@@ -357,7 +357,7 @@ if (condition) {
357357
if (condition) {
358358
```
359359

360-
### Examples
360+
### 示例
361361

362362
::: code-group
363363

@@ -462,11 +462,11 @@ export function ignored() { // [!code error]
462462
```
463463
:::
464464

465-
## Coverage Performance
465+
## Coverage Performance {#coverage-performance}
466466

467467
If code coverage generation is slow on your project, see [Profiling Test Performance | Code coverage](/guide/profiling-test-performance.html#code-coverage).
468468

469-
## Vitest UI
469+
## Vitest UI {#vitest-ui}
470470

471471
我们可以在 [Vitest UI](/guide/ui) 中查看你的覆盖率报告。
472472

guide/debugging.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
title: 调试 | 指南
33
---
44

5-
# 调试
5+
# 调试 {#debugging}
66

7-
## Terminal
7+
## 终端 {#terminal}
88

99
在非集成开发环境下,为了调试一个测试文件,你可以使用 [`ndb`](https://github.com/GoogleChromeLabs/ndb)。仅仅在你的代码的任何位置添加一个 `debugger` 语句,然后运行 `ndb`
1010

@@ -56,7 +56,7 @@ ndb npm run test
5656

5757
然后在调试选项卡中确保选择 'Debug Current Test File',然后你可以打开要调试的测试文件并按 F5 开始调试。
5858

59-
### 浏览器模式
59+
### 浏览器模式 {#browser-mode}
6060

6161
要调试 [Vitest 浏览器模式](/guide/browser/index.md),请在 CLI 中传递 `--inspect``--inspect-brk`,或在 Vitest 配置中定义它:
6262

@@ -65,8 +65,8 @@ ndb npm run test
6565
vitest --inspect-brk --browser --no-file-parallelism
6666
```
6767
```ts [vitest.config.js]
68-
import { defineConfig } from 'vitest/config'
6968
import { playwright } from '@vitest/browser-playwright'
69+
import { defineConfig } from 'vitest/config'
7070

7171
export default defineConfig({
7272
test: {

guide/extending-matchers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: 扩展断言 | 指南
33
---
44

5-
# 扩展断言(Matchers)
5+
# 扩展断言 {#extending-matchers}
66

77
由于 Vitest 兼容 Chai 和 Jest,所以可以根据个人喜好使用 `chai.use` API 或者 `expect.extend`
88

guide/features.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ title: 主要功能 | 指南
33
outline: deep
44
---
55

6-
# 主要功能
6+
# 主要功能 {#features}
77

88
<FeaturesList class="!gap-1 text-lg" />
99

10-
## 一套配置可以运用在多种环境
10+
## 一套配置可以运用在多种环境 {#shared-config-between-test-dev-and-build}
1111

1212
<div h-2 />
1313
<CourseLink href="https://vueschool.io/lessons/your-first-test?friend=vueuse">通过视频了解如何编写你的第一个测试</CourseLink>
@@ -16,7 +16,7 @@ outline: deep
1616

1717
了解更多信息 [配置 Vitest](/guide/#配置-vitest)
1818

19-
## 监听模式(watch mode)
19+
## 监听模式(watch mode) {#watch-mode}
2020

2121
```bash
2222
$ vitest
@@ -28,11 +28,11 @@ $ vitest
2828

2929
使用 `--standalone` 标志启动 Vitest,使其在后台运行。它不会运行任何测试,直到测试发生变化。如果源代码发生变化,Vitest 不会运行测试,直到运行了导入源代码的测试为止
3030

31-
## 开箱即用的常见 Web 支持
31+
## 开箱即用的常见 Web 支持 {#common-web-idioms-out-of-the-box}
3232

3333
开箱即用的 ES Module / TypeScript / JSX support / PostCSS
3434

35-
## 多线程
35+
## 多线程 {#threads}
3636

3737
Vitest 默认会通过 [Tinypool](https://github.com/tinylibs/tinypool)(它是 [Piscina](https://github.com/piscinajs/piscina) 的轻量版分支),利用 [`node:child_process`](https://nodejs.org/api/child_process.html)[多个进程](/guide/parallelism) 中并行执行测试文件,
3838
从而提升测试执行效率。如果你想让测试套件跑得更快,可以尝试开启 `--pool=threads` 选项,让 Vitest 使用 [`node:worker_threads`](https://nodejs.org/api/worker_threads.html) 执行测试——不过要注意,有些依赖可能在该模式下无法正常工作。
@@ -41,13 +41,13 @@ Vitest 默认会通过 [Tinypool](https://github.com/tinylibs/tinypool)(它是
4141

4242
Vitest 还隔离了每个测试文件的运行环境,因此一个文件中的运行环境改变不会影响其他文件。可以通过将 `--no-isolate` 传递给 CLI 来禁用隔离(以正确性换取运行性能)。
4343

44-
## 测试可过滤
44+
## 测试可过滤 {#test-filtering}
4545

4646
Vitest 提供了许多缩小测试范围的方法,以便在开发过程中加快速度并集中精力。
4747

4848
了解更多信息 [测试筛选](/guide/filtering)
4949

50-
## 同时运行多个测试
50+
## 同时运行多个测试 {#running-tests-concurrently}
5151

5252
在连续的测试中使用 `.concurrent` 来并行运行它们。
5353

@@ -95,12 +95,13 @@ describe.concurrent('suite', () => {
9595
在异步并发测试中使用快照时,由于 JavaScript 的限制,你需要使用 [测试环境](/guide/test-context) 中的 `expect` 来确保检测到正确的测试。
9696
:::
9797

98-
## 快照
98+
## 快照 {#snapshot}
9999

100100
兼容 [Jest 快照测试](https://jestjs.io/zh-Hans/docs/snapshot-testing) 功能。
101101

102102
```ts
103103
import { expect, it } from 'vitest'
104+
104105
it('renders correctly', () => {
105106
const result = render()
106107
expect(result).toMatchSnapshot()
@@ -109,18 +110,19 @@ it('renders correctly', () => {
109110

110111
了解更多信息 [快照](/guide/snapshot)
111112

112-
## Chai 和 Jest 的 `expect` 语法兼容
113+
## Chai 和 Jest 的 `expect` 语法兼容 {#chai-and-jest-expect-compatibility}
113114

114115
内置 [Chai](https://www.chaijs.com/) 进行断言和与 [Jest expect](https://jestjs.io/docs/expect) 兼容的 APIs
115116

116117
注意,如果你正在使用添加匹配器的第三方库,将 [`test.globals`](/config/#globals) 设置为 `true` 将提供更好的兼容性。
117118

118-
## 对象模拟(Mocking)
119+
## 对象模拟(Mocking) {#mocking}
119120

120121
内置 [Tinyspy](https://github.com/tinylibs/tinyspy) 用于在 `vi` 对象上使用 `jest` 兼容的 API 进行对象模拟。
121122

122123
```ts
123124
import { expect, vi } from 'vitest'
125+
124126
const fn = vi.fn()
125127
fn('hello', 1)
126128
expect(vi.isMockFunction(fn)).toBe(true)
@@ -147,6 +149,7 @@ $ npm i -D jsdom
147149

148150
```ts [vitest.config.ts]
149151
import { defineConfig } from 'vitest/config'
152+
150153
export default defineConfig({
151154
test: {
152155
environment: 'happy-dom', // 或 'jsdom' 和 'node'
@@ -156,7 +159,7 @@ export default defineConfig({
156159

157160
了解更多信息 [模拟对象](/guide/mocking)
158161

159-
## 测试覆盖率
162+
## 测试覆盖率 {#coverage}
160163

161164
Vitest 通过 [`v8`](https://v8.dev/blog/javascript-code-coverage) 支持原生代码覆盖率,通过 [`istanbul`](https://istanbul.js.org/) 支持检测代码覆盖率。
162165

@@ -174,6 +177,7 @@ Vitest 通过 [`v8`](https://v8.dev/blog/javascript-code-coverage) 支持原生
174177
```ts
175178
// vite.config.ts
176179
import { defineConfig } from 'vitest/config'
180+
177181
export default defineConfig({
178182
test: {
179183
coverage: {
@@ -185,7 +189,7 @@ export default defineConfig({
185189

186190
了解更多信息 [测试覆盖率](/guide/coverage)
187191

188-
## 源码内联测试
192+
## 源码内联测试 {#in-source-testing}
189193

190194
Vitest 还提供了一种方式,可以运行与你的代码实现放在一起的测试,类似 [Rust's 模块测试](https://doc.rust-lang.org/book/ch11-03-test-organization.html#the-tests-module-and-cfgtest).
191195

@@ -253,7 +257,7 @@ test('my types work properly', () => {
253257
})
254258
```
255259

256-
## 分片
260+
## 分片 {#sharding}
257261

258262
使用 [`--shard`](/guide/cli#shard)[`--reporter=blob`](/guide/reporters#blob-reporter)标志在不同的计算机上运行测试。可以使用 `--merge-reports` 命令在 CI 管道的末尾合并所有测试结果:
259263

@@ -265,7 +269,7 @@ vitest --merge-reports --reporter=junit --coverage
265269

266270
了解更多信息 [`性能优化 | 分片`](/guide/improving-performance#sharding)
267271

268-
## 环境变量
272+
## 环境变量 {#environment-variables}
269273

270274
Vitest 只从 `.env` 文件中自动加载以 `VITE_` 为前缀的环境变量,以保持与前端相关测试的兼容性,并遵守 [Vite 的既定惯例](https://vitejs.dev/guide/env-and-mode.html#env-files)。要从 `.env` 文件加载所有环境变量,可以使用从 `vite` 导入的 `loadEnv` 方法:
271275

@@ -281,7 +285,7 @@ export default defineConfig(({ mode }) => ({
281285
}))
282286
```
283287

284-
## 未处理的错误
288+
## 未处理的错误 {#unhandled-errors}
285289

286290
默认情况下,Vitest 会捕获并报告所有的 [未处理的拒绝(Promise)](https://developer.mozilla.org/en-US/docs/Web/API/Window/unhandledrejection_event)[未捕获的异常](https://nodejs.org/api/process.html#event-uncaughtexception)(在 Node.js 中)以及 [错误事件](https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event)(在 [浏览器](/guide/browser/) 中)。
287291

guide/filtering.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
title: 测试筛选 | 指南
33
---
44

5-
# 测试筛选
5+
# 测试筛选 {#test-filtering}
66

77
用于测试套件(suite)和测试的筛选(filtering)、超时(timeouts)、并发(concurrent)。
88

9-
## CLI
9+
## CLI {#cli}
1010

1111
你可以使用 CLI 按名称筛选测试文件:
1212

@@ -49,7 +49,7 @@ $ vitest basic/foo.test.ts:10-25 # ❌
4949
```
5050
:::
5151

52-
## 指定超时阈值
52+
## 指定超时阈值 {#specifying-a-timeout}
5353

5454
你可以选择将超时阈值(以毫秒为单位)作为第三个参数传递给测试。默认值为 [5 秒](/config/#testtimeout)
5555

@@ -71,7 +71,7 @@ beforeAll(async () => {
7171
}, 1000)
7272
```
7373

74-
## 跳过测试套件和测试
74+
## 跳过测试套件和测试 {#skipping-suites-and-tests}
7575

7676
使用 `.skip` 以避免运行某些测试套件或测试
7777

@@ -93,7 +93,7 @@ describe('suite', () => {
9393
})
9494
```
9595

96-
## 选择要运行的测试套件和测试
96+
## 选择要运行的测试套件和测试 {#selecting-suites-and-tests-to-run}
9797

9898
使用 `.only` 仅运行某些测试套件或测试
9999

@@ -120,7 +120,7 @@ describe('another suite', () => {
120120
})
121121
```
122122

123-
## 未实现的测试套件和测试
123+
## 未实现的测试套件和测试 {#unimplemented-suites-and-tests}
124124

125125
使用 `.todo` 留存将要实施的测试套件和测试的待办事项
126126

guide/ide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: IDE 插件 | 指南
33
---
44

5-
# IDE 插件
5+
# IDE 插件 {#ide-integrations}
66

77
## VS Code <Badge>Official</Badge> {#vs-code}
88

0 commit comments

Comments
 (0)