Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .vitepress/components/FeaturesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ListItem from './ListItem.vue'
<ul class="features-list" dir="auto">
<ListItem>
<a target="_blank" href="https://vitejs.dev" rel="noopener noreferrer">Vite</a>
<a target="_blank" href="https://cn.vitejs.dev" rel="noopener noreferrer">Vite</a>
通用的配置、转换器、解析器和插件。
</ListItem>
<ListItem>使用与你的应用相同的设置来运行测试!</ListItem>
Expand Down
2 changes: 1 addition & 1 deletion .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ export default ({ mode }: { mode: string }) => {
],
},
{
text: '测试',
text: '测试标签',
link: '/guide/test-tags',
},
{
Expand Down
2 changes: 1 addition & 1 deletion api/advanced/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function onTestRunStart(
): Awaitable<void>
```

当新的测试运行开始时调用此方法。它接收计划运行的 [测试规范](/api/advanced/test-specification) 数组。此数组是只读的,仅用于信息目的。
当新的测试运行开始时调用此方法。它接收计划运行的 [TestSpecification](/api/advanced/test-specification) 数组。此数组是只读的,仅用于信息目的。

如果 Vitest 没有找到任何要运行的测试文件,此事件将以空数组调用,然后 [`onTestRunEnd`](#ontestrunend) 将立即被调用。

Expand Down
6 changes: 3 additions & 3 deletions api/advanced/test-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,19 @@ function artifacts(): ReadonlyArray<TestArtifact>
```

通过 `recordArtifact` API,在测试执行过程中记录的 [测试产物](/api/advanced/artifacts)。
<!-- TODO: translation -->

## toTestSpecification <Version>4.1.0</Version> {#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 <Version>5.0.0</Version> {#logs}

```ts
function logs(): ReadonlyArray<UserConsoleLog>
```

Console logs recorded during the test execution.
测试执行期间记录的 console 日志。
49 changes: 25 additions & 24 deletions api/advanced/test-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ if (task.type === 'module') {
'example.test.ts' // ✅
'project\\example.test.ts' // ❌
```
<!-- TODO: translation -->

## viteEnvironment <Version>4.1.0</Version> {#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
Expand Down Expand Up @@ -88,51 +88,52 @@ function diagnostic(): ModuleDiagnostic
```ts
interface ModuleDiagnostic {
/**
* 导入和初始化环境所需的时间
* 导入和初始化环境所需的时间
*/
readonly environmentSetupDuration: number
/**
* Vitest 设置测试运行环境(运行器、模拟等)所需的时间
* Vitest 设置测试运行环境(运行器、模拟等)所需的时间
*/
readonly prepareDuration: number
/**
* 导入测试模块所需的时间
* 这包括导入模块中的所有内容以及执行套件回调函数
* 导入测试模块所需的时间
* 这包括导入模块中的所有内容以及执行套件回调函数
*/
readonly collectDuration: number
/**
* 导入设置模块所需的时间
* 导入设置模块所需的时间
*/
readonly setupDuration: number
/**
* 模块中所有测试和钩子函数的累计持续时间。
*/
readonly duration: number
/**
* 模块使用的内存量(以字节为单位)
* 此值仅在使用 `logHeapUsage` 参数执行测试时才可用
* 模块使用的内存量(以字节为单位)
* 此值仅在使用 `logHeapUsage` 参数执行测试时才可用
*/
readonly heap: number | undefined
/**
* Vitest处理的每个非外部化依赖项的导入时间
* Vitest处理的每个非外部化依赖项的导入时间
*/
readonly importDurations: Record<string, ImportDuration>
/**
* 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
}
Expand All @@ -146,14 +147,14 @@ interface ImportDuration {
totalTime: number
}
```
<!-- TODO: translation -->

## logs <Version>5.0.0</Version> {#logs}

```ts
function logs(): ReadonlyArray<UserConsoleLog>
```

Console logs recorded on top level of the module during test collection.For example:
测试收集期间在模块顶层记录的 console 日志。例如:

```ts
console.log('included') // [!code highlight]
Expand Down
2 changes: 1 addition & 1 deletion api/advanced/test-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions api/advanced/test-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('a group of tests', () => { // [!code error]

## testIds <Version>4.1.0</Version> {#testids}

当前测试规范中需要运行的任务 ID 集合。
当前 TestSpecification 中需要运行的任务 ID 集合。

## testTagsFilter <Version>4.1.0</Version> {#testtagsfilter}

Expand Down
6 changes: 3 additions & 3 deletions api/advanced/test-suite.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,14 @@ describe('the validation works correctly', { meta: { decorated: true } }, () =>
:::tip
如果元数据是在收集阶段(而非 `test` 函数内部)附加的,那么它将在 available 的 [`onTestModuleCollected`](./reporters#ontestmodulecollected) 中可用。
:::
<!-- TODO: translation -->

## logs <Version>5.0.0</Version> {#logs}

```ts
function logs(): ReadonlyArray<UserConsoleLog>
```

Console logs recorded during test collection of this suite. For example:
此套件测试收集期间记录的 console 日志。例如:

```ts
describe('suite', () => {
Expand All @@ -251,4 +251,4 @@ describe('suite', () => {
function toTestSpecification(): TestSpecification
```

返回一个新的 [测试规范](/api/advanced/test-specification),该规范可用于筛选或运行此特定测试套件。
返回一个新的 [TestSpecification](/api/advanced/test-specification),该规范可用于筛选或运行此特定测试套件。
18 changes: 9 additions & 9 deletions api/advanced/vitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ function globTestSpecifications(
): Promise<TestSpecification[]>
```

此方法通过收集所有项目中的每个测试来构造新的 [测试规范](/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
Expand All @@ -162,7 +162,7 @@ function getRelevantTestSpecifications(
): Promise<TestSpecification[]>
```

此方法通过调用 [`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` 参数。如果我们只需要测试文件列表,请不要使用它。
Expand Down Expand Up @@ -191,7 +191,7 @@ function collect(filters?: string[]): Promise<TestRunResult>

执行测试文件而不运行测试回调。`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 将像运行常规测试一样在隔离环境中运行每个测试文件。
Expand Down Expand Up @@ -237,7 +237,7 @@ function standalone(): Promise<void>
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) 时,规范会自动缓存。

Expand All @@ -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

Expand Down Expand Up @@ -288,7 +288,7 @@ function runTestFiles(
): Promise<TestRunResult>
```

该功能会根据文件路径过滤器自动创建待运行的测试规范
该功能会根据文件路径过滤器自动创建待运行的 TestSpecification

这与 [`start`](#start) 的不同之处在于:它不会创建覆盖率提供程序、不会触发 `onInit` 和 `onWatcherStart` 事件,且在无文件可运行时也不会抛出错误(此时函数将返回空数组且不会触发测试运行)。

Expand All @@ -312,7 +312,7 @@ function collectTests(

执行测试文件而不运行测试回调。`collectTests` 返回未处理的错误和 [测试模块](/api/advanced/test-module) 数组。

此方法与 [`collect`](#collect) 完全相同,但我们需要自己提供测试规范
此方法与 [`collect`](#collect) 完全相同,但我们需要自己提供 TestSpecification

::: warning
请注意,Vitest 不使用静态分析来收集测试。Vitest 将像运行常规测试一样在隔离环境中运行每个测试文件。
Expand Down
10 changes: 5 additions & 5 deletions api/assert.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# assert

Vitest 从 [`chai`](https://www.chaijs.com/api/assert/) 重新导出了 `assert` 方法,用于验证不变量。
<!-- TODO: translation -->
::: 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 ++]
Expand All @@ -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 ++]
Expand Down
10 changes: 5 additions & 5 deletions api/browser/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ outline: deep
在浏览器测试中,可借助 `readFile`、`writeFile` 与 `removeFile` 三个 API 完成文件操作。自 Vitest 3.2 起,所有路径均以 [project](/guide/projects) 根目录为基准解析(根目录默认为 `process.cwd()`,可手动重写);旧版本则以当前测试文件所在目录为基准。

默认情况下,Vitest 使用 `utf-8` 编码,但你可以使用选项覆盖它。
<!-- TODO: translation -->

::: 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
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -126,7 +126,7 @@ declare module 'vitest/browser' {
::: warning
如果自定义命令具有相同的名称,则它们将覆盖内置命令。
:::

<!-- TODO: translation -->
::: 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.

Expand Down
Loading
Loading