Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
13c1798
docs(coverage): update guide/coverage.md (#845)
web3Worker-x Dec 12, 2025
aed2fe7
Modify the v3 guide link in mocking-classes
HHG66 Dec 12, 2025
0c0d73e
docs: translate /guide/mocking/classes (#849)
z-kunf Dec 12, 2025
c78921c
Modify the v3 guide link in mocking-classes
HHG66 Dec 13, 2025
da7666d
docs(cn): Modify the v3 guide link in mocking-classes (#850)
NoiseFan Dec 13, 2025
002bc3a
docs(reporters): fix link formatting issues in the document (#848)
chengyanqi123 Dec 13, 2025
f1f8ed0
docs(cn): update menu item translate (#846)
WuPeiM Dec 13, 2025
8ad3cc7
docs(cn): update testTimeout default value and description (#851)
faluoys Dec 13, 2025
98fc4f1
docs(runner): update /config/runner.md (#853)
duyidao Dec 13, 2025
2404f85
docs(benchmark): update config/benchmark description (#856)
Glue6 Dec 13, 2025
83c1672
docs(guide/advanced/reporters): update comment for MyDefaultReporter …
wshiqyuan Dec 13, 2025
5074c91
docs(fileparallelism): translate config/fileparallelism
tz200 Dec 13, 2025
bc65046
docs(fileparallelism): translate config/fileparallelism
tz200 Dec 13, 2025
1335684
Update config/fileparallelism.md
thinkasany Dec 13, 2025
44ea53c
Merge pull request #860 from tz200/docs/fileparallelism
thinkasany Dec 13, 2025
9b3936d
docs(watch): update /config/watch comment (#852)
GitHubdinosaur Dec 13, 2025
25d1a56
docs(testnamepattern): translate config/testnamepattern (#855)
zero-years Dec 13, 2025
3ffd13c
docs(config): Translate the setupfiles.md document into Chinese. (#857)
chengyanqi123 Dec 13, 2025
f1bb99e
docs(execargy): translate config/execargv.md (#861)
duyidao Dec 14, 2025
fbd8435
docs(execargy): translate config/execargv.md (#862)
NoiseFan Dec 14, 2025
931282d
docs(execargy): translate config/execargv.md (#863)
NoiseFan Dec 14, 2025
0308539
docs(cn): fix `/api/advanced/test-case` formatter (#865)
Blackxoxo Dec 14, 2025
893274d
docs(hookTimeout): translate /config/hookTimeout.md (#866)
duyidao Dec 14, 2025
7962455
docs(config): correct Chinese translation in themeConfig (#864)
QmAhaha Dec 14, 2025
c1566af
docs(cn): update the translation of the untubeEnvs configuration docu…
wshiqyuan Dec 15, 2025
157b7b0
docs(locators): translate /config/browser/locators.md (#870)
duyidao Dec 15, 2025
3ce24ad
docs(viewport): translate /config/browser/viewport.md (#872)
duyidao Dec 15, 2025
bdd2843
docs(cn): update /guide/brower/why (#871)
NoiseFan Dec 15, 2025
0bb4b5e
docs(screenshotDirectory): translate /config/screenshotDirectory.md (…
duyidao Dec 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ export default ({ mode }: { mode: string }) => {
},
themeConfig: {
logo: '/logo.svg',

lastUpdatedText: '最后更新',
docFooter: {
prev: '上一页',
next: '下一页',
},
editLink: {
pattern: 'https://github.com/vitest-dev/docs-cn/edit/dev/:path',
text: '在 GitHub 上编辑此页面',
Expand Down Expand Up @@ -212,7 +216,7 @@ export default ({ mode }: { mode: string }) => {
collapsed: false,
items: [
{
text: 'Config File',
text: '配置文件',
link: '/config/',
},
{
Expand Down
2 changes: 1 addition & 1 deletion api/advanced/test-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function ok(): boolean
function meta(): TaskMeta
```

在测试执行期间附加到测试上的自定义[元数据](/api/advanced/metadata)。我们可以在测试运行期间通过给 `ctx.task.meta` 对象分配属性来附加元数据。
在测试执行期间附加到测试上的自定义 [元数据](/api/advanced/metadata)。我们可以在测试运行期间通过给 `ctx.task.meta` 对象分配属性来附加元数据。

```ts {3,6}
import { test } from 'vitest'
Expand Down
50 changes: 25 additions & 25 deletions config/benchmark.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,66 @@ outline: deep

# benchmark <Experimental /> {#benchmark}

- **Type:** `{ include?, exclude?, ... }`
- **类型:** `{ include?, exclude?, ... }`

Options used when running `vitest bench`.
运行 `vitest bench` 时使用的选项。

## benchmark.include

- **Type:** `string[]`
- **Default:** `['**/*.{bench,benchmark}.?(c|m)[jt]s?(x)']`
- **类型:** `string[]`
- **默认值:** `['**/*.{bench,benchmark}.?(c|m)[jt]s?(x)']`

Include globs for benchmark test files
匹配包含基准测试文件的 glob 规则。

## benchmark.exclude

- **Type:** `string[]`
- **Default:** `['node_modules', 'dist', '.idea', '.git', '.cache']`
- **类型:** `string[]`
- **默认值:** `['node_modules', 'dist', '.idea', '.git', '.cache']`

Exclude globs for benchmark test files
匹配排除基准测试文件的 glob 规则。

## benchmark.includeSource

- **Type:** `string[]`
- **Default:** `[]`
- **类型:** `string[]`
- **默认值:** `[]`

Include globs for in-source benchmark test files. This option is similar to [`includeSource`](#includesource).
匹配包含内联基准测试文件的 glob 规则。此选项类似于 [`includeSource`](#includesource)

When defined, Vitest will run all matched files with `import.meta.vitest` inside.
定义后,Vitest 将运行所有匹配的文件,其中包含 `import.meta.vitest`

## benchmark.reporters

- **Type:** `Arrayable<BenchmarkBuiltinReporters | Reporter>`
- **Default:** `'default'`
- **类型:** `Arrayable<BenchmarkBuiltinReporters | Reporter>`
- **默认值:** `'default'`

Custom reporter for output. Can contain one or more built-in report names, reporter instances, and/or paths to custom reporters.
用于定义输出的自定义报告器。它可以包含一个或多个内置报告名称、报告实例和(或)自定义报告的路径。

## benchmark.outputFile

Deprecated in favor of `benchmark.outputJson`.
已弃用,建议使用 `benchmark.outputJson`

## benchmark.outputJson {#benchmark-outputJson}

- **Type:** `string | undefined`
- **Default:** `undefined`
- **类型:** `string | undefined`
- **默认值:** `undefined`

A file path to store the benchmark result, which can be used for `--compare` option later.
存储基准测试结果的文件路径,可用于稍后的 `--compare` 选项。

For example:
例如:

```sh
# save main branch's result
# 保存主分支的结果
git checkout main
vitest bench --outputJson main.json

# change a branch and compare against main
# 切换到另一个分支并与主分支进行比较
git checkout feature
vitest bench --compare main.json
```

## benchmark.compare {#benchmark-compare}

- **Type:** `string | undefined`
- **Default:** `undefined`
- **类型:** `string | undefined`
- **默认值:** `undefined`

A file path to a previous benchmark result to compare against current runs.
用于与当前运行结果进行比较的先前基准测试结果的文件路径。
8 changes: 4 additions & 4 deletions config/browser/locators.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ outline: deep

# browser.locators

Options for built-in [browser locators](/api/browser/locators).
内置 [浏览器定位器](/api/browser/locators) 的选项。

## browser.locators.testIdAttribute

- **Type:** `string`
- **Default:** `data-testid`
- **类型:** `string`
- **默认值:** `data-testid`

Attribute used to find elements with `getByTestId` locator.
用于通过 `getByTestId` 定位器查找元素的属性。
6 changes: 3 additions & 3 deletions config/browser/screenshotdirectory.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ outline: deep

# browser.screenshotDirectory

- **Type:** `string`
- **Default:** `__screenshots__` in the test file directory
- **类型:** `string`
- **默认值:** 测试文件目录中的 `__screenshots__`

Path to the screenshots directory relative to the `root`.
相对于 `root` 的屏幕截图目录路径。
6 changes: 3 additions & 3 deletions config/browser/viewport.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ outline: deep

# browser.viewport

- **Type:** `{ width, height }`
- **Default:** `414x896`
- **类型:** `{ width, height }`
- **默认值:** `414x896`

Default iframe's viewport.
默认 `iframe` 的视口。
8 changes: 4 additions & 4 deletions config/execargv.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ outline: deep

# execArgv

- **Type:** `string[]`
- **Default:** `[]`
- **类型:** `string[]`
- **默认值:** `[]`

Pass additional arguments to `node` in the runner worker. See [Command-line API | Node.js](https://nodejs.org/docs/latest/api/cli.html) for more information.
向运行器工作线程中 `node` 传递额外参数,请参阅 [Command-line API | Node.js](https://nodejs.org/docs/latest/api/cli.html)

:::warning
Be careful when using, it as some options may crash worker, e.g. `--prof`, `--title`. See https://github.com/nodejs/node/issues/41103.
使用时要小心,因为某些选项(如 `--prof` `--title`)可能会导致 `worker` 崩溃。更多内容请参阅 https://github.com/nodejs/node/issues/41103
:::
10 changes: 5 additions & 5 deletions config/fileparallelism.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ outline: deep

# fileParallelism

- **Type:** `boolean`
- **Default:** `true`
- **CLI:** `--no-file-parallelism`, `--fileParallelism=false`
- **类型:** `boolean`
- **默认值:** `true`
- **命令行终端:** `--no-file-parallelism`, `--fileParallelism=false`

Should all test files run in parallel. Setting this to `false` will override `maxWorkers` option to `1`.
所有测试文件应该并行运行。 将其设置为 `false` 将覆盖 `maxWorkers` 选项为 `1`

::: tip
This option doesn't affect tests running in the same file. If you want to run those in parallel, use `concurrent` option on [describe](/api/#describe-concurrent) or via [a config](#sequence-concurrent).
此选项不会影响在同一文件中运行的测试。如果你想并行运行这些程序,使用 `concurrent` 选项 [describe](/api/#describe-concurrent) 或通过 [配置](#sequence-concurrent)
:::
8 changes: 4 additions & 4 deletions config/hooktimeout.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ outline: deep

# hookTimeout

- **Type:** `number`
- **Default:** `10_000` in Node.js, `30_000` if `browser.enabled` is `true`
- **CLI:** `--hook-timeout=10000`, `--hookTimeout=10000`
- **类型:** `number`
- **默认值:** Node.js 环境下为 `10_000`,当 `browser.enabled` `true` 时为 `30_000`
- **命令行终端:** `--hook-timeout=10000``--hookTimeout=10000`

Default timeout of a hook in milliseconds. Use `0` to disable timeout completely.
钩子 (`hook`) 的默认超时时间 (以毫秒为单位)。使用 `0` 完全禁用超时。
2 changes: 1 addition & 1 deletion config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
outline: deep
---

# 配置索引 {#configuring-vitest}
# 配置 Vitest {#configuring-vitest}

如果我们正在使用 Vite 并且拥有一个 `vite.config` 文件,Vitest 会读取它来匹配我们的 Vite 应用的插件和设置。如果我们想要为测试配置不同的设置,或者我们的并不特别依赖于 Vite,我们我们可以选择:

Expand Down
6 changes: 3 additions & 3 deletions config/runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ outline: deep

# runner

- **Type**: `VitestRunnerConstructor`
- **Default**: `node`, when running tests, or `benchmark`, when running benchmarks
- **类型**: `VitestRunnerConstructor`
- **默认值**: 运行测试时为 `node`,运行基准测试时为 `benchmark`

Path to a custom test runner. This is an advanced feature and should be used with custom library runners. You can read more about it in [the documentation](/api/advanced/runner).
自定义测试运行器的路径。这是一项高级功能,应与自定义库运行器一起使用。你可以在 [文档](/api/advanced/runner) 中阅读更多相关信息。
14 changes: 7 additions & 7 deletions config/setupfiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ outline: deep

# setupFiles

- **Type:** `string | string[]`
- **类型:** `string | string[]`

Path to setup files. They will be run before each test file.
setup 文件的路径。它们将在每个测试文件之前运行。

:::info
Editing a setup file will automatically trigger a rerun of all tests.
编辑设置文件将自动触发所有测试的重新运行。
:::

You can use `process.env.VITEST_POOL_ID` (integer-like string) inside to distinguish between workers.
你可以在全局设置文件中使用 `process.env.VITEST_POOL_ID`(类似整数的字符串)来区分不同的线程。

:::tip
Note, that if you are running [`--isolate=false`](#isolate), this setup file will be run in the same global scope multiple times. Meaning, that you are accessing the same global object before each test, so make sure you are not doing the same thing more than you need.
请注意,如果运行 [`--isolate=false`](#isolate),这个配置文件将在全局范围内多次运行。这意味着每次测试前都要访问同一个全局对象,因此请确保不要重复做同一件事。
:::

For example, you may rely on a global variable:
比如,你可能依赖于一个全局变量:

```ts
import { config } from '@some-testing-lib'
Expand All @@ -30,7 +30,7 @@ if (!globalThis.defined) {
globalThis.defined = true
}

// hooks are reset before each suite
// 钩子函数会在每个测试套件前重置
afterEach(() => {
cleanup()
})
Expand Down
11 changes: 5 additions & 6 deletions config/testnamepattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ outline: deep

# testNamePattern <CRoot /> {#testnamepattern}

- **Type** `string | RegExp`
- **CLI:** `-t <pattern>`, `--testNamePattern=<pattern>`, `--test-name-pattern=<pattern>`
- **类型** `string | RegExp`
- **命令行终端** `-t <pattern>`, `--testNamePattern=<pattern>`, `--test-name-pattern=<pattern>`

Run tests with full names matching the pattern.
If you add `OnlyRunThis` to this property, tests not containing the word `OnlyRunThis` in the test name will be skipped.
运行符合全名匹配的测试。如果在此属性中添加 `OnlyRunThis`,则测试中不包含 `OnlyRunThis` 关键字的用例将会被跳过。

```js
import { expect, test } from 'vitest'

// run
// 运行
test('OnlyRunThis', () => {
expect(true).toBe(true)
})

// skipped
// 跳过
test('doNotRun', () => {
expect(true).toBe(true)
})
Expand Down
8 changes: 4 additions & 4 deletions config/testtimeout.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ outline: deep

# testTimeout

- **Type:** `number`
- **Default:** `5_000` in Node.js, `15_000` if `browser.enabled` is `true`
- **CLI:** `--test-timeout=5000`, `--testTimeout=5000`
- **类型:** `number`
- **默认值:** Node.js 环境下为 `5_000`,当 `browser.enabled` `true` 时为 `15_000`
- **命令行终端:** `--test-timeout=5000`, `--testTimeout=5000`

Default timeout of a test in milliseconds. Use `0` to disable timeout completely.
测试的默认超时时间(以毫秒为单位)。 使用 `0` 完全禁用超时。
6 changes: 3 additions & 3 deletions config/unstubenvs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ outline: deep

# unstubEnvs

- **Type:** `boolean`
- **Default:** `false`
- **类型:** `boolean`
- **默认值:** `false`

Should Vitest automatically call [`vi.unstubAllEnvs()`](/api/vi#vi-unstuballenvs) before each test.
将在每次测试前调用 [`vi.unstubAllEnvs()`](/api/vi#vi-unstuballenvs)

```js [vitest.config.js]
import { defineConfig } from 'vitest/config'
Expand Down
12 changes: 6 additions & 6 deletions config/watch.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ outline: deep

# watch <CRoot /> {#watch}

- **Type:** `boolean`
- **Default:** `!process.env.CI && process.stdin.isTTY`
- **CLI:** `-w`, `--watch`, `--watch=false`
- **类型:** `boolean`
- **默认值:** `!process.env.CI && process.stdin.isTTY`
- **命令行终端:** `-w`, `--watch`, `--watch=false`

Enable watch mode
启动监听模式

In interactive environments, this is the default, unless `--run` is specified explicitly.
交互式环境中,默认启用监听模式,除非显式传入 `--run`

In CI, or when run from a non-interactive shell, "watch" mode is not the default, but can be enabled explicitly with this flag.
CI 或非交互式 shell 中,监听模式默认关闭,但可通过该标志显式启用。
2 changes: 1 addition & 1 deletion guide/advanced/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { DefaultReporter } from 'vitest/reporters'

export default class MyDefaultReporter extends DefaultReporter {
// do something
// 在此实现自定义功能
}
```

Expand Down
3 changes: 1 addition & 2 deletions guide/browser/why.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ outline: deep
# 什么是浏览器模式? {#why-browser-mode}

## 动机 {#motivation}
<!-- TODO: translation -->
We developed the Vitest browser mode feature to help improve testing workflows and achieve more accurate and reliable test results. This addition to our testing API allows developers to run tests in a native browser environment. In this section, we'll explore the motivations behind this feature and its benefits for testing.
我们开发了 Vitest 浏览器模式功能,以帮助改进测试工作流程并实现更准确、可靠的测试结果。这个实验性的测试 API 增加了在本地浏览器环境中运行测试的功能。本节将深入探讨该功能的开发初衷以及其为测试工作带来的优势。

### 不同的测试方式 {#different-ways-of-testing}

Expand Down
2 changes: 1 addition & 1 deletion guide/coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ export function ignored() { // [!code error]

## 覆盖率性能 {#coverage-performance}

如果你的项目中代码覆盖率生成较慢,请参阅[性能测试分析 | 代码覆盖率](/guide/profiling-test-performance.html#code-coverage)。
如果你的项目中代码覆盖率生成较慢,请参阅 [性能测试分析 | 代码覆盖率](/guide/profiling-test-performance.html#code-coverage)。

## UI 模式 {#vitest-ui}

Expand Down
4 changes: 2 additions & 2 deletions guide/mocking/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import { Dog } from './dog.js'
vi.mock(import('./dog.js'), () => {
const Dog = vi.fn(class {
feed = vi.fn()
// ... other mocks
// 模拟其他内容...
})
return { Dog }
})
Expand Down Expand Up @@ -154,5 +154,5 @@ expect(nameSpy).toHaveBeenCalledTimes(1)
:::

::: danger
在 Vitest 4 中引入了使用 `vi.fn()` 模拟类的功能。在此之前,你必须直接使用 `function` 和 `prototype` 继承。参见 [v3 指南](https://v3.vitest.dev/guide/mocking.html#classes)。
在 Vitest 4 中引入了使用 `vi.fn()` 模拟类的功能。在此之前,你必须直接使用 `function` 和 `prototype` 继承。参见 [v3 指南](https://v3.cn.vitest.dev/guide/mocking.html#classes)。
:::
4 changes: 2 additions & 2 deletions guide/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,10 @@ export default defineConfig({

:::

此外,你还可以定义自己的[自定义报告器](/guide/advanced/reporters),并通过指定文件路径来使用它们:
此外,你还可以定义自己的 [自定义报告器](/guide/advanced/reporters),并通过指定文件路径来使用它们:

```bash
npx vitest --reporter=./path/to/reporter.ts
```

自定义报告器应实现[报告器接口](https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/node/types/reporter.ts)。
自定义报告器应实现 [报告器接口](https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/node/types/reporter.ts)。