Skip to content
2 changes: 1 addition & 1 deletion api/advanced/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ vitest.config.project.push('my-project-name')
:::

::: tip 引用当前配置
内联配置默认继承根配置。如果想改为继承特定的配置文件,请将 `extends` 属性设为该文件的路径。其他所有属性都会与用户定义的配置合并。
内联配置默认继承顶级配置。如果想改为继承特定的配置文件,请将 `extends` 属性设为该文件的路径。其他所有属性都会与用户定义的配置合并。

项目的 `configFile` 可以在 Vite 的配置中访问:`project.vite.config.configFile`。

Expand Down
4 changes: 2 additions & 2 deletions api/advanced/vitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Since Vitest 5, this property is always `'test'`.

## config

这是根配置(也叫全局配置)。如果你在配置中定义了多个项目,这些项目都会将这个配置视作它们的 `globalConfig` 并进行继承或引用。
这是顶级配置(也叫全局配置)。如果你在配置中定义了多个项目,这些项目都会将这个配置视作它们的 `globalConfig` 并进行继承或引用。

::: warning
这是 Vitest 配置,它不扩展 _Vite_ 配置。它仅包含从 `test` 属性解析的值。
Expand Down Expand Up @@ -63,7 +63,7 @@ Vitest 会保证这个数组里至少有一个项目可用。如果用户在命
function getRootProject(): TestProject
```

该方法会返回根测试项目。一般情况下,根项目并不会实际执行测试,也不会被加入到 `vitest.projects` 列表中,除非用户在配置中主动包含了根配置,或者没有定义任何独立的测试项目。
该方法会返回根测试项目。一般情况下,根项目并不会实际执行测试,也不会被加入到 `vitest.projects` 列表中,除非用户在配置中主动包含了顶级配置,或者没有定义任何独立的测试项目。

根项目的主要目标是设置全局配置。实际上,`rootProject.config` 直接引用 `rootProject.globalConfig` 和 `vitest.config`:

Expand Down
2 changes: 1 addition & 1 deletion blog/vitest-3-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Vitest 3.2 专注于改进浏览器模式和 TypeScript 支持。此版本还包

## `workspace` 已弃用 {#workspace-is-deprecated}

为了简化配置,团队决定弃用单独的 `vitest.workspace` 文件,推荐仅在根配置中使用 `projects` 选项。这也简化了全局选项的配置方式(因为当你没有根配置时,不需要再猜测如何添加报告器)。
为了简化配置,团队决定弃用单独的 `vitest.workspace` 文件,推荐仅在顶级配置中使用 `projects` 选项。这也简化了全局选项的配置方式(因为当你没有顶级配置时,不需要再猜测如何添加报告器)。

我们还决定弃用 `workspace` 这个名称,因为它与 PNPM 等工具通过该选项提供 monorepo 支持功能存在冲突。Vitest 不会为这些项目分配独立的 `工作目录(CWD)`,而是将其视为子 Vitest 实例。这也为我们提供了更多空间,以便在不破坏其他功能的情况下为 monorepo 提供更好的解决方案。

Expand Down
2 changes: 1 addition & 1 deletion config/attachmentsdir.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ outline: deep

指定通过 [`context.annotate`](/guide/test-context#annotate) 创建的文件附件的存储目录。

Vitest 会相对于根配置解析此路径。使用 [`projects`](/guide/projects) 时,所有项目共用同一个 `attachmentsDir`,无法为每个项目单独配置。
Vitest 会相对于顶级配置解析此路径。使用 [`projects`](/guide/projects) 时,所有项目共用同一个 `attachmentsDir`,无法为每个项目单独配置。
4 changes: 2 additions & 2 deletions config/browser/instances.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ outline: deep
你可以指定大多数 [项目选项](/config/)(未标记<CRoot />图标的)以及部分 `browser` 选项,如 `browser.testerHtmlPath`。

::: warning
每个浏览器配置都会继承根配置的选项
每个浏览器配置都会继承顶级配置的选项

```ts{3,9} [vitest.config.ts]
export default defineConfig({
Expand All @@ -26,7 +26,7 @@ export default defineConfig({
{
// 将同时包含 "root" 和 "browser" 两个全局初始化文件
setupFile: ['./browser-setup-file.js'],
// 隐式继承根配置的"testerHtmlPath" // [!code warning]
// 隐式继承顶级配置的"testerHtmlPath" // [!code warning]
// testerHtmlPath: './custom-path.html', // [!code warning]
},
],
Expand Down
2 changes: 1 addition & 1 deletion config/experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ vitest --experimental.importDurations.failOnDanger

控制 Vitest 是否使用 Vite 的 [模块运行器](https://cn.vite.dev/guide/api-environment-runtimes#modulerunner) 执行代码,或回退至原生 `import` 方式。

如果在根配置中定义此选项,所有 [项目](/guide/projects) 将自动继承该设置。
如果在顶级配置中定义此选项,所有 [项目](/guide/projects) 将自动继承该设置。

当测试运行环境与代码执行环境相同时(例如服务端后端或简单脚本),可考虑禁用模块运行器。但对于 `jsdom`/`happy-dom` 测试,我们仍建议使用 Vite 模块运行器或在 [浏览器模式](/guide/browser/) 中运行,因为这样无需添加额外的配置。

Expand Down
2 changes: 1 addition & 1 deletion config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ export default defineConfig(configEnv => mergeConfig(

## 配置选项 {#config-options}

在 [项目](/guide/projects) 配置中不支持的配置选项旁边会显示 <CRoot /> 图标。这意味着它们只能在 Vitest 根配置文件中进行设置
在 [项目](/guide/projects) 配置中不支持的配置选项旁边会显示 <CRoot /> 图标。这意味着它们只能在 Vitest 顶级配置文件中进行设置
2 changes: 1 addition & 1 deletion config/sequence.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Vitest 通常使用缓存对测试进行排序,使耗时较长的测试优先

是否随机排列测试文件。请注意,启用此选项后,耗时较长的测试将无法优先开始执行。

由于所有 [项目](/guide/projects) 共享同一套文件排序,因此此选项只能由根配置决定。各个项目仍可通过 [`sequence.shuffle.tests`](#sequence-shuffle-tests) 随机排列自身的测试用例。
由于所有 [项目](/guide/projects) 共享同一套文件排序,因此此选项只能由顶级配置决定。各个项目仍可通过 [`sequence.shuffle.tests`](#sequence-shuffle-tests) 随机排列自身的测试用例。

### sequence.shuffle.tests {#sequence-shuffle-tests}

Expand Down
1 change: 1 addition & 0 deletions guide/advanced/benchmark-provider.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Custom Benchmark Provider <Version type="experimental">5.0.0</Version> <Badge type="danger">advanced</Badge> {#custom-benchmark-provider}

<!-- TODO: translation -->
::: warning
This is an advanced, experimental API. If you only need to run benchmarks with Vitest's built-in provider, read the [Benchmarking](/guide/benchmarking) guide instead.
:::
Expand Down
51 changes: 25 additions & 26 deletions guide/advanced/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Advanced API
title: 高级 API
---

# 快速起步 <Badge type="danger">advanced</Badge> {#getting-started}
Expand Down Expand Up @@ -90,8 +90,8 @@ function resolveConfig(
```

此方法使用自定义参数解析配置,而不会创建 Vite 服务器。如果未提供任何参数,root 将设为 process.cwd()。
<!-- TODO: translation -->
It returns the resolved Vite config. The fully resolved Vitest config, including every project, lives on its `test` property.

该方法返回解析后的 Vite 配置。其 `test` 属性包含完整解析后的 Vitest 配置,其中包括所有项目。

```ts
import { resolveConfig } from 'vitest/node'
Expand Down Expand Up @@ -121,34 +121,33 @@ viteConfig.test.pool // 'threads'
另外请注意,`viteConfig.test` 不会被完全解析。如果你需要 Vitest 配置,请使用 `vitestConfig` 代替。
:::

<!-- TODO: translation -->
## Project Configuration Resolution
## 解析项目配置 {#project-configuration-resolution}

This section describes how the arguments of `startVitest`, `createVitest`, and `resolveConfig` interact with [test projects](/guide/projects). Without projects, all resolved options apply to the single root project and none of this matters.
本节说明 `startVitest``createVitest` `resolveConfig` 的参数如何影响 [测试项目](/guide/projects)。在没有项目配置的情况下,解析后的所有选项都会应用于唯一的顶级项目,因此无须考虑以下规则。

The root configuration is resolved from three inputs, in ascending priority:
顶级配置由以下三类输入解析而成,优先级从低到高依次为:

1. the root config file
2. `viteOverrides`, merged on top of the config file values
3. CLI options (`options`), applied on top of everything else
1. 顶级配置文件
2. `viteOverrides`,其内容会覆盖配置文件中的对应值
3. CLI 选项(`options`),其优先级高于其他所有配置

Every project then resolves its own Vite config independently:
每个项目随后独立解析其自身的 Vite 配置:

- A project referenced as a config file or a directory resolves only its own file. It does not inherit any options from the root configuration.
- An inline project inherits the root configuration by default (see [`extends`](/guide/projects#configuration)): the root config file is re-executed for the project, `viteOverrides` are merged on top of it, and the project's own options are merged last. Inheritance works even when there is no root config file, because `viteOverrides` are part of the effective root configuration.
- With `extends: false`, an inline project resolves only its own options. With `extends: './path'`, the referenced file is re-executed instead of the root config file, and `viteOverrides` are not merged.
- 通过配置文件或目录引用的项目只解析自身的配置文件,不会继承顶级配置中的任何选项。
- 默认情况下,内联项目会继承顶级配置(参阅 [`extends`](/guide/projects#configuration))。会为该项目重新执行顶级配置文件,然后合并 `viteOverrides`,最后再合并项目自身的选项。即使不存在顶级配置文件,继承仍然有效,因为 `viteOverrides` 也属于最终生效的顶级配置。
- 设置 `extends: false` 后,内联项目只解析自身的选项。设置 `extends: './path'` 后,会重新执行所引用的文件,而不是顶级配置文件,并且不会合并 `viteOverrides`

A few options are excluded from inheritance:
以下选项不会按常规规则继承:

- `plugins` from `viteOverrides` are never inherited. A config file is re-executed for every project, which creates fresh plugin instances, but plugin instances passed in `viteOverrides` belong to the root Vite server and cannot be shared with project servers.
- `test.browser` and `test.tagsFilter` from `viteOverrides` are never inherited: `browser` describes the instances of a single project, and `tagsFilter` applies to the whole run.
- `name` and `projects` are never inherited; the root `globalSetup` is not inherited because it already runs once per test run.
- The project's own `tags` always replace the `tags` array merged from an extended config instead of being concatenated with it, so the same tag names can be redefined.
- 永远不会继承 `viteOverrides` 中的 `plugins`。配置文件会为每个项目重新执行,从而创建新的插件实例;但通过 `viteOverrides` 传入的插件实例属于顶级 Vite 服务器,无法与项目服务器共享。
- 永远不会继承 `viteOverrides` 中的 `test.browser` `test.tagsFilter`。`browser` 描述单个项目的浏览器实例,而 `tagsFilter` 作用于整次测试运行。
- 永远不会继承 `name` `projects`。顶级配置中的 `globalSetup` 也不会被继承,因为它已经会在每次测试运行时执行一次。
- 项目自身的 `tags` 始终会替换从被继承配置中合并而来的 `tags` 数组,而不是与其拼接,因此可以重新定义同名标签。

Independently of `extends`, two groups of options reach every project:
独立与 `extends` 机制,以下两组选项都会应用于每个项目:

- A fixed subset of CLI options that configure how tests run (`--testTimeout`, `--retry`, `--pool`, and similar) is applied to every project at the highest priority, mirroring the root resolution.
- Run-level options only make sense for the test run as a whole: every project receives the root's resolved `coverage`, `attachmentsDir`, and `mergeReportsLabel` values.
- 一组用于控制测试运行方式的固定 CLI 选项,例如 `--testTimeout``--retry``--pool`,会以最高优先级应用于每个项目,与顶级配置的解析方式一致。
- 运行级选项只对整次测试运行有意义,因此每个项目都会使用顶级配置解析后的 `coverage``attachmentsDir` `mergeReportsLabel` 值。

## parseCLI

Expand Down Expand Up @@ -182,7 +181,7 @@ result.filter
function createCLI(options?: CliParseOptions): CAC
```

Creates the Vitest command-line interface: a [`cac`](https://github.com/cacjs/cac) instance with all of Vitest's commands and options registered. [`parseCLI`](#parsecli) is built on top of it; use `createCLI` directly if you need the raw parser.
创建 Vitest 命令行界面,返回一个注册了 Vitest 全部命令和选项的 [`cac`](https://github.com/cacjs/cac) 实例。[`parseCLI`](#parsecli) 基于该实例实现;如果需要直接使用原始解析器,请调用 `createCLI`

```ts
import { createCLI } from 'vitest/node'
Expand All @@ -202,9 +201,9 @@ class PluginHarness {
}
```

A container that Vitest passes to its internal plugins while the config is being resolved, before a [`Vitest`](/api/advanced/vitest) instance exists. It holds the [`Logger`](#logger), the package installer and the resolved version, and exposes the `Vitest` instance via `getVitest()` once it has been created (calling it earlier throws).
这是一个容器,在配置解析期间、[`Vitest`](/api/advanced/vitest) 会将此容器传递给内部插件。它保存 [`Logger`](#logger)、包安装器和解析后的版本,并在 `Vitest` 实例创建后通过 `getVitest()` 提供该实例(如果提前调用此方法,则会抛出错误)。

This is an advanced, plugin-facing API. You rarely construct one directly, but you can pass a shared instance to [`resolveConfig`](#resolveconfig) to reuse a logger and package installer.
这是一个面向插件的高级 API。通常有很少机会直接实现它。但你可以向 [`resolveConfig`](#resolveconfig) 传递一个共享实例,以复用日志记录器和包安装器。

## Logger

Expand All @@ -217,7 +216,7 @@ class Logger {
}
```

Vitest's terminal logger, exposed as [`vitest.logger`](/api/advanced/vitest). It handles formatted output, the error summary, the run banner and screen clearing. Construct one with custom `stdout`/`stderr` streams to capture or redirect Vitest's output when running it programmatically.
Vitest 的终端日志记录器,通过 [`vitest.logger`](/api/advanced/vitest) 暴露。它负责格式化输出、错误摘要、运行横幅和终端清屏。以编程方式运行 Vitest 时,可以使用自定义的 `stdout`/`stderr` 流创建 `Logger`,以捕获或重定向 Vitest 的输出。

```ts
import { Logger } from 'vitest/node'
Expand Down
2 changes: 1 addition & 1 deletion guide/advanced/pool.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Custom Pool <Badge type="danger">advanced</Badge> {#custom-pool}

<!-- TODO: translation -->
::: warning
This is an advanced, experimental and very low-level API. If you just want to [run tests](/guide/), you probably don't need this. It is primarily used by library authors.
:::
Expand Down
2 changes: 1 addition & 1 deletion guide/browser/multiple-setups.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test('ratio works', () => {

## 过滤 {#filtering}

你可以使用 [`--project` 参数](/guide/cli#project) 来过滤要运行的项目。如果未手动分配项目名称,Vitest 会自动将浏览器名称作为项目名称。如果根配置已经有一个名称,Vitest 会将它们合并:`custom` -> `custom (browser)`。
你可以使用 [`--project` 参数](/guide/cli#project) 来过滤要运行的项目。如果未手动分配项目名称,Vitest 会自动将浏览器名称作为项目名称。如果顶级配置已经有一个名称,Vitest 会将它们合并:`custom` -> `custom (browser)`。

```shell
$ vitest --project=chromium
Expand Down
2 changes: 1 addition & 1 deletion guide/browser/visual-regression-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: 可视化回归测试
outline: [2, 3]
---

<!-- TODO: translation -->
<script setup>
import MoonPhase from '../../.vitepress/components/MoonPhase.vue'
</script>
Expand Down
Loading
Loading