Skip to content

Commit 1fdc544

Browse files
committed
docs(cn): update guide/filtering.md
1 parent a5e03a6 commit 1fdc544

2 files changed

Lines changed: 56 additions & 53 deletions

File tree

guide/features.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,17 @@ import FeaturesList from '../.vitepress/components/FeaturesList.vue'
1313

1414
<CourseLink href="https://vueschool.io/lessons/your-first-test?friend=vueuse">通过视频了解如何编写你的第一个测试</CourseLink>
1515

16-
## 一套配置可以运用在多种环境 {#shared-config-between-test-dev-and-build}
17-
18-
<div h-2 />
19-
2016
::: tip
2117
本页为 Vitest 核心功能的概览指南。如果你初次接触 Vitest,建议先学习 [入门](/guide/learn/writing-tests) 教程,通过实践演练快速掌握测试编写基础。
2218
:::
2319

24-
## Shared Config between Test, Dev and Build
20+
## 测试、开发与构建之间共享配置 {#shared-config-between-test-dev-and-build}
2521

2622
与 Vite 的配置、转换器、解析器和插件通用,将会使用应用中的相同配置来运行测试。
2723

28-
更多详情请参阅 [配置 Vitest](/config/)
24+
更多详情请参阅 [配置 Vitest](/config/)
2925

30-
## 监听模式(watch mode) {#watch-mode}
26+
## 监听模式 (watch mode) {#watch-mode}
3127

3228
```bash
3329
$ vitest
@@ -49,11 +45,11 @@ $ vitest
4945

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

52-
## 测试可过滤 {#test-filtering}
48+
## 测试筛选 {#test-filtering}
5349

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

56-
了解更多信息 [测试筛选](/guide/filtering)
52+
了解更多信息 [测试筛选](/guide/filtering)
5753

5854
## 同时运行多个测试 {#running-tests-concurrently}
5955

@@ -145,12 +141,15 @@ expect(fn.mock.results[1].value).toBe('world')
145141
Vitest 支持 [happy-dom](https://github.com/capricorn86/happy-dom)[jsdom](https://github.com/jsdom/jsdom) 来模拟 DOM 和浏览器 API。Vitest 并不内置它们,所以你可能需要安装:
146142

147143
::: code-group
144+
148145
```bash [happy-dom]
149146
$ npm i -D happy-dom
150147
```
148+
151149
```bash [jsdom]
152150
$ npm i -D jsdom
153151
```
152+
154153
:::
155154

156155
然后,更改 `environment` 配置文件中的选项:
@@ -165,7 +164,7 @@ export default defineConfig({
165164
})
166165
```
167166

168-
了解更多信息 [模拟对象](/guide/mocking)
167+
了解更多信息 [模拟对象](/guide/mocking)
169168

170169
## 测试覆盖率 {#coverage}
171170

@@ -195,7 +194,7 @@ export default defineConfig({
195194
})
196195
```
197196

198-
了解更多信息 [覆盖率](/guide/coverage)
197+
了解更多信息 [覆盖率](/guide/coverage)
199198

200199
## 源码内联测试 {#in-source-testing}
201200

@@ -267,7 +266,7 @@ test('my types work properly', () => {
267266

268267
## 分片 {#sharding}
269268

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

272271
```bash
273272
vitest --shard=1/2 --reporter=blob --coverage
@@ -300,6 +299,7 @@ export default defineConfig(({ mode }) => ({
300299
你可以通过手动捕获这些错误来禁用此行为。Vitest 会认为回调已由你处理,不会再报告该错误。
301300

302301
::: code-group
302+
303303
```ts [setup.node.js]
304304
// 在 Node.js
305305
process.on('unhandledRejection', () => {
@@ -310,6 +310,7 @@ process.on('uncaughtException', () => {
310310
// 你自己的处理程序
311311
})
312312
```
313+
313314
```ts [setup.browser.js]
314315
// 在浏览器
315316
window.addEventListener('error', () => {
@@ -320,6 +321,7 @@ window.addEventListener('unhandledrejection', () => {
320321
// 你自己的处理程序
321322
})
322323
```
324+
323325
:::
324326

325327
或者,你也可以使用 [`dangerouslyIgnoreUnhandledErrors`](/config/dangerouslyignoreunhandlederrors) 选项来忽略报告的错误。Vitest 仍会报告它们,但它们不会影响测试结果(退出码不会改变)。

guide/filtering.md

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,92 @@
11
---
22
title: 测试筛选 | 指南
33
---
4-
<!-- TODO: translation -->
4+
55
# 测试筛选 {#test-filtering}
66

7-
As your test suite grows, running every test on every change becomes slow and distracting. If you're fixing a bug in a single module, you don't need to wait for hundreds of unrelated tests to finish. Test filtering lets you narrow down which tests run so you can stay focused on the code you're actively working on.
7+
随着测试套件不断增长,每次改动都运行全部测试会变得缓慢且干扰开发节奏。如果你只是在修复某个模块中的一个 bug,就没必要等待数百个无关测试全部跑完。测试过滤可以让你缩小执行范围,只运行当前正在处理的代码相关测试,从而更专注地开发。
88

9-
Vitest offers several ways to filter tests: from the command line, inside your test files, and through tags. Each approach is useful in different situations.
9+
Vitest 提供了多种过滤测试的方式:可以通过命令行、在测试文件内部,或使用标签来过滤。不同的方法适用于不同的场景。
1010

11-
::: tip Performance Note
12-
Filters like `-t`, `--tags-filter`, `.only`, and `.skip` are applied *per test file*Vitest still has to run each test file to discover which tests match. In a large project, this overhead adds up even if only a few tests actually execute.
11+
::: tip 性能说明
12+
`-t``--tags-filter``.only` `.skip` 这样的过滤方式,都是按 _文件级_ 应用的。也就是说,Vitest 仍然需要运行每个测试文件,才能找出哪些测试符合条件。在大型项目中,即使最终真正执行的测试只有少数几个,这部分开销也会逐渐累积变大。
1313

14-
To avoid this, always pass a file path alongside your filter so Vitest only loads the files you care about:
14+
为了避免这种情况,建议在使用过滤条件时同时传入文件路径,这样 Vitest 就只会加载你关注的哪些文件:
1515

1616
```bash
1717
vitest utils.test.ts -t "handles empty input"
1818
```
1919

20-
Alternatively, you can use the [`--experimental.preParse`](/config/experimental#experimental-preparse) flag, which parses test files to discover test names without fully executing them:
20+
另外,你也可以使用 [`--experimental.preParse`](/config/experimental#experimental-preparse) 参数。它会通过解析测试文件来发现测试名称,而不需要完整执行这些文件:
2121

2222
```bash
2323
vitest --experimental.preParse -t "handles empty input"
2424
```
25+
2526
:::
2627

27-
## Filtering by File Name
28+
## 按文件名过滤 {#filtering-by-file-name}
2829

29-
The simplest way to run a subset of tests is to pass a filename pattern as a CLI argument. Vitest will only run test files whose path contains the given string:
30+
在命令行参数中传入文件名的形式,是运行部分测试最简单的方式。Vitest 只会运行路径中包含该字符串的测试文件:
3031

3132
```bash
3233
vitest basic
3334
```
3435

35-
This matches any test file with `basic` in its path:
36+
匹配路径中包含 `basic` 的任意测试文件:
3637

3738
```
3839
basic.test.ts
3940
basic-foo.test.ts
4041
basic/foo.test.ts
4142
```
4243

43-
This is useful when you know which file you need to work on and want to skip everything else.
44+
适用于明确知道需要处理哪些文件,并希望跳过其他所有内容。
4445

45-
## Filtering by Test Name
46+
## 按测试名称过滤 {#filtering-by-test-name}
4647

47-
Sometimes the test you care about is buried in a file with many other tests. The `-t` (or `--testNamePattern`) option filters by the test's name rather than the filename. It accepts a regex pattern and matches against the full test name, which includes any `describe` block names:
48+
有时,你关心的那个测试会埋在一个包含许多其他测试的文件里。`-t`(或 `--testNamePattern`)选项会按测试名称而不是文件名进行过滤。它接受一个正则表达式,并会匹配完整的测试名称,其中也包括所有 `describe` 代码块的名称:
4849

4950
```bash
5051
vitest -t "handles empty input"
5152
```
5253

53-
You can combine this with a file filter to narrow things down further:
54+
你可以与文件过滤器结合使用,进一步缩小范围:
5455

5556
```bash
5657
vitest utils -t "handles empty input"
5758
```
5859

59-
This runs only tests whose name matches `"handles empty input"` inside files matching `utils`.
60+
这仅运行匹配 `utils` 的文件中名称匹配 `"handles empty input"` 的测试。
6061

61-
## Filtering by Line Number
62+
## 按行号过滤 {#filtering-by-line-number}
6263

63-
When you're looking at a specific test in your editor, you often just want to run *that one test*. You can point directly to a line number:
64+
当你在编辑器里查看某个具体测试时,通常只想运行那 _一个测试_。你可以直接指定对应的行号:
6465

6566
```bash
6667
vitest basic/foo.test.ts:10
6768
```
6869

69-
Vitest will run the test that contains line 10. This requires the full filename (relative or absolute):
70+
Vitest 将运行包含第 10 行的测试。这需要完整的文件名(相对路径或绝对路径):
7071

7172
```bash
7273
vitest basic/foo.test.ts:10 #
7374
vitest ./basic/foo.test.ts:10 #
7475
vitest /users/project/basic/foo.test.ts:10 #
75-
vitest foo:10 #partial name won't work
76-
vitest ./basic/foo:10 #missing file extension
76+
vitest foo:10 #部分名称无效
77+
vitest ./basic/foo:10 #缺少文件扩展名
7778
```
7879

79-
To run multiple specific tests, separate them with spaces:
80+
要运行多个特定测试,用空格分隔它们:
8081

8182
```bash
8283
vitest basic/foo.test.ts:10 basic/foo.test.ts:25 #
83-
vitest basic/foo.test.ts:10-25 #ranges are not supported
84+
vitest basic/foo.test.ts:10-25 #不支持范围
8485
```
8586

86-
## Filtering by Tags
87+
## 按标签过滤 {#filtering-by-tags}
8788

88-
For larger projects, you may want to categorize tests and run them by category. [Tags](/guide/test-tags) let you label tests and then filter by those labels from the CLI:
89+
对于更大的项目,你可能希望对测试进行分类,并按类别运行它们。通过 [标签](/guide/test-tags),可以让你为测试添加标记,然后通过命令行按这些标签进行过滤:
8990

9091
```ts
9192
test('renders a form', { tags: ['frontend'] }, () => {
@@ -101,70 +102,70 @@ test('calls an external API', { tags: ['backend'] }, () => {
101102
vitest --tags-filter=frontend
102103
```
103104

104-
This is particularly helpful in CI pipelines where you might want to run frontend and backend tests in separate jobs, or skip slow integration tests during quick checks.
105+
适合在 CI 流水线中使用,例如你可能希望在不同的任务中分别运行前端测试和后端测试,或者在快速检查时跳过耗时较长的集成测试。
105106

106-
## Focusing on Specific Tests with `.only`
107+
## 使用 `.only` 聚焦特定测试 {#focusing-on-specific-tests-with--only}
107108

108-
When you're debugging a failing test, you want to run just that test without modifying CLI arguments every time. Adding `.only` to a test or suite tells Vitest to skip everything else in the file:
109+
当你调试失败的测试时,希望只运行该测试而无需每次都修改 CLI 参数。在测试用例或测试套件中添加 `.only` 会告诉 Vitest 跳过文件中的所有其他内容:
109110

110111
```ts
111112
import { describe, expect, it } from 'vitest'
112113

113114
describe.only('suite', () => {
114115
it('test', () => {
115-
// This runs because the suite is marked with .only
116+
// 仅有这个会运行,因为测试套件被标记为 .only
116117
expect(Math.sqrt(4)).toBe(2)
117118
})
118119
})
119120

120121
describe('another suite', () => {
121122
it('skipped test', () => {
122-
// This does not run
123+
// 这个不会运行
123124
expect(Math.sqrt(4)).toBe(2)
124125
})
125126

126127
it.only('focused test', () => {
127-
// This also runs because it is marked with .only
128+
// 这个也会运行,因为它被标记为 .only
128129
expect(Math.sqrt(4)).toBe(2)
129130
})
130131
})
131132
```
132133

133-
You can use `.only` on both `describe` blocks and individual tests. When any test or suite in a file is marked with `.only`, all unmarked tests in that file are skipped.
134+
你可以在 `describe` 块和单个测试上使用 `.only`。当文件中任何测试或测试套件被标记为 `.only` 时,该文件中所有未标记的测试都会被跳过。
134135

135136
::: warning
136-
Remember to remove `.only` before committing. By default, Vitest will fail the entire test run if it encounters `.only` in CI (when `process.env.CI` is set), preventing you from accidentally skipping tests in your pipeline. This behavior is controlled by the [`allowOnly`](/config/allowonly) option.
137+
请记得在提交前移除 `.only`。默认情况下,在 CI 环境中(即设置了 `process.env.CI` 时), Vitest 遇到 `.only` 会使整个测试运行失败,以防你在流水线中意外跳过测试。此行为由 [`allowOnly`](/config/allowonly) 选项控制。
137138

138-
To catch `.only` even earlier, the [`no-focused-tests`](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/no-focused-tests.md) ESLint rule (also available in [oxlint](https://oxc.rs/docs/guide/usage/linter/rules/jest/no-focused-tests.html)) can flag it in your editor before you commit.
139+
为了更早地捕获 `.only`[`no-focused-tests`](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/no-focused-tests.md) ESLint 规则(在 [oxlint](https://oxc.rs/docs/guide/usage/linter/rules/jest/no-focused-tests.html) 中也可用)这样你在提交前就能在编辑器里发现它。
139140
:::
140141

141-
## Skipping Tests with `.skip`
142+
## 使用 `.skip` 跳过测试 {#skipping-tests-with-skip}
142143

143-
The opposite of `.only` is `.skip`. Use it to temporarily disable a test or suite without deleting it. Skipped tests still show up in the report so you don't forget about them:
144+
`.skip` `.only` 正好相反。使用它可以临时禁用某个测试或测试套件而无需删除它。被跳过的测试仍会显示在报告中,这样你就不会忘记它们:
144145

145146
```ts
146147
import { describe, expect, it } from 'vitest'
147148

148149
describe.skip('skipped suite', () => {
149150
it('test', () => {
150-
// This entire suite is skipped
151+
// 整个测试套件都被跳过
151152
expect(Math.sqrt(4)).toBe(2)
152153
})
153154
})
154155

155156
describe('suite', () => {
156157
it.skip('skipped test', () => {
157-
// Just this one test is skipped
158+
// 只有这个测试被跳过
158159
expect(Math.sqrt(4)).toBe(2)
159160
})
160161
})
161162
```
162163

163-
This is useful when a test is flaky or depends on an external service that's temporarily down. It lets you keep the test in place as a reminder while unblocking the rest of the suite.
164+
它让你可以保留测试作为提醒,同时不影响套件的其他部分运行,适用于测试不稳定或依赖于暂时不可用的外部服务。
164165

165-
## Placeholder Tests with `.todo`
166+
## 使用 `.todo` 作为占位测试 {#placeholder-tests-with-todo}
166167

167-
When planning new features, you might know what tests you'll need before you write the actual implementation. `.todo` marks a test as planned but not yet written. It shows up in the report as a reminder:
168+
在规划新功能时,你可能在编写实际实现之前,就知道需要哪些测试。`.todo` 用于将测试标记为已计划但尚未编写。它会作为提醒显示在报告中:
168169

169170
```ts
170171
import { describe, it } from 'vitest'
@@ -176,4 +177,4 @@ describe('suite', () => {
176177
})
177178
```
178179

179-
Unlike `.skip`, a `.todo` test has no test body. It's purely a placeholder for future work.
180+
`.skip` 不同,`.todo` 测试没有测试体。它纯粹是为后续工作预留的占位符。

0 commit comments

Comments
 (0)