diff --git a/.vitepress/components/FeaturesList.vue b/.vitepress/components/FeaturesList.vue
index 82526fba..3c211264 100644
--- a/.vitepress/components/FeaturesList.vue
+++ b/.vitepress/components/FeaturesList.vue
@@ -6,7 +6,7 @@ import ListItem from './ListItem.vue'
与
- Vite
+ Vite
通用的配置、转换器、解析器和插件。
使用与你的应用相同的设置来运行测试!
diff --git a/.vitepress/config.ts b/.vitepress/config.ts
index c621fe9a..c11a0ee0 100644
--- a/.vitepress/config.ts
+++ b/.vitepress/config.ts
@@ -898,7 +898,7 @@ export default ({ mode }: { mode: string }) => {
],
},
{
- text: '测试',
+ text: '测试标签',
link: '/guide/test-tags',
},
{
diff --git a/api/advanced/reporters.md b/api/advanced/reporters.md
index fa7135f3..a839cf40 100644
--- a/api/advanced/reporters.md
+++ b/api/advanced/reporters.md
@@ -79,7 +79,7 @@ function onTestRunStart(
): Awaitable
```
-当新的测试运行开始时调用此方法。它接收计划运行的 [测试规范](/api/advanced/test-specification) 数组。此数组是只读的,仅用于信息目的。
+当新的测试运行开始时调用此方法。它接收计划运行的 [TestSpecification](/api/advanced/test-specification) 数组。此数组是只读的,仅用于信息目的。
如果 Vitest 没有找到任何要运行的测试文件,此事件将以空数组调用,然后 [`onTestRunEnd`](#ontestrunend) 将立即被调用。
diff --git a/api/advanced/test-case.md b/api/advanced/test-case.md
index 009acba0..fd05f14d 100644
--- a/api/advanced/test-case.md
+++ b/api/advanced/test-case.md
@@ -292,14 +292,14 @@ function artifacts(): ReadonlyArray
```
通过 `recordArtifact` API,在测试执行过程中记录的 [测试产物](/api/advanced/artifacts)。
-
+
## toTestSpecification 4.1.0 {#totestspecification}
```ts
function toTestSpecification(): TestSpecification
```
-Returns a new [test specification](/api/advanced/test-specification) that can be used to filter or run this specific test case.
+返回一个新的 [TestSpecification](/api/advanced/test-specification),可用于过滤或运行此特定测试用例。
## logs 5.0.0 {#logs}
@@ -307,4 +307,4 @@ Returns a new [test specification](/api/advanced/test-specification) that can be
function logs(): ReadonlyArray
```
-Console logs recorded during the test execution.
+测试执行期间记录的 console 日志。
diff --git a/api/advanced/test-module.md b/api/advanced/test-module.md
index 53b4b40a..33e4e712 100644
--- a/api/advanced/test-module.md
+++ b/api/advanced/test-module.md
@@ -33,13 +33,13 @@ if (task.type === 'module') {
'example.test.ts' // ✅
'project\\example.test.ts' // ❌
```
-
+
## viteEnvironment 4.1.0 {#viteenvironment}
-This is a Vite's [`DevEnvironment`](https://vite.dev/guide/api-environment) that transforms all files inside of the test module.
+这是一个 Vite 的 [`DevEnvironment`](https://cn.vite.dev/guide/api-environment),它会转换测试模块内的所有文件。
-::: details History
-- `v4.0.15`: added as experimental
+::: details 历史
+- `v4.0.15`:已作为实验性功能
:::
## state
@@ -88,20 +88,20 @@ function diagnostic(): ModuleDiagnostic
```ts
interface ModuleDiagnostic {
/**
- * 导入和初始化环境所需的时间。
+ * 导入和初始化环境所需的时间
*/
readonly environmentSetupDuration: number
/**
- * Vitest 设置测试运行环境(运行器、模拟等)所需的时间。
+ * Vitest 设置测试运行环境(运行器、模拟等)所需的时间
*/
readonly prepareDuration: number
/**
- * 导入测试模块所需的时间。
- * 这包括导入模块中的所有内容以及执行套件回调函数。
+ * 导入测试模块所需的时间
+ * 这包括导入模块中的所有内容以及执行套件回调函数
*/
readonly collectDuration: number
/**
- * 导入设置模块所需的时间。
+ * 导入设置模块所需的时间
*/
readonly setupDuration: number
/**
@@ -109,30 +109,31 @@ interface ModuleDiagnostic {
*/
readonly duration: number
/**
- * 模块使用的内存量(以字节为单位)。
- * 此值仅在使用 `logHeapUsage` 参数执行测试时才可用。
+ * 模块使用的内存量(以字节为单位)
+ * 此值仅在使用 `logHeapUsage` 参数执行测试时才可用
*/
readonly heap: number | undefined
/**
- * Vitest处理的每个非外部化依赖项的导入时间。
+ * Vitest处理的每个非外部化依赖项的导入时间
*/
readonly importDurations: Record
/**
- * The id of the worker that ran this file. This value cannot be higher than `maxWorkers`.
- * If file did not run yet, this will be 0.
+ * 运行此文件的 worker 的 ID。此值不能高于 `maxWorkers`。
+ * 如果文件尚未运行,此值将为 0。
*
- * **Warning**: Node.js tests and browser tests run in different pools and do not share `concurrencyId`.
- * It is possible to have multiple modules with the same `concurrencyId` because of that.
- * Use `project.isBrowserEnabled()` to distinguish the concurrency.
+ * **警告**:Node.js 测试和浏览器测试在不同的池中运行,不共享 `concurrencyId`
+ * 因此,可能存在多个具有相同 `concurrencyId` 的模块。
+ * 使用 `project.isBrowserEnabled()` 来区分并发性。
*/
readonly concurrencyId: number
/**
- * Incremental number of the worker that ran this file. This number increases with each worker.
- * If file did not run yet, this will be 0.
+ * å
+ * 运行此文件的 worker 的递增编号。编号随每个 worker 进行递增
+ * 如果文件尚未运行,此值将为 0
*
- * **Warning**: Node.js tests and browser tests run in different pools and do not share `workerId`.
- * It is possible to have multiple modules with the same `workerId` because of that.
- * Use `project.isBrowserEnabled()` to distinguish the concurrency.
+ * **警告**:Node.js 测试和浏览器测试在不同的池中运行,不共享 `workerId`
+ * 因此,可能存在多个具有相同 `workerId` 的模块
+ * 使用 `project.isBrowserEnabled()` 来区分并发性
*/
readonly workerId: number
}
@@ -146,14 +147,14 @@ interface ImportDuration {
totalTime: number
}
```
-
+
## logs 5.0.0 {#logs}
```ts
function logs(): ReadonlyArray
```
-Console logs recorded on top level of the module during test collection.For example:
+测试收集期间在模块顶层记录的 console 日志。例如:
```ts
console.log('included') // [!code highlight]
diff --git a/api/advanced/test-project.md b/api/advanced/test-project.md
index 67016bf8..acc23e4c 100644
--- a/api/advanced/test-project.md
+++ b/api/advanced/test-project.md
@@ -179,7 +179,7 @@ function createSpecification(
): TestSpecification
```
-创建一个 [测试规范](/api/advanced/test-specification),可用于 [`vitest.runTestSpecifications`](/api/advanced/vitest#runtestspecifications)。规范将测试文件限定到特定的 `project` 和测试 `locations`(可选)。测试 [位置](/api/advanced/test-case#location) 是源代码中定义测试的代码行。如果提供了位置,Vitest 将仅运行在这些行上定义的测试。请注意,如果定义了 [`testNamePattern`](/config/testnamepattern),则它也将被应用。
+创建一个 [TestSpecification](/api/advanced/test-specification),可用于 [`vitest.runTestSpecifications`](/api/advanced/vitest#runtestspecifications)。规范将测试文件限定到特定的 `project` 和测试 `locations`(可选)。测试 [位置](/api/advanced/test-case#location) 是源代码中定义测试的代码行。如果提供了位置,Vitest 将仅运行在这些行上定义的测试。请注意,如果定义了 [`testNamePattern`](/config/testnamepattern),则它也将被应用。
```ts
import { resolve } from 'node:path/posix'
diff --git a/api/advanced/test-specification.md b/api/advanced/test-specification.md
index 9b95cba2..d403ef4c 100644
--- a/api/advanced/test-specification.md
+++ b/api/advanced/test-specification.md
@@ -45,7 +45,7 @@ Vite 模块图中的模块 ID。通常,它是一个使用 POSIX 分隔符的
测试模块将运行的 [`pool`](/config/pool)。
::: danger
-在启用 [`typecheck.enabled`](/config/typecheck#typecheck-enabled) 配置的情况下,单个测试项目中可能存在多个运行池。这意味着可能出现多个测试规范共享相同 `moduleId` 但使用不同 `pool` 的情况。请注意,后续版本将仅支持单一运行池模式。
+在启用 [`typecheck.enabled`](/config/typecheck#typecheck-enabled) 配置的情况下,单个测试项目中可能存在多个运行池。这意味着可能出现多个 TestSpecification 共享相同 `moduleId` 但使用不同 `pool` 的情况。请注意,后续版本将仅支持单一运行池模式。
:::
## testLines
@@ -81,7 +81,7 @@ describe('a group of tests', () => { // [!code error]
## testIds 4.1.0 {#testids}
-当前测试规范中需要运行的任务 ID 集合。
+当前 TestSpecification 中需要运行的任务 ID 集合。
## testTagsFilter 4.1.0 {#testtagsfilter}
diff --git a/api/advanced/test-suite.md b/api/advanced/test-suite.md
index 4c8ccd28..c6d44d68 100644
--- a/api/advanced/test-suite.md
+++ b/api/advanced/test-suite.md
@@ -222,14 +222,14 @@ describe('the validation works correctly', { meta: { decorated: true } }, () =>
:::tip
如果元数据是在收集阶段(而非 `test` 函数内部)附加的,那么它将在 available 的 [`onTestModuleCollected`](./reporters#ontestmodulecollected) 中可用。
:::
-
+
## logs 5.0.0 {#logs}
```ts
function logs(): ReadonlyArray
```
-Console logs recorded during test collection of this suite. For example:
+此套件测试收集期间记录的 console 日志。例如:
```ts
describe('suite', () => {
@@ -251,4 +251,4 @@ describe('suite', () => {
function toTestSpecification(): TestSpecification
```
-返回一个新的 [测试规范](/api/advanced/test-specification),该规范可用于筛选或运行此特定测试套件。
+返回一个新的 [TestSpecification](/api/advanced/test-specification),该规范可用于筛选或运行此特定测试套件。
diff --git a/api/advanced/vitest.md b/api/advanced/vitest.md
index 0749af20..28a1dc99 100644
--- a/api/advanced/vitest.md
+++ b/api/advanced/vitest.md
@@ -140,12 +140,12 @@ function globTestSpecifications(
): Promise
```
-此方法通过收集所有项目中的每个测试来构造新的 [测试规范](/api/advanced/test-specification),使用 [`project.globTestFiles`](/api/advanced/test-project#globtestfiles)。它接受字符串过滤器以匹配测试文件 - 这些过滤器与 [CLI 支持的过滤器](/guide/filtering#cli) 相同。
+此方法通过收集所有项目中的每个测试来构造新的 [TestSpecification](/api/advanced/test-specification),使用 [`project.globTestFiles`](/api/advanced/test-project#globtestfiles)。它接受字符串过滤器以匹配测试文件 - 这些过滤器与 [CLI 支持的过滤器](/guide/filtering#cli) 相同。
-此方法自动缓存所有测试规范。当我们下次调用 [`getModuleSpecifications`](#getmodulespecifications) 时,它将返回相同的规范,除非在此之前调用了 [`clearSpecificationsCache`](#clearspecificationscache)。
+此方法自动缓存所有 TestSpecification。当我们下次调用 [`getModuleSpecifications`](#getmodulespecifications) 时,它将返回相同的规范,除非在此之前调用了 [`clearSpecificationsCache`](#clearspecificationscache)。
::: warning
-从 Vitest 3 开始,如果 `poolMatchGlob` 有多个池或启用了 `typecheck`,则可能有多个具有相同模块 ID(文件路径)的测试规范。这种可能性将在 Vitest 4 中移除。
+从 Vitest 3 开始,如果 `poolMatchGlob` 有多个池或启用了 `typecheck`,则可能有多个具有相同模块 ID(文件路径)的 TestSpecification。这种可能性将在 Vitest 4 中移除。
:::
```ts
@@ -162,7 +162,7 @@ function getRelevantTestSpecifications(
): Promise
```
-此方法通过调用 [`project.globTestFiles`](/api/advanced/test-project#globtestfiles) 解析每个测试规范。它接受字符串过滤器以匹配测试文件 - 这些过滤器与 [CLI 支持的过滤器](/guide/filtering#cli) 相同。如果指定了 `--changed` 参数,则列表将被过滤为仅包含已更改的文件。`getRelevantTestSpecifications` 不会运行任何测试文件。
+此方法通过调用 [`project.globTestFiles`](/api/advanced/test-project#globtestfiles) 解析每个 TestSpecification。它接受字符串过滤器以匹配测试文件 - 这些过滤器与 [CLI 支持的过滤器](/guide/filtering#cli) 相同。如果指定了 `--changed` 参数,则列表将被过滤为仅包含已更改的文件。`getRelevantTestSpecifications` 不会运行任何测试文件。
::: warning
此方法可能很慢,因为它需要过滤 `--changed` 参数。如果我们只需要测试文件列表,请不要使用它。
@@ -191,7 +191,7 @@ function collect(filters?: string[]): Promise
执行测试文件而不运行测试回调。`collect` 返回未处理的错误和 [测试模块](/api/advanced/test-module) 数组。它接受字符串过滤器以匹配测试文件 - 这些过滤器与 [CLI 支持的过滤器](/guide/filtering#cli) 相同。
-此方法根据配置的 `include`、`exclude` 和 `includeSource` 值解析测试规范。有关更多信息,请参阅 [`project.globTestFiles`](/api/advanced/test-project#globtestfiles)。如果指定了 `--changed` 参数,则列表将被过滤为仅包含已更改的文件。
+此方法根据配置的 `include`、`exclude` 和 `includeSource` 值解析 TestSpecification。有关更多信息,请参阅 [`project.globTestFiles`](/api/advanced/test-project#globtestfiles)。如果指定了 `--changed` 参数,则列表将被过滤为仅包含已更改的文件。
::: warning
请注意,Vitest 不使用静态分析来收集测试。Vitest 将像运行常规测试一样在隔离环境中运行每个测试文件。
@@ -237,7 +237,7 @@ function standalone(): Promise
function getModuleSpecifications(moduleId: string): TestSpecification[]
```
-返回与模块 ID 相关的测试规范列表。ID 应已解析为绝对文件路径。如果 ID 不匹配 `include` 或 `includeSource` 模式,则返回的数组将为空。
+返回与模块 ID 相关的 TestSpecification 列表。ID 应已解析为绝对文件路径。如果 ID 不匹配 `include` 或 `includeSource` 模式,则返回的数组将为空。
此方法可以根据 `moduleId` 和 `pool` 返回已缓存的规范。但请注意,[`project.createSpecification`](/api/advanced/test-project#createspecification) 总是返回一个新实例,并且不会自动缓存。但是,当调用 [`runTestSpecifications`](#runtestspecifications) 时,规范会自动缓存。
@@ -251,7 +251,7 @@ function getModuleSpecifications(moduleId: string): TestSpecification[]
function clearSpecificationsCache(moduleId?: string): void
```
-当调用 [`globTestSpecifications`](#globtestspecifications) 或 [`runTestSpecifications`](#runtestspecifications) 时,Vitest 会自动缓存每个文件的测试规范。此方法会根据第一个参数清除给定文件的缓存或整个缓存。
+当调用 [`globTestSpecifications`](#globtestspecifications) 或 [`runTestSpecifications`](#runtestspecifications) 时,Vitest 会自动缓存每个文件的 TestSpecification。此方法会根据第一个参数清除给定文件的缓存或整个缓存。
## runTestSpecifications
@@ -288,7 +288,7 @@ function runTestFiles(
): Promise
```
-该功能会根据文件路径过滤器自动创建待运行的测试规范。
+该功能会根据文件路径过滤器自动创建待运行的 TestSpecification。
这与 [`start`](#start) 的不同之处在于:它不会创建覆盖率提供程序、不会触发 `onInit` 和 `onWatcherStart` 事件,且在无文件可运行时也不会抛出错误(此时函数将返回空数组且不会触发测试运行)。
@@ -312,7 +312,7 @@ function collectTests(
执行测试文件而不运行测试回调。`collectTests` 返回未处理的错误和 [测试模块](/api/advanced/test-module) 数组。
-此方法与 [`collect`](#collect) 完全相同,但我们需要自己提供测试规范。
+此方法与 [`collect`](#collect) 完全相同,但我们需要自己提供 TestSpecification。
::: warning
请注意,Vitest 不使用静态分析来收集测试。Vitest 将像运行常规测试一样在隔离环境中运行每个测试文件。
diff --git a/api/assert.md b/api/assert.md
index 5dad88bd..c2c0410a 100644
--- a/api/assert.md
+++ b/api/assert.md
@@ -1,12 +1,12 @@
# assert
Vitest 从 [`chai`](https://www.chaijs.com/api/assert/) 重新导出了 `assert` 方法,用于验证不变量。
-
-::: warning In-Source Testing {#in-source-testing}
-When using [assertion functions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) such as `assert` from `import.meta.vitest` in [in-source tests](/guide/in-source), TypeScript reports error `TS2775` because they must be called via an explicitly annotated name. Annotate the variable with `Chai.Assert` or call it directly:
+
+::: warning 源码内联测试 {#in-source-testing}
+在 [源码内联测试](/guide/in-source) 中使用来自 `import.meta.vitest` 的 [断言函数](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions)(例如 `assert`)时,TypeScript 会报告 `TS2775` 错误,因为这类函数必须通过带有显式类型注解的名称来调用。请为该变量标注 Chai.Assert,或直接调用它:
::: code-group
-```ts [Annotated variable]
+```ts [类型注释变量]
if (import.meta.vitest) {
const { test, assert } = import.meta.vitest // [!code --]
const { test } = import.meta.vitest // [!code ++]
@@ -17,7 +17,7 @@ if (import.meta.vitest) {
})
}
```
-```ts [Direct call]
+```ts [直接调用]
if (import.meta.vitest) {
const { test, assert } = import.meta.vitest // [!code --]
const { test } = import.meta.vitest // [!code ++]
diff --git a/api/browser/commands.md b/api/browser/commands.md
index 284e4b04..303c5e2f 100644
--- a/api/browser/commands.md
+++ b/api/browser/commands.md
@@ -14,11 +14,11 @@ outline: deep
在浏览器测试中,可借助 `readFile`、`writeFile` 与 `removeFile` 三个 API 完成文件操作。自 Vitest 3.2 起,所有路径均以 [project](/guide/projects) 根目录为基准解析(根目录默认为 `process.cwd()`,可手动重写);旧版本则以当前测试文件所在目录为基准。
默认情况下,Vitest 使用 `utf-8` 编码,但你可以使用选项覆盖它。
-
+
::: tip
-The built-in file commands follow Vite's [`server.fs`](https://vitejs.dev/config/server-options.html#server-fs-allow) restrictions for security reasons.
+出于安全原因,内置的文件命令遵循 Vite 的 [`server.fs`](https://cn.G/config/server-options.html#server-fs-allow) 限制。
-`writeFile` and `removeFile` also require write access through [`browser.api.allowWrite`](/config/browser/api) and [`api.allowWrite`](/config/api#api-allowwrite).
+`writeFile` 和 `removeFile` 还需要通过 [`browser.api.allowWrite`](/config/browser/api) 和 [`api.allowWrite`](/config/api#api-allowwrite) 获得写入权限。
:::
```ts
@@ -60,7 +60,7 @@ expect(input).toHaveValue('a')
::: warning
CDP session 仅适用于 `playwright` provider,并且仅在使用 `chromium` 浏览器时有效。有关详细信息,请参阅 playwright 的 [`CDPSession`](https://playwright.dev/docs/api/class-cdpsession) 文档。
-CDP is a privileged debugging API. It is available only when browser API write and exec operations are enabled through [`browser.api.allowWrite`](/config/browser/api#api-allowwrite), [`browser.api.allowExec`](/config/browser/api#api-allowexec), [`api.allowWrite`](/config/api#api-allowwrite), and [`api.allowExec`](/config/api#api-allowexec).
+CDP 是一个特权调试 API。仅当通过 [`browser.api.allowWrite`](/config/browser/api#api-allowwrite)、[`browser.api.allowExec`](/config/browser/api#api-allowexec)、[`api.allowWrite`](/config/api#api-allowwrite) 和 [`api.allowExec`](/config/api#api-allowexec) 启用浏览器 API 写入和执行操作时,该 API 才可用。
:::
## 自定义命令 {#custom-commands}
@@ -126,7 +126,7 @@ declare module 'vitest/browser' {
::: warning
如果自定义命令具有相同的名称,则它们将覆盖内置命令。
:::
-
+
::: warning Security
Custom commands run in the Vitest Node process and are callable from browser test code through Vitest's browser RPC connection. They can access local files, environment variables, network services, databases, shell commands, and other Node APIs.
diff --git a/api/browser/context.md b/api/browser/context.md
index 534508b3..8a3b8915 100644
--- a/api/browser/context.md
+++ b/api/browser/context.md
@@ -63,41 +63,41 @@ export const commands: BrowserCommands
:::
使用 [Commands API](/api/browser/commands) 如果你需要访问 Playwright 的 `page` 对象。
-
+
```ts
export const page: {
/**
- * 更改 iframe 视口的大小。
+ * 更改 iframe 视口的大小
*/
viewport: (width: number, height: number) => Promise
/**
- * 对测试 iframe 或特定元素进行截图。
- * @returns 截图文件的路径或路径和 base64 编码。
+ * 对测试 iframe 或特定元素进行截图
+ * @returns 截图文件的路径或路径和 base64 编码
*/
screenshot: ((options: Omit & { base64: true }) => Promise<{
path: string
base64: string
}>) & ((options?: ScreenshotOptions) => Promise)
/**
- * Add a trace marker when browser tracing is enabled.
+ * 当启用浏览器追踪时,添加一个追踪标记
*/
mark(name: string, options?: { stack?: string; kind?: BrowserTraceEntryKind }): Promise
/**
- * Group multiple operations under a trace marker when browser tracing is enabled.
+ * 当启用浏览器追踪时,将多个操作分组在一个追踪标记下
*/
mark(name: string, body: () => T | Promise, options?: { stack?: string; kind?: BrowserTraceEntryKind }): Promise
/**
- * Extend default `page` object with custom methods.
+ * 使用自定义方法扩展默认的 `page` 对象
*/
extend: (methods: Partial) => BrowserPage
/**
- * 将一个 HTML 元素包装在 `Locator` 中。在查询元素时,搜索将始终返回此元素。
+ * 将一个 HTML 元素包装在 `Locator` 中。在查询元素时,搜索将始终返回此元素
*/
elementLocator(element: Element): Locator
/**
* iframe 定位器。这是一个进入 iframe body 的文档定位器
- * 其工作原理与 `page` 对象类似。
- * **Warning:** 目前,仅有 `playwright` 提供程序支持该功能。
+ * 其工作原理与 `page` 对象类似
+ * **Warning:** 目前,仅有 `playwright` 提供程序支持该功能
*/
frameLocator(iframeElement: Locator): FrameLocator
@@ -122,7 +122,7 @@ export const page: {
请注意,如果 `save` 设置为 `false`,`screenshot` 将始终返回 base64 字符串。
在这种情况下,`path` 也会被忽略。
:::
-
+
### mark
```ts
@@ -134,13 +134,13 @@ function mark(
): Promise
```
-Adds a named marker to the trace timeline for the current test.
+在追踪时间线为当前测试向添加一个命名标记。
-Pass `options.stack` to override the callsite location in trace metadata. This is useful for wrapper libraries that need to preserve the end-user source location.
+传递 `options.stack` 以覆盖追踪元数据中的调用位置。适用于需要保留最终用户源代码位置的封装库。
-Pass `options.kind` to categorize your marker as specific type, for example as `'action'`.
+传递 `options.kind` 以将你的标记分类为特定类型,例如 `'action'`。
-If you pass a callback, Vitest creates a trace group with this name, runs the callback, and closes the group automatically.
+如果你传递一个回调函数,Vitest 将使用此名称创建一个追踪组,运行回调,并自动关闭该组。
```ts
import { page } from 'vitest/browser'
@@ -156,9 +156,9 @@ await page.mark('submit flow', async () => {
```
::: tip
-This method is useful only when [`browser.trace`](/config/browser/trace) is enabled.
+此方法仅在启用 [`browser.trace`](/config/browser/trace) 时生效。
-A server-side equivalent is available on the [`BrowserCommandContext`](/api/browser/commands#recording-trace-markers) so [custom commands](/api/browser/commands#custom-commands) can record markers attributed to the test that triggered them.
+在 [`BrowserCommandContext`](/api/browser/commands#recording-trace-markers) 上有一个服务器端的等效方法,因此 [自定义命令](/api/browser/commands#custom-commands) 可以记录触发它们的测试的标记。
:::
### frameLocator
@@ -179,23 +179,23 @@ const frame = page.frameLocator(
await frame.getByText('Hello World').click() // ✅
await frame.click() // ❌ 不可用
```
-
-::: danger IMPORTANT
-By default `frameLocator` does not support querying elements with `expect.element()` in cross-origin iframes. Interactive methods, such as `.click()` work fine. This is different behaviour than Playwright.
+
+::: danger 重要
+这与 Playwright 的行为不同。默认情况下,`frameLocator` 不支持在跨域 iframe 中使用 `expect.element()` 查询元素。交互式方法(例如 `.click()`)可以正常工作。
```ts
const frame = page.frameLocator(page.getByTestId('cross-origin-iframe'))
const button = frame.getByRole('button', { name: 'Submit' })
-await button.click() // Interactive methods work fine ✅
-await expect.element(button).toBeVisible() // Querying elements does not work ❌
+await button.click() // 交互式方法可以正常工作 ✅
+await expect.element(button).toBeVisible() // 查询元素失败 ❌
```
-If you need to work with cross-origin iframes, you'll need to pass `args: ["--disable-web-security"]` in [`launchOptions`](/config/browser/playwright.html#launchoptions). Or alternatively create a custom [browser command](/api/browser/commands.html#custom-commands) that accesses the iframe on server side where it's available.
+如果你需要处理跨域 iframe,你需要在 [`launchOptions`](/config/browser/playwright.html#launchoptions) 中传递 `args: ["--disable-web-security"]`。或者创建一个自定义的 [浏览器命令](/api/browser/commands.html#custom-commands),在服务器端访问可用的 iframe。
:::
-::: danger IMPORTANT
-At the moment, the `frameLocator` method is only supported by the `playwright` provider.
+::: danger 重要
+目前,`frameLocator` 方法只有 `playwright` 支持。
交互方法(如 `click` 或 `fill`)在 iframe 内的元素上始终可用,但使用 `expect.element` 进行断言时要求 iframe 具有[同源策略](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy)。
:::
@@ -206,8 +206,8 @@ At the moment, the `frameLocator` method is only supported by the `playwright` p
::: warning
CDP 会话仅适用于 `playwright` provider,并且仅在使用 `chromium` 浏览器时有效。有关详细信息,请参阅 playwright 的 [`CDPSession`](https://playwright.dev/docs/api/class-cdpsession)文档。
-
-CDP is a privileged debugging API. It is available only when browser API write and exec operations are enabled through [`browser.api.allowWrite`](/config/browser/api#api-allowwrite), [`browser.api.allowExec`](/config/browser/api#api-allowexec), [`api.allowWrite`](/config/api#api-allowwrite), and [`api.allowExec`](/config/api#api-allowexec).
+
+CDP 是一个特权调试 API。仅当通过 [`browser.api.allowWrite`](/config/browser/api#api-allowwrite)、[`browser.api.allowExec`](/config/browser/api#api-allowexec)、[`api.allowWrite`](/config/api#api-allowwrite) 和 [`api.allowExec`](/config/api#api-allowexec) 启用浏览器 API 写入和执行操作时,该 API 才可用。
:::
```ts
diff --git a/api/describe.md b/api/describe.md
index d2cce684..c7caffdd 100644
--- a/api/describe.md
+++ b/api/describe.md
@@ -207,8 +207,8 @@ describe.concurrent('suite', () => {
test.concurrent('concurrent test 3', async () => { /* ... */ })
})
```
-
-Set `concurrent` to `false` to opt out of concurrency inherited from a parent suite or [`sequence.concurrent`](/config/sequence#sequence-concurrent):
+
+将 `concurrent` 设为 `false`,从而禁用从父级测试套件或 [`sequence.concurrent`](/config/sequence#sequence-concurrent) 继承的并发执行:
```ts
describe.concurrent('suite', () => {
diff --git a/api/expect.md b/api/expect.md
index dac621f2..90871988 100644
--- a/api/expect.md
+++ b/api/expect.md
@@ -108,10 +108,11 @@ interface ExpectPoll extends ExpectStatic {
(actual: (options: { signal: AbortSignal }) => T, options?: { interval?: number; timeout?: number; message?: string }): Promise>>
}
```
-
-`expect.poll` reruns the _assertion_ until it is succeeded. You can configure how often Vitest retries and how long it waits by setting `interval` and `timeout` options. The `timeout` applies to the whole polling operation, including pending callback and async matcher execution.
-The callback receives an `AbortSignal` that is aborted when the poll timeout is reached.
+`expect.poll` 会重新运行 _断言_,直到成功为止。你可以通过设置 `interval` 和 `timeout` 选项来配置 Vitest 的重试频率和等待时长。`timeout` 适用于整个轮询操作,包括尚未完成的回调和异步匹配器的执行。
+
+回调函数会接收一个 `AbortSignal`,该信号会在轮询超时时被中止。
+当轮询超时时,传给回调的 `AbortSignal` 会被中止。
如果在 `expect.poll` 回调中抛出错误,Vitest 将重试直到超时为止。
@@ -1013,12 +1014,12 @@ it('render basic', async () => {
- **类型:** `(snapshot?: string, hint?: string) => void`
与 [`toMatchInlineSnapshot`](#tomatchinlinesnapshot) 类似,但期望的值与 [`toThrow`](#toThrow) 相同。
-
+
## toMatchAriaSnapshot 4.1.4 {#tomatcharisnapshot}
-- **Type:** `() => void`
+- **类型:** `() => void`
-Captures the accessibility tree of a DOM element and generate a snapshot file or compares it against a stored snapshot. See the [ARIA Snapshots guide](/guide/browser/aria-snapshots) for more details.
+捕获 DOM 元素的无障碍树,生成快照文件或将其与存储的快照进行比较。详情请参阅 [ARIA 快照指南](/guide/browser/aria-snapshots)。
```ts
import { expect, test } from 'vitest'
@@ -1036,9 +1037,10 @@ test('navigation accessibility', () => {
## toMatchAriaInlineSnapshot 4.1.4 {#tomatchariainlinesnapshot}
-- **Type:** `(snapshot?: string) => void`
+- **类型:** `(snapshot?: string) => void`
+
+与 [`toMatchAriaSnapshot`](#tomatcharisnapshot) 相同,但会将快照内联存储在测试文件中。详情请参阅 [ARIA 快照指南](/guide/browser/aria-snapshots)。
-Same as [`toMatchAriaSnapshot`](#tomatcharisnapshot), but stores the snapshot inline in the test file. See the [ARIA Snapshots guide](/guide/browser/aria-snapshots) for more details.
```ts
import { expect, test } from 'vitest'
@@ -1454,14 +1456,14 @@ test('spy function returns bananas on second call', async () => {
expect(sell).toHaveNthResolvedWith(2, { product: 'bananas' })
})
```
-
+
## toHaveBeenExhausted 5.0.0 {#tohavebeenexhausted}
-- **Type:** `() => void`
+- **类型:** `() => void`
-This assertion checks that every behavior registered on a [`vi.when`](/api/vi#vi-when) chain has been consumed. A behavior is considered exhausted when it has been called the number of times specified by its `times` option, or at least once for behaviors that apply indefinitely.
+此断言检查 [`vi.when`](/api/vi#vi-when) 链上注册的每个行为是否都已被消耗。当某个行为被调用次数达到其 `times` 选项指定的次数时,或者对于无限期生效的行为至少被调用一次时,该行为就被视为已消耗。
-Requires a `When` chain returned by `vi.when` to be passed to `expect`.
+需要将 `vi.when` 返回的 `When` 链传递给 `expect`。
```ts
import { expect, test, vi } from 'vitest'
@@ -1476,15 +1478,15 @@ test('all behaviors were consumed', () => {
expect(w).not.toHaveBeenExhausted()
- spy(1) // consumes the `thenReturnOnce` behavior
- spy(2) // satisfies the `thenReturn` behavior (called at least once)
+ spy(1) // 消耗 `thenReturnOnce` 行为
+ spy(2) // 满足 `thenReturn` 行为(至少被调用一次)
expect(w).toHaveBeenExhausted()
})
```
::: warning
-A `When` chain with no registered behaviors is never considered exhausted. `toHaveBeenExhausted` only passes when at least one `calledWith` with an associated action (`then*`) has been registered and every registered behavior has been fully consumed.
+一个没有注册任何行为的 `When` 链永远不会被视为已消耗。只有当至少注册了一个带有关联操作 (`then*`) 的 `calledWith`,并且每个已注册的行为都已被完全消耗时,`toHaveBeenExhausted` 才会通过。
:::
## called 4.1.0 {#called}
diff --git a/api/hooks.md b/api/hooks.md
index 18d21bdd..c898fecd 100644
--- a/api/hooks.md
+++ b/api/hooks.md
@@ -34,8 +34,8 @@ beforeEach(async () => {
```
此处,`beforeEach` 确保每个测试都会添加用户。
-
-`beforeEach` can also return an optional cleanup function. It's similar to [`afterEach`](#aftereach). The only difference is that it's executed after all other `afterEach` hooks:
+
+`beforeEach` 也可以返回一个可选的清理函数。这类似于 [`afterEach`](#aftereach)。唯一的区别是,它会在所有其他 `afterEach` 钩子之后执行:
```ts
import { beforeEach } from 'vitest'
diff --git a/api/test.md b/api/test.md
index 38fb60c0..4bd81a77 100644
--- a/api/test.md
+++ b/api/test.md
@@ -216,8 +216,8 @@ describe(
- **别名:** [`test.concurrent`](#test-concurrent)
是否与测试套件中其他并发测试并行运行。
-
-Set `concurrent` to `false` to opt out of concurrency inherited from [`describe.concurrent`](/api/describe#describe-concurrent) or [`sequence.concurrent`](/config/sequence#sequence-concurrent):
+
+将 `concurrent` 设为 `false`,以禁用从 [`describe.concurrent`](/api/describe#describe-concurrent) 或 [`sequence.concurrent`](/config/sequence#sequence-concurrent) 继承的并发执行:
```ts
test('runs sequentially', { concurrent: false }, async () => {
@@ -666,9 +666,9 @@ test.concurrent.for([
挂载在 test.extend 实例上的 `aroundAll` 钩子,继承 [`test.extend`](#test-extend) 的类型。更多内容请参阅 [aroundAll](/api/hooks#aroundall)。
## bench {#bench}
-
-::: warning Updated in Vitest 5
-The benchmarking API has been rewritten. `bench` is no longer a top-level import from `vitest`, and the `bench.skip` / `bench.only` / `bench.todo` helpers have been removed. `bench` is now a [test-context fixture](/guide/test-context#bench) accessed from inside a `test()`.
-See the [Benchmarking guide](/guide/benchmarking) for the new API.
+::: warning 已在 Vitest 5 中更新
+基准测试 API 已重构。`bench` 不再是 `vitest` 的顶层导入,并且 `bench.skip`/`bench.only`/`bench.todo` 工具函数已被移除。`bench` 现在是从 `test()` 内部访问的 [测试上下文](/guide/test-context#bench)。
+
+新的 API 详情请参阅 [基准测试指南](/guide/benchmarking)。
:::
diff --git a/config/browser/api.md b/config/browser/api.md
index 99931666..5dd5d2ca 100644
--- a/config/browser/api.md
+++ b/config/browser/api.md
@@ -15,8 +15,8 @@ outline: deep
- **类型:** `boolean`
- **默认值:** 未暴露公网时默认为 `true`,否则为 `false`
-
-Vitest 通过接收来自浏览器的 WebSocket 连接来保存 [测试注解](/guide/test-annotations)、[测试产物](/api/advanced/artifacts) 和 [快照](/guide/snapshot)。这意味着任何能连接到该 API 的人都可在你机器的项目根目录(由 [`fs.allow`](https://cn.vite.dev/config/server-options#server-fs-allow) 配置)内执行任意代码。 This option also gates privileged browser APIs that can write files indirectly, such as raw Chrome DevTools Protocol access through [`cdp()`](/api/browser/context#cdp).
+
+Vitest 通过接收来自浏览器的 WebSocket 连接来保存 [测试注解](/guide/test-annotations)、[测试产物](/api/advanced/artifacts) 和 [快照](/guide/snapshot)。这意味着任何能连接到该 API 的人都可在你机器的项目根目录(由 [`fs.allow`](https://cn.vite.dev/config/server-options#server-fs-allow) 配置)内执行任意代码。此选项也限制了可以间接写入文件的特权浏览器 API,例如通过 [`cdp()`](/api/browser/context#cdp) 进行的原始 Chrome DevTools 协议访问。
当浏览器服务器未暴露至互联网(主机为 `localhost`)时,默认值设为 `true` 不会构成安全隐患。若你修改了主机配置,Vitest 将默认将 `allowWrite` 设为 `false` 以防止潜在的恶意写入风险。
@@ -25,4 +25,4 @@ Vitest 通过接收来自浏览器的 WebSocket 连接来保存 [测试注解](/
- **类型:** `boolean`
- **默认值:** 未暴露至公网时默认为 `true`,否则为 `false`
-允许通过 [UI 模式](/guide/ui) 运行任意测试文件。此配置仅作用于界面交互元素(及其背后的服务端代码)的可执行权限。如果 UI 模式被禁用,则该配置不生效。This option also gates privileged browser APIs that can execute code indirectly, such as raw Chrome DevTools Protocol access through [`cdp()`](/api/browser/context#cdp). 更多信息请参阅 [`api.allowExec`](/config/api#api-allowexec)。
+允许通过 [UI 模式](/guide/ui) 运行任意测试文件。此配置仅作用于界面交互元素(及其背后的服务端代码)的可执行权限。如果 UI 模式被禁用,则该配置不生效。此选项也限制了可以间接写入文件的特权浏览器 API,例如通过 [`cdp()`](/api/browser/context#cdp) 进行的原始 Chrome DevTools 协议访问。更多信息请参阅 [`api.allowExec`](/config/api#api-allowexec)。
diff --git a/config/browser/commands.md b/config/browser/commands.md
index a6b30a9b..ea1c60be 100644
--- a/config/browser/commands.md
+++ b/config/browser/commands.md
@@ -9,9 +9,9 @@ outline: deep
- **默认值:** `{ readFile, writeFile, ... }`
可在浏览器测试中通过 `vitest/browser` 导入 [自定义命令](/api/browser/commands)。
-
-::: warning Security
-Commands run in the Vitest Node process. If a command exposes filesystem, process, network, database, or shell access based on browser-provided input, validate and restrict that input inside the command. Built-in file commands apply Vite `server.fs` checks and write-access checks, but custom commands are responsible for their own protections.
-See [Custom Commands security notes](/api/browser/commands#custom-commands).
+::: warning 安全性
+命令在 Vitest 的 Node 进程中运行。如果一个命令基于浏览器提供的输入暴露了文件系统、进程、网络、数据库或 shell 访问权限,请在命令内部验证并限制该输入。内置的文件命令会应用 Vite 的 `server.fs` 检查和写入权限检查,但自定义命令需要自行负责其防护措施。
+
+请参阅 [自定义命令安全性说明](/api/browser/commands#custom-commands)。
:::
diff --git a/config/browser/locators.md b/config/browser/locators.md
index 42028721..6422a58c 100644
--- a/config/browser/locators.md
+++ b/config/browser/locators.md
@@ -27,17 +27,17 @@ outline: deep
const locator = page.getByText('Hello, World', { exact: true })
await locator.click()
```
-
+
## browser.locators.errorFormat 5.0.0 {#browser-locators-errorformat}
-- **Type:** `'html' | 'aria' | 'all'`
-- **Default:** `'all'`
+- **类型:** `'html' | 'aria' | 'all'`
+- **默认值:** `'all'`
-Controls what Vitest prints when a locator cannot find an element. Vitest prints information for the DOM subtree where the locator search ran, or `document.body` for page-level locators.
+控制当定位器找不到元素时 Vitest 打印的内容。Vitest 会打印定位器搜索所运行的 DOM 子树的信息,对于页面级定位器则打印 `document.body` 的信息。
-- `'html'` prints that DOM subtree as HTML using [`utils.prettyDOM`](/api/browser/context#prettydom).
-- `'aria'` prints that DOM subtree as an [ARIA snapshot](/guide/browser/aria-snapshots), which focuses on accessible roles, names, and state.
-- `'all'` prints the ARIA snapshot first, followed by the HTML output.
+- `'html'` 使用 [`utils.prettyDOM`](/api/browser/context#prettydom) 将该 DOM 子树以 HTML 格式打印。
+- `'aria'` 将该 DOM 子树以 [ARIA 快照](/guide/browser/aria-snapshots) 格式打印,重点关注无障碍角色、名称和状态。
+- `'all'` 先打印 ARIA 快照,然后输出 HTML。
```ts
import { defineConfig } from 'vitest/config'
@@ -54,7 +54,7 @@ export default defineConfig({
})
```
-For example, `all` displays a following error:
+例如,`all` 会显示如下错误:
```html
VitestBrowserElementError: Cannot find element with locator: getByRole('button', { name: 'Save' })
diff --git a/config/browser/trace.md b/config/browser/trace.md
index fa8d14bd..a694d0d1 100644
--- a/config/browser/trace.md
+++ b/config/browser/trace.md
@@ -10,8 +10,8 @@ outline: deep
- **默认值:** `'off'`
捕获浏览器测试运行的追踪记录。你可以通过 [Playwright Trace Viewer](https://trace.playwright.dev/) 预览追踪文件。
-
-See [Playwright Traces](/guide/browser/playwright-traces) for the full workflow.
+
+完整工作流程请参阅 [Playwright 追踪](/guide/browser/playwright-traces)。
该选项支持以下取值:
diff --git a/config/browser/webdriverio.md b/config/browser/webdriverio.md
index 7febaaa4..5b9df775 100644
--- a/config/browser/webdriverio.md
+++ b/config/browser/webdriverio.md
@@ -62,21 +62,15 @@ export default defineConfig({
请注意,Vitest 将忽略 `capabilities.browserName` — 请改用 [`test.browser.instances.browser`](/config/browser/instances#browser)。
:::
-
-## Headful Chrome in CI
-Vitest enables [`browser.headless`](/config/browser/headless) automatically in CI.
-If you explicitly set `headless: false` for Chrome on a Linux CI runner, Chrome
-still needs a display server. Without one, WebDriverIO or ChromeDriver can fail
-with a misleading error such as `session not created: probably user data
-directory is already in use`.
+## CI 中的有头模式 Chrome {#headful-chrome-in-ci}
-Run the test command through `xvfb-run` when you need headful Chrome in GitHub
-Actions or another Linux CI environment:
+Vitest 在 CI 中会自动启用 [`browser.headless`](/config/browser/headless)。如果你在 Linux CI 运行器上为 Chrome 显式设置了 `headless: false`,Chrome 仍然需要一个显示服务器。如果没有,WebDriverIO 或 ChromeDriver 可能会失败,并出现误导性错误,例如 `session not created: probably user data directory is already in use`。
+
+当你在 GitHub Actions 或其他 Linux CI 环境中需要使用有头模式的 Chrome 时,请通过 `xvfb-run` 运行测试命令:
```bash
xvfb-run npm test
```
-Alternatively, keep `browser.headless` enabled in CI and use headful mode only
-for local debugging.
+或者,在 CI 中保持启用 `browser.headless`,仅在本地调试时使用有头模式。
diff --git a/config/cache.md b/config/cache.md
index 8eddc27f..9fed3279 100644
--- a/config/cache.md
+++ b/config/cache.md
@@ -10,7 +10,7 @@ outline: deep
使用此选项可禁用缓存功能。当前 Vitest 会缓存测试结果,以便优先运行耗时较长和失败的测试。
-缓存目录由 Vite 的 [`cacheDir`](https://vitejs.dev/config/shared-options.html#cachedir) 选项控制:
+缓存目录由 Vite 的 [`cacheDir`](https://cn.vitejs.dev/config/shared-options.html#cachedir) 选项控制:
```ts
import { defineConfig } from 'vitest/config'
diff --git a/config/coverage.md b/config/coverage.md
index f91ac274..c29c1462 100644
--- a/config/coverage.md
+++ b/config/coverage.md
@@ -237,8 +237,8 @@ npx vitest --coverage.enabled --coverage.provider=istanbul
- **默认值:** `false`
- **可用的测试提供者:** `'v8' | 'istanbul'`
- **命令行终端:** `--coverage.thresholds.perFile`, `--coverage.thresholds.perFile=false`
-
-When `true`, each file is checked against the top-level thresholds instead of the project-wide aggregate. When set to an object, both are checked: the aggregate against the top-level thresholds, and every file against these per-file minimums.
+
+当设置为 `true` 时,每个文件都会根据顶层阈值进行检查,而不是根据项目范围的聚合值。当设置为对象时,两者都会检查:聚合值根据顶层阈值检查,每个文件根据这些按文件最小值检查。
```ts
@@ -260,7 +260,7 @@ When `true`, each file is checked against the top-level thresholds instead of th
}
```
-`{ 100: true }` is also accepted inside the object as a shortcut for setting all four metrics to `100`:
+对象中也接受 `{ 100: true }` 作为将所有四个指标设置为 `100` 的快捷方式:
```ts
@@ -276,7 +276,7 @@ When `true`, each file is checked against the top-level thresholds instead of th
}
```
-`perFile` can also be set on an individual [glob-pattern threshold](/config/coverage#coverage-thresholds-glob-pattern). Glob patterns do **not** inherit the top-level `perFile`; set it on each glob explicitly.
+perFile` 也可以设置在单独的 [全局模式阈值](/config/coverage#coverage-thresholds-glob-pattern) 上。全局模式 **不会** 继承顶层的 `perFile`;需要在每个全局模式上显式设置它。
```ts
@@ -305,7 +305,7 @@ When `true`, each file is checked against the top-level thresholds instead of th
当实际覆盖率超过配置阈值时,自动将 `lines`、`functions`、`branches` 和 `statements` 的阈值更新到配置文件中。
此选项适用于覆盖率提高时保持阈值不变。
-你也可以通过传入函数自定义阈值更新值的格式,The function receives the new threshold as the first argument and the previous threshold as the second:
+你也可以通过传入函数自定义阈值更新值的格式,该函数将新的阈值作为第一个参数传入,将先前的阈值作为第二个参数传入:
```ts
@@ -342,8 +342,8 @@ When `true`, each file is checked against the top-level thresholds instead of th
- **可用的测试提供者:** `'v8' | 'istanbul'`
设置与 glob 模式匹配的文件的阈值。
-
-Each glob pattern can set its own `perFile` (`boolean | object`), checked exactly like the top-level `perFile` but scoped to the matched files. Glob patterns do not inherit the top-level `perFile` — set it per glob.
+
+每个全局模式可以设置自己的 `perFile`(`boolean | object`),其检查方式与顶层 `perFile` 完全相同,但作用域限定在匹配的文件上。全局模式不会继承顶层的 `perFile`,需要为每个全局模式单独设置。
::: tip 注意
Vitest 会将所有文件(包括匹配 glob 模式的文件)计入全局覆盖率阈值计算。
@@ -365,7 +365,7 @@ Vitest 会将所有文件(包括匹配 glob 模式的文件)计入全局覆
functions: 90,
branches: 85,
lines: 80,
- // each matching file must individually hit the thresholds above
+ // 每个匹配的文件都必须单独达到上述阈值
perFile: true,
},
@@ -520,14 +520,14 @@ export default defineConfig({
- **命令行终端:** `--coverage.changed`, `--coverage.changed=`
仅收集自指定提交或分支以来更改的文件的代码覆盖率。设置为 `true` 时,使用已暂存和未暂存的更改。
-
+
## coverage.autoAttachSubprocess 5.0.0 {#coverage-autoattachsubprocess}
-- **Type:** `boolean`
-- **Default:** `false`
-- **Available for providers:** `'v8'`
-- **CLI:** `--coverage.autoAttachSubprocess`
+- **类型:** `boolean`
+- **默认值:** `false`
+- **可用的测试提供者:** `'v8'`
+- **命令行终端:** `--coverage.autoAttachSubprocess`
-Track coverage of the `node:child_process` and `node:worker_threads` spawned during test run.
+跟踪测试运行期间生成的 `node:child_process` 和 `node:worker_threads` 的覆盖率。
-Note that this option has some performance overhead as its using [`NODE_V8_COVERAGE`](https://nodejs.org/api/cli.html#node-v8-coveragedir) internally. This triggers Node to write lots of unnecessary files on file system.
+请注意,此选项有一定的性能开销,因为它在内部使用了 [`NODE_V8_COVERAGE`](https://nodejs.org/api/cli.html#node-v8-coveragedir)。这会触发 Node 在文件系统上写入大量不必要的文件。
diff --git a/config/experimental.md b/config/experimental.md
index f44067d9..0f377d0d 100644
--- a/config/experimental.md
+++ b/config/experimental.md
@@ -485,7 +485,7 @@ export default {
- **类型:** `boolean`
- **默认值:** `false`
-在运行测试前预解析测试规范。这会在所有文件中应用 [`.only`](/api/test#test-only) 修饰符、[`-t`](/config/testnamepattern) 测试名称模式、[`--tags-filter`](/guide/test-tags#syntax)、[测试行号](/api/advanced/test-specification#testlines) 和 [测试ID](/api/advanced/test-specification#testids) 而无需执行它们。例如,如果只有一个测试标记了 `.only`,Vitest 将跳过所有其他文件中的测试。
+在运行测试前预解析 TestSpecification。这会在所有文件中应用 [`.only`](/api/test#test-only) 修饰符、[`-t`](/config/testnamepattern) 测试名称模式、[`--tags-filter`](/guide/test-tags#syntax)、[测试行号](/api/advanced/test-specification#testlines) 和 [测试ID](/api/advanced/test-specification#testids) 而无需执行它们。例如,如果只有一个测试标记了 `.only`,Vitest 将跳过所有其他文件中的测试。
::: tip
使用 [`.only`](/api/test#test-only)、[`-t`](/config/testnamepattern) 参数或 [`--tags-filter`](/guide/test-tags#syntax) 时推荐启用此选项。
diff --git a/config/faketimers.md b/config/faketimers.md
index f92d1400..f5fa2aa1 100644
--- a/config/faketimers.md
+++ b/config/faketimers.md
@@ -24,18 +24,18 @@ outline: deep
需要模拟的全局方法和 API 名称数组。例如仅需模拟 `setTimeout()` 和 `nextTick()`,可将此属性指定为 `['setTimeout', 'nextTick']`。
当使用 `--pool=forks` 在 `node:child_process` 中运行 Vitest 时,不支持模拟 `nextTick`。NodeJS 会在 `node:child_process` 内部使用 `process.nextTick`,模拟后会导致进程挂起。使用 `--pool=threads` 运行 Vitest 时支持模拟 `nextTick`。
-
+
## fakeTimers.toNotFake
-- **Type:** `('setTimeout' | 'clearTimeout' | 'setImmediate' | 'clearImmediate' | 'setInterval' | 'clearInterval' | 'Date' | 'nextTick' | 'hrtime' | 'requestAnimationFrame' | 'cancelAnimationFrame' | 'requestIdleCallback' | 'cancelIdleCallback' | 'performance' | 'queueMicrotask')[]`
-- **Default:** `[]`
+- **类型:** `('setTimeout' | 'clearTimeout' | 'setImmediate' | 'clearImmediate' | 'setInterval' | 'clearInterval' | 'Date' | 'nextTick' | 'hrtime' | 'requestAnimationFrame' | 'cancelAnimationFrame' | 'requestIdleCallback' | 'cancelIdleCallback' | 'performance' | 'queueMicrotask')[]`
+- **默认值:** `[]`
-An array with names of global methods and APIs to keep native. All other available timers will be mocked. For example, to keep `setInterval()` native and mock all other timers, specify this property as `['setInterval']`.
+一个包含要保留为原生方法的全局方法和 API 名称的数组。其他所有可用的定时器都会被模拟。例如,要保留 `setInterval()` 为原生实现,同时模拟其他所有定时器,请将此属性指定为 `['setInterval']`。
-Mocking `nextTick` is not supported when running Vitest inside `node:child_process` by using `--pool=forks`. When running with `--pool=forks`, Vitest automatically adds `nextTick` to the `toNotFake` array.
+当通过 `--pool=forks` 在 `node:child_process` 中运行 Vitest 时,不支持模拟 `nextTick`。使用 `--pool=forks` 运行时,Vitest 会自动将 `nextTick` 添加到 `toNotFake` 数组中。
::: warning
-Using both `toFake` and `toNotFake` together is not supported.
+不支持同时使用 `toFake` 和 `toNotFake`。
:::
## fakeTimers.loopLimit
diff --git a/config/index.md b/config/index.md
index 5663a913..3910f990 100644
--- a/config/index.md
+++ b/config/index.md
@@ -9,8 +9,8 @@ outline: deep
- 创建 `vitest.config.ts`,它将具有更高的优先级,并且会**覆盖** `vite.config.ts` 中的配置(Vitest 支持所有传统的 JS 和 TS 文件扩展名,但不支持 `json`) - 这意味着我们在 `vite.config` 中的所有选项将被**忽略**。
- 向 CLI 传递 `--config` 选项,例如 `vitest --config ./path/to/vitest.config.ts`。
- 使用 `process.env.VITEST` 或在 `defineConfig` 上的 `mode` 属性(如果没有用 `--mode` 覆盖,默认设置为 `test`)也可以在 `vite.config.ts` 中有条件地应用不同的配置。请注意,像任何其他环境变量一样,`VITEST` 也会在测试中的 `import.meta.env` 上暴露出来。
-
-When an explicit `--config` option is not provided, Vitest looks for `vitest.config.{ts,mts,cts,js,mjs,cjs}` first and `vite.config.{ts,mts,cts,js,mjs,cjs}` second in the project [`root`](/config/root). If no config file is found, Vitest will run without one.
+
+当未提供显式的 `--config` 选项时,Vitest 会先在项目 [`root`](/config/root) 中查找 `vitest.config.{ts,mts,cts,js,mjs,cjs}`,然后查找 `vite.config.{ts,mts,cts,js,mjs,cjs}`。如果未找到配置文件,Vitest 将在没有配置文件的情况下运行。
要配置 Vitest 本身,请在我们的 Vite 配置中添加 `test` 属性。如果我们是从 `vite` 本身导入 `defineConfig`,我们还需要在配置文件顶部使用 [三斜杠指令](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-) 添加对 Vitest 类型引用。
@@ -79,7 +79,7 @@ export default defineConfig(configEnv => mergeConfig(
))
```
-由于 Vitest 使用 Vite 的配置,我们也可以使用 [Vite](https://vitejs.dev/config/) 中的任何配置选项。例如,使用 `define` 来定义全局变量,或者使用 `resolve.alias` 来定义别名——这些选项应该在顶级定义,而不是在 `test` 属性内部。
+由于 Vitest 使用 Vite 的配置,我们也可以使用 [Vite](https://cn.vitejs.dev/config/) 中的任何配置选项。例如,使用 `define` 来定义全局变量,或者使用 `resolve.alias` 来定义别名——这些选项应该在顶级定义,而不是在 `test` 属性内部。
## 自动依赖安装 {#automatic-dependency-installation}
diff --git a/config/repeats.md b/config/repeats.md
index 7f13ebba..1c3cd1b1 100644
--- a/config/repeats.md
+++ b/config/repeats.md
@@ -1,14 +1,14 @@
---
-title: repeats | Config
+title: repeats | 配置
outline: deep
---
# repeats
-
-- **Type:** `number`
-- **Default:** `0`
-- **CLI:** `--repeats=`
-Repeat every test a specific number of times regardless of the result. A test that uses the [`repeats`](/api/test#repeats) test option takes precedence over this value.
+- **类型:** `number`
+- **默认值:** `0`
+- **命令行终端:** `--repeats=`
-This is useful for verifying that tests are stable across multiple runs. If a test fails on any repetition, the whole test is reported as failed.
+无论结果如何,每个测试都会重复运行指定的次数。使用 [`repeats`](/api/test#repeats) 测试选项的测试将优先于此值。
+
+适用于验证测试在多次运行中的稳定性。如果测试在任何一次重复中失败,整个测试将被报告为失败。
diff --git a/config/reporters.md b/config/reporters.md
index 74bded13..efb70c8f 100644
--- a/config/reporters.md
+++ b/config/reporters.md
@@ -13,11 +13,11 @@ interface UserConfig {
type ConfigReporter = string | Reporter | [string, object?]
```
-
-- **Default:** [`'default'`](/guide/reporters#default-reporter). See [Default Reporters](/guide/reporters#default-reporters) for environment-specific behavior.
-- **CLI:**
- - `--reporter=tap` for a single reporter
- - `--reporter=verbose --reporter=github-actions` for multiple reporters
+
+- **默认值:** [`'default'`](/guide/reporters#default-reporter)。环境特定行为请参阅 [默认报告器](/guide/reporters#default-reporters)。
+- **命令行终端:**
+ - 单报告器 `--reporter=tap`
+ - 多报告器 `--reporter=verbose --reporter=github-actions`
此选项定义在 Vitest 测试运行期间可用的单个报告器或报告器列表。
diff --git a/guide/cli-generated.md b/guide/cli-generated.md
index 1eb8cecb..ca762c4c 100644
--- a/guide/cli-generated.md
+++ b/guide/cli-generated.md
@@ -199,8 +199,8 @@ Coverage reporters to use. Visit [`coverage.reporter`](/config/coverage#coverage
- **命令行终端:** `--coverage.thresholds.perFile `
- **配置:** [coverage.thresholds.perFile](/config/coverage#coverage-thresholds-perfile)
-
-检查每个文件的阈值。 `--coverage.thresholds.lines`, `--coverage.thresholds.functions`, `--coverage.thresholds.branches`, `--coverage.thresholds.statements` 为实际阈值(默认值:`false`)。Object form is available in config files only.
+
+检查每个文件的阈值。 `--coverage.thresholds.lines`, `--coverage.thresholds.functions`, `--coverage.thresholds.branches`, `--coverage.thresholds.statements` 为实际阈值(默认值:`false`)。对象形式仅在配置文件中可用。
### coverage.thresholds.autoUpdate
@@ -636,13 +636,13 @@ UI 模式和 HTML 报告器中提供的 HTML 覆盖率输出目录。
- **配置:** [retry.condition](/config/retry#retry-condition)
触发重试操作的错误信息匹配正则表达式。仅当错误信息符合该模式时才会执行重试(默认值:所有错误都会触发重试)
-
+
### repeats
-- **CLI:** `--repeats `
-- **Config:** [repeats](/config/repeats)
+- **命令行终端:** `--repeats `
+- **配置:** [repeats](/config/repeats)
-Repeat every test a specific number of times regardless of the result (default: `0`)
+无论结果如何,都重复每个测试运行到指定的次数(默认值:`0`)
### diff.aAnnotation
@@ -1011,4 +1011,4 @@ watch 模式下重新运行测试时清除终端屏幕(默认值:`true`)
- **命令行终端:** `--experimental.preParse`
- **配置:** [experimental.preParse](/config/experimental#experimental-preparse)
-在运行测试前解析测试规范。此选项将应用 `.only` 标记和测试名称模式至所有文件而不实际执行它们(默认值:`false`)。
+在运行测试前解析 TestSpecification。此选项将应用 `.only` 标记和测试名称模式至所有文件而不实际执行它们(默认值:`false`)。
diff --git a/guide/comparisons.md b/guide/comparisons.md
index 66c05bce..a4882329 100644
--- a/guide/comparisons.md
+++ b/guide/comparisons.md
@@ -10,7 +10,7 @@ title: 与其他测试框架对比 | 指南
在 Vite 项目中使用 Jest 是可能的。[@sodatea](https://bsky.app/profile/haoqun.dev) 开发了 [vite-jest](https://github.com/sodatea/vite-jest#readme) ,旨在为 [Jest](https://jestjs.io/) 提供一流的 Vite 集成。[Jest 中最后的阻碍](https://github.com/sodatea/vite-jest/blob/main/packages/vite-jest/README.md#vite-jest) 已经解决。因此,在 Vite 项目的单元测试中,Jest 仍是一个可用选项。
-然而,在 [Vite](https://vitejs.dev) 已为最常见的 Web 工具(TypeScript、JSX、最流行的 UI 框架)提供了支持的情况下,引入 Jest 会增添不必要的复杂性。如果你的应用由 Vite 驱动,那么配置和维护两个不同的管道是不合理的。如果使用 Vitest,你可以在同一个管道中进行开发、构建和测试环境的配置,它们共享相同的插件和 `vite.config.js` 文件。
+然而,在 [Vite](https://cn.vitejs.dev) 已为最常见的 Web 工具(TypeScript、JSX、最流行的 UI 框架)提供了支持的情况下,引入 Jest 会增添不必要的复杂性。如果你的应用由 Vite 驱动,那么配置和维护两个不同的管道是不合理的。如果使用 Vitest,你可以在同一个管道中进行开发、构建和测试环境的配置,它们共享相同的插件和 `vite.config.js` 文件。
即使你的库没有使用 Vite(而是例如 esbuild 或 rollup),Vitest 也是一个有趣的选择,因为它可以让你更快地运行单元测试,并通过 Vite 的模块热重载(HMR)观察模式来提高开发体验。 Vitest 提供了对大多数 Jest API 和生态系统库的兼容性,因此在大多数项目中,它应该可以直接替换 Jest 使用。
@@ -48,7 +48,7 @@ WebdriverIO 具有与 Cypress 相同的优点,允许你在真实的浏览器
[uvu](https://github.com/lukeed/uvu) 是一个适用于 Node.js 和浏览器的测试运行器。它在单个线程中运行测试,因此测试不是隔离的,可能会在不同测试间泄漏。然而,Vitest 使用 Worker Threads 来隔离测试环境、并行运行它们。
-uvu 使用 require 和 loader 钩子 进行代码转译,而 Vitest 使用 [Vite](https://vitejs.dev),因此代码会使用 Vite 的插件系统进行转换。在 Vite 已为最常见的 Web 工具(TypeScript、JSX、最流行的 UI 框架)提供了支持的情况下,引入 uvu 会增添不必要的复杂性。如果你的应用由 Vite 驱动,那么配置和维护两个不同的管道是不合理的。如果使用 Vitest,你可以在同一个管道中进行开发、构建和测试环境的配置,它们共享相同的插件和 `vite.config.js` 文件。
+uvu 使用 require 和 loader 钩子 进行代码转译,而 Vitest 使用 [Vite](https://cn.vitejs.dev),因此代码会使用 Vite 的插件系统进行转换。在 Vite 已为最常见的 Web 工具(TypeScript、JSX、最流行的 UI 框架)提供了支持的情况下,引入 uvu 会增添不必要的复杂性。如果你的应用由 Vite 驱动,那么配置和维护两个不同的管道是不合理的。如果使用 Vitest,你可以在同一个管道中进行开发、构建和测试环境的配置,它们共享相同的插件和 `vite.config.js` 文件。
uvu 不提供观察模式以在文件更改后重新运行测试, 而 Vitest 通过 Vite 的模块热重载(HMR)观察模式提供了更好的开发体验。
diff --git a/guide/debugging.md b/guide/debugging.md
index 5c46aef3..59d048b1 100644
--- a/guide/debugging.md
+++ b/guide/debugging.md
@@ -27,8 +27,8 @@ ndb npm run test
:::
## VS Code
-
-The [official VS Code](https://vitest.dev/vscode) extension supports debugging tests via "Debug Tests" button. However Vitest also exposes tools to define a custom configuration.
+
+[官方 VS Code](https://vitest.dev/vscode) 扩展支持通过 "Debug Tests" 按钮来调试测试。不过,Vitest 也提供了自定义配置。
在 VSCode 中快速调试测试的方法是通过 `JavaScript Debug Terminal` 。打开一个新的 `JavaScript Debug Terminal` 并直接运行 `npm run test` 或 `vitest` 。*这适用于在 Node 中运行的任何代码,因此将适用于大多数 JS 测试框架*。
@@ -59,10 +59,10 @@ The [official VS Code](https://vitest.dev/vscode) extension supports debugging t
然后在调试选项卡中确保选择 'Debug Current Test File',然后你可以打开要调试的测试文件并按 F5 开始调试。
### 浏览器模式 {#browser-mode}
-
-The simplest way to debug browser tests is to use the [official VS Code](https://vitest.dev/vscode) extension.
-However you can also pass `--inspect` or `--inspect-brk` in CLI or define it in your Vitest configuration:
+调试浏览器测试最简单的方法是使用 [官方 VS Code](https://vitest.dev/vscode) 扩展。
+
+不过,你也可以在 CLI 中传递 `--inspect` 或 `--inspect-brk`,或在你的 Vitest 配置文件中定义它们:
::: code-group
```bash [CLI]
diff --git a/guide/environment.md b/guide/environment.md
index ef8548e2..faa6b331 100644
--- a/guide/environment.md
+++ b/guide/environment.md
@@ -14,7 +14,7 @@ Vitest 提供 [`environment`](/config/environment) 选项以在特定环境中
- `edge-runtime` 模拟 Vercel 的 [edge-runtime](https://edge-runtime.vercel.app/),使用 [`@edge-runtime/vm`](https://npmx.dev/package/@edge-runtime/vm) 包
::: info
-当使用 `jsdom` 或 `happy-dom` 环境时,Vitest 在导入 [CSS](https://vitejs.dev/guide/features.html#css) 和 [资源文件](https://vitejs.dev/guide/features.html#static-assets) 时遵循与 Vite 相同的规则。如果在导入外部依赖时出现 `unknown extension .css` 错误,则需要通过将所有相关包添加到 [`server.deps.inline`](/config/server#inline) 中,手动内联整个导入链。例如,在以下导入链中:`源代码 -> package-1 -> package-2 -> package-3`,如果错误发生在 `package-3`,你需要将这三个包都添加到 `server.deps.inline` 中。
+当使用 `jsdom` 或 `happy-dom` 环境时,Vitest 在导入 [CSS](https://cn.vitejs.dev/guide/features.html#css) 和 [资源文件](https://cn.vitejs.dev/guide/features.html#static-assets) 时遵循与 Vite 相同的规则。如果在导入外部依赖时出现 `unknown extension .css` 错误,则需要通过将所有相关包添加到 [`server.deps.inline`](/config/server#inline) 中,手动内联整个导入链。例如,在以下导入链中:`源代码 -> package-1 -> package-2 -> package-3`,如果错误发生在 `package-3`,你需要将这三个包都添加到 `server.deps.inline` 中。
外部依赖中的 CSS 和资源文件的 `require` 调用会自动解析。
:::
diff --git a/guide/features.md b/guide/features.md
index 136affe0..67548deb 100644
--- a/guide/features.md
+++ b/guide/features.md
@@ -278,7 +278,7 @@ vitest --merge-reports --reporter=junit --coverage
## 环境变量 {#environment-variables}
-Vitest 只从 `.env` 文件中自动加载以 `VITE_` 为前缀的环境变量,以保持与前端相关测试的兼容性,并遵守 [Vite 的既定惯例](https://vitejs.dev/guide/env-and-mode.html#env-files)。要从 `.env` 文件加载所有环境变量,可以使用从 `vite` 导入的 `loadEnv` 方法:
+Vitest 只从 `.env` 文件中自动加载以 `VITE_` 为前缀的环境变量,以保持与前端相关测试的兼容性,并遵守 [Vite 的既定惯例](https://cn.vitejs.dev/guide/env-and-mode.html#env-files)。要从 `.env` 文件加载所有环境变量,可以使用从 `vite` 导入的 `loadEnv` 方法:
```ts [vitest.config.ts]
import { loadEnv } from 'vite'
diff --git a/guide/in-source.md b/guide/in-source.md
index 0add6597..bdba13fc 100644
--- a/guide/in-source.md
+++ b/guide/in-source.md
@@ -154,9 +154,9 @@ module.exports = {
```
完整的示例请参考 [`examples/in-source-test`](https://github.com/vitest-dev/vitest/tree/main/examples/in-source-test)。
-
+
::: warning
-There is a limitation when using [assertion functions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) such as `assert` in in-source tests. See [`assert`](/api/assert#in-source-testing) for details and workarounds.
+在使用 [断言函数](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions)(例如 `assert`)进行源码内联测试时存在一个限制。详情请参阅 [`assert`](/api/assert#in-source-testing)。
:::
## 说明 {#notes}
diff --git a/guide/mocking/functions.md b/guide/mocking/functions.md
index d4670fe2..6d0c75ee 100644
--- a/guide/mocking/functions.md
+++ b/guide/mocking/functions.md
@@ -7,9 +7,9 @@
如果你需要传递自定义函数实现作为参数或创建新的模拟实体,你可以使用 [`vi.fn()`](/api/vi#vi-fn) 来创建一个模拟函数。
`vi.spyOn` 和 `vi.fn` 都共享相同的方法。
-
+
::: tip
-If you need a mock to return different values depending on the arguments it receives, [`vi.when()`](/api/vi#vi-when) lets you define argument-specific behaviors without writing your own `if/else` logic. See the [Conditional Mocking](/guide/recipes/conditional-mocking) recipe for details.
+如果你需要一个模拟函数根据接收到的参数返回不同的值,[`vi.when()`](/api/vi#vi-when) 允许你定义特定于参数的行为,而无需编写自己的 `if/else` 逻辑。详情请参阅 [条件模拟](/guide/recipes/conditional-mocking) 技巧指南。
:::
## 示例 {#example}
diff --git a/guide/profiling-test-performance.md b/guide/profiling-test-performance.md
index 77641308..018b0e0f 100644
--- a/guide/profiling-test-performance.md
+++ b/guide/profiling-test-performance.md
@@ -158,7 +158,7 @@ vitest --experimental.importDurations.print
### 使用特定入口 {#use-specific-entry-points}
-许多库提供了多个入口点。导入主入口点(通常是 [桶文件](https://vitejs.dev/guide/performance.html#avoid-barrel-files))可能会引入比你所需多得多的代码。
+许多库提供了多个入口点。导入主入口点(通常是 [桶文件](https://cn.vitejs.dev/guide/performance.html#avoid-barrel-files))可能会引入比你所需多得多的代码。
例如,`date-fns` 从其主入口点重新导出了数百个函数。与其从顶层模块导入,不如直接从特定入口导入:
diff --git a/guide/reporters.md b/guide/reporters.md
index ab2a21b6..4dae2842 100644
--- a/guide/reporters.md
+++ b/guide/reporters.md
@@ -518,8 +518,8 @@ export default defineConfig({
### HTML 报告器 {#html-reporter}
生成 HTML 文件,通过交互式 [GUI](/guide/ui) 查看测试结果。文件生成后,Vitest 将保持本地开发服务器运行,并提供一个链接,以便在浏览器中查看报告。
-
-The report artifact root can be specified using the reporter's `outputDir` option. The report entry is written to `/index.html` and the UI assets files live under `/ui/`. By default `outputDir` is `.vitest`, the shared Vitest artifact directory, so attachments (`.vitest/attachments`) and coverage (`.vitest/coverage`) are reused without being copied.
+
+报告产物的根目录可以通过报告器的 `outputDir` 选项指定。报告入口文件会被写入 `/index.html`,UI 资源文件则位于 `/ui/` 下。默认情况下,`outputDir` 是 `.vitest`,即 Vitest 共享的产物目录,因此附件(`.vitest/attachments`)和覆盖率(`.vitest/coverage`)会被直接复用,而是复制一份。
:::code-group
@@ -536,8 +536,8 @@ export default defineConfig({
```
:::
-
-Set `singleFile` to generate a self-contained HTML report:
+
+设置 `singleFile` 以生成自包含的 HTML 报告:
```ts [vitest.config.ts]
export default defineConfig({
@@ -549,15 +549,15 @@ export default defineConfig({
})
```
-When `singleFile` is enabled, Vitest inlines the UI assets, metadata, and test attachments into a single self-contained `index.html`. This makes the report easy to share, upload, or download as one artifact instead of preserving the whole `html` output directory.
+当启用 `singleFile` 时,Vitest 会将 UI 资源、元数据和测试附件内联到一个单一的自包含 `index.html` 文件中。这样一来,报告就更易于分享、上传或下载,而不必保留整个 `html` 输出目录。
::: warning
-`singleFile` has two caveats:
+`singleFile` 有两个注意事项:
-- The file can grow very large because everything is embedded inline — slow to open, memory-hungry, and possibly over the size limits of artifact viewers or static hosts.
-- Coverage HTML reports are not inlined yet and remain as separate files.
+- 由于所有内容都会以内联方式嵌入,文件体积可能会变得非常大,打开速度较慢、内存占用较高,也可能超出某些产物查看器或静态托管服务的大小限制。
+- 覆盖率 HTML 报告目前不会被内联,仍作为独立文件形式保留。
-Prefer the default multi-file report when the suite has many or large attachments, or when you need coverage included in the bundle.
+当测试套件包含许多或大型附件,或者你需要将覆盖率包含在包中时,建议优先使用默认的多文件报告。
:::
::: tip
diff --git a/guide/test-context.md b/guide/test-context.md
index 1cde36b4..9f979b89 100644
--- a/guide/test-context.md
+++ b/guide/test-context.md
@@ -116,10 +116,10 @@ it('stop request when test times out', async ({ signal }) => {
await fetch('/resource', { signal })
}, 2000)
```
-
+
### `bench` 5.0.0 {#bench}
-The `bench` fixture lets you define and run benchmarks inside regular tests. You can measure throughput, compare implementations, and assert relative performance:
+`bench` 夹具可让你在常规测试中定义并运行基准测试。你可以用它衡量吞吐量、比较不同实现,并对相对性能进行断言:
```ts
import { expect, test } from 'vitest'
@@ -138,7 +138,7 @@ test('compare parsers', async ({ bench }) => {
})
```
-See the [Benchmarks guide](/guide/benchmarking) for full documentation on comparisons, baselines, and assertion matchers.
+有关对比、基线和断言匹配器的完整说明,请参阅 [基线测试指南](/guide/benchmarking)。
### `onTestFailed`
@@ -889,7 +889,7 @@ test.afterAll(async ({ database }) => {
})
```
-::: warning IMPORTANT
+::: warning 重要
Suite-level hooks (`beforeAll`, `afterAll`, `aroundAll`) **must be called on the `test` object returned from `test.extend()`** to have access to the extended fixtures. Using the global `beforeAll`/`afterAll`/`aroundAll` functions will not have access to your custom fixtures:
```ts
diff --git a/guide/test-tags.md b/guide/test-tags.md
index df3e312c..ddf9544f 100644
--- a/guide/test-tags.md
+++ b/guide/test-tags.md
@@ -6,25 +6,25 @@ outline: deep
# 测试标签 4.1.0 {#test-tags}
允许你在测试用例上添加 [`标签`](/config/tags),在必要时可以使用标签进行过滤测试,或覆盖测试配置。
-
-## Why tags
-Tags become useful once a suite has groups of tests that share runner options, like a longer timeout for database queries or retries for integration tests on CI. Repeating those options on every relevant test by hand is brittle, and the categories often don't line up with file paths anyway, so splitting them out by file isn't an option. Flaky tests in particular tend to accumulate wherever the bugs landed, not in a `flaky/` folder.
+## 为什么使用测试标签 {#why-tags}
-A tag captures that kind of category: the definition holds the shared options, and any test marked with the tag inherits them. Those tag names can also be combined into expressions: `--tags-filter='db && !flaky'` runs database tests that aren't marked flaky. [`TestRunner.matchesTags`](#checking-tags-filter-at-runtime) exposes the same expression at runtime, useful when `globalSetup` does expensive work that should be skipped if no tagged tests are scheduled.
+测试标签适用于共享运行配置的测试套件中使用。例如为数据库查询设置更长的超时时间,或者在 CI 上为集成测试设置重试机制。手动在相关测试上重复设置配置既繁琐又容易出错,并且这些类测试和文件路径并不是一一对应的,因此按文件拆分也不是一个可行的方案。对于那些特别不稳定的测试,它们往往出现在真实有 bug 的地方,而不是集中在 `flaky` 文件里面。
-## When to reach for tags
+测试标签可以捕获这种类别:定义包含了共享的选项,和标记了该测试标签的测试都会继承这些选项。这些标签名称还可以组合成表达式:`--tags-filter='db && !flaky'` 会运行那些标记为数据库测试但未被标记为不稳定的测试。[`TestRunner.matchesTags`](#checking-tags-filter-at-runtime) 可在运行时使用同样的标签表达式,适用于 `globalSetup` 中包含开销较大的操作,且在没有任何带标签的测试需要执行时,需要跳过这些操作的场景。
-| If you want to… | Use |
+## 何时使用标签 {#when-to-reach-for-tags}
+
+| 应用场景 | 使用 |
| --- | --- |
-| Apply timeout/retry to a *category* of tests | **Tags** |
-| Mark cross-cutting categories (`flaky`, `slow`, `frontend`) scattered across many files | **Tags** |
-| Conditionally run expensive setup based on what's filtered | **Tags** + [`matchesTags`](#checking-tags-filter-at-runtime) |
-| Run a subset by test name match | [`-t` / `testNamePattern`](/config/testnamepattern) |
-| Run a subset by file path | `--include` / `--exclude` |
-| Run different files with different *runner settings* (isolation, pool, environment) | [Test Projects](/guide/projects) |
+| 对 *一类* 测试设置超时/重试 | **标签** |
+| 标记分散在许多文件中的横向分类(`flaky`、`slow`、`frontend`) | **标签** |
+| 根据过滤条件有条件地运行开销大的设置 | **标签** + [`matchesTags`](#checking-tags-filter-at-runtime) |
+| 通过测试名称匹配来运行子集 | [`-t` / `testNamePattern`](/config/testnamepattern) |
+| 通过文件路径来运行子集 | `--include`/`--exclude` |
+| 使用不同的 *运行器设置*(测试隔离、运行池、测试环境)运行不同的文件 | [测试项目](/guide/projects) |
-You can combine projects and tags. A test that sits in a `Sequential` project can also carry a `flaky` tag, and Vitest applies both.
+可以将测试项目(project)与测试标签结合使用。例如在 `Sequential` 项目中筛选带有 `flaky` 标签的测试。
## 定义标签 {#defining-tags}
@@ -120,7 +120,7 @@ flaky: Flaky CI tests.
}
```
-### Resolving option conflicts
+### 解决配置冲突 {#resolving-option-conflicts}
如果多个标签具有相同配置项且应用于同一个测试时,将按从上至下的顺序解析,或按优先级排序解析(数值越低,优先级越高)。未定义优先级的标签会先合并,随后被优先级更高的标签覆盖。
@@ -337,10 +337,9 @@ beforeAll(async () => {
```
该方法接收一个标签数组作为参数,如果当前 `--tags-filter` 会包含带有这些标签的测试,则返回 `true`。如果未启用标签过滤器,则始终返回 `true`。
-The method accepts an array of tags and returns `true` if the current `--tags-filter` would include a test with those tags. If no tags filter is active, it always returns `true`.
-
-## See also
-- [Per-File Isolation Settings](/guide/recipes/disable-isolation) and [Parallel and Sequential Test Files](/guide/recipes/parallel-sequential) use projects to partition tests by file. Reach for projects when categories need different runner settings rather than different timeouts or retries.
-- [Test Filtering](/guide/filtering) covers `-t`, `--include`, and the rest of the CLI filters.
-- [`tags`](/config/tags) and [`strictTags`](/config/stricttags) configuration reference.
+## 另请参阅 {#see-also}
+
+- [按文件隔离](/guide/recipes/disable-isolation) 和 [并行与顺序测试文件](/guide/recipes/parallel-sequential) 都是通过测试项目参数按文件划分测试。对于需要使用不同运行器配置,而不是仅调整不同超时时间或重试次数的测试分类,适合使用测试项目参数。
+- [测试过滤](/guide/filtering) 覆盖了 `-t`、`--include` 以及其他 CLI 过滤器。
+- 参考 [`tags`](/config/tags) 和 [`strictTags`](/config/stricttags) 配置。
diff --git a/guide/ui.md b/guide/ui.md
index 62281ade..818e2427 100644
--- a/guide/ui.md
+++ b/guide/ui.md
@@ -48,16 +48,16 @@ export default defineConfig({
:::
::: tip
-要预览你的 HTML 报告,可以使用 [vite preview](https://vitejs.dev/guide/cli.html#vite-preview) 命令:
+要预览你的 HTML 报告,可以使用 [vite preview](https://cn.vitejs.dev/guide/cli.html#vite-preview) 命令:
```sh
npx vite preview --outDir .vitest
```
-
-You can configure the output location with the HTML reporter's `outputDir` option. It points to the report artifact root, and the report entry is written to `/index.html`. The default value is `.vitest`, the shared Vitest artifact directory.
+
+你可以通过 HTML 报告器的 `outputDir` 选项来配置输出位置。它指向报告产物的根目录,报告入口文件会被写入 `/index.html`。默认值是 `.vitest`,即 Vitest 共享的产物目录。
:::
-If you need a portable report that can be opened or shared as one file, see [`singleFile`](/guide/reporters#html-reporter) in the HTML reporter documentation.
+如果你需要一个便于打开或分享的单文件报告,请参阅 HTML 报告器文档中的 [`singleFile`](/guide/reporters#html-reporter)。
::: tip
要在持续集成环境,例如 GitHub Actions 中查看 HTML 报告,请将输出目录作为产物上传:
@@ -74,8 +74,8 @@ If you need a portable report that can be opened or shared as one file, see [`si
```
这会在任务摘要中添加一个链接。点击该链接即可在浏览器中直接通过 [Vitest Viewer](https://viewer.vitest.dev/) 查看报告。你也可以手动下载产物并解压,然后按照前文所述在本地运行 `vite preview` 命令。
-
-When you use `singleFile: true`, you can upload the report as a single file and it will become viewable directly GitHub artifacts with `archive: false` option:
+
+当你使用 `singleFile: true` 时,报告作为单个文件进行上传,配合 `archive: false` 选项,报告直接可以在 GitHub Artifacts 中查看:
```yaml
- uses: actions/upload-artifact@v7
diff --git a/guide/using-plugins.md b/guide/using-plugins.md
index bcc15ba8..759f02f8 100644
--- a/guide/using-plugins.md
+++ b/guide/using-plugins.md
@@ -6,4 +6,4 @@ title: 使用插件 | 指南
Vitest 可以使用插件进行扩展,类似于 Vite 插件的工作方式。这允许你使用相同的 API 和 Vite 插件概念来增强和自定义 Vitest 的功能。
-有关如何编写插件的详细指导,你可以参考 [Vite 插件文档](https://vitejs.dev/guide/api-plugin).
+有关如何编写插件的详细指导,你可以参考 [Vite 插件文档](https://cn.vitejs.dev/guide/api-plugin).
diff --git a/package.json b/package.json
index ef9a5423..a9990c1c 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "docs-cn",
"type": "module",
- "version": "5.0.0-beta.2",
+ "version": "5.0.0-beta.5",
"private": true,
"packageManager": "pnpm@9.7.1",
"scripts": {