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
4 changes: 2 additions & 2 deletions .github/workflows/autofix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
timeout-minutes: 10

steps:
- uses: actions/checkout@v5
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

- name: Use Node.js lts/*
uses: actions/setup-node@v4
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
with:
node-version: lts/*

Expand Down
2 changes: 1 addition & 1 deletion .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default ({ mode }: { mode: string }) => {
zh: {
label: 'English',
lang: 'en-US',
link: 'https://vitest.dev/',
link: 'https://v3.vitest.dev/',
},
},
head: [
Expand Down
10 changes: 7 additions & 3 deletions advanced/api/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ outline: deep
Vitest 自 3.1 版起支持实验性的 `configureVitest` [插件](https://cn.vite.dev/guide/api-plugin) hook。欢迎在 [GitHub](https://github.com/vitest-dev/vitest/discussions/7104) 中提供有关此 API 的任何反馈。

::: code-group

```ts [only vitest]
import type { Vite, VitestPluginContext } from 'vitest/node'

Expand All @@ -26,6 +27,7 @@ export function plugin(): Vite.Plugin {
}
}
```

```ts [vite and vitest]
/// <reference types="vitest/config" />

Expand All @@ -43,14 +45,16 @@ export function plugin(): Plugin {
}
}
```

:::

::: tip TypeScript
Vitest 通过 `Vite` namespace 重新导出所有仅 Vite 类型的导入,我们可以使用它来保持版本同步。但是,如果我们正在为 Vite 和 Vitest 编写插件,则可以继续使用 `vite` 入口点的 `Plugin` 类型。只需确保我们在某处引用了 `vitest/config` ,以便正确增强 `configureVitest` 即可:
Vitest 通过 `Vite` namespace 重新导出所有仅 Vite 类型的导入,我们可以使用它来保持版本同步。但是,如果我们正在为 Vite 和 Vitest 编写插件,则可以继续使用 `vite` 入口点的 `Plugin` 类型。只需确保我们在某处引用了 `vitest/config`,以便正确增强 `configureVitest` 即可:

```ts
/// <reference types="vitest/config" />
```

:::

与 [`reporter.onInit`](/advanced/api/reporters#oninit) 不同,此 hooks 在 Vitest 生命周期的早期运行,允许我们更改 `coverage` 和 `reporters` 等配置。更值得注意的变化是,如果我们的插件是在项目中定义而不是在全局配置中定义的,我们可以从 [工作区项目](/guide/projects) 操作全局配置。
Expand All @@ -59,7 +63,7 @@ Vitest 通过 `Vite` namespace 重新导出所有仅 Vite 类型的导入,我

### project

该插件所属的当前[测试项目](./test-project)。
该插件所属的当前 [测试项目](./test-project)。

::: warning 浏览器模式
请注意,如果我们依赖浏览器功能,则 `project.browser` 字段尚未设置。请改用 [`reporter.onBrowserInit`](./reporters#onbrowserinit) 事件。
Expand All @@ -75,7 +79,7 @@ vitest.config.reporters.push([['my-reporter', {}]])
```

::: warning 配置已解析完成
请注意,Vitest 已经解析了配置,因此某些类型可能与通常的用户配置不同。这也意味着某些属性将不会再次解析,例如 `setupFile` 。如果我们要添加新文件,请确保先解析它。
请注意,Vitest 已经解析了配置,因此某些类型可能与通常的用户配置不同。这也意味着某些属性将不会再次解析,例如 `setupFile`。如果我们要添加新文件,请确保先解析它。

此时尚未创建记者,因此修改 `vitest.reporters` 将不起作用,因为它将被覆盖。如果我们需要注入自己的记者,请修改配置。
:::
Expand Down
21 changes: 21 additions & 0 deletions advanced/api/reporters-life-cycle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
- [`onInit`](#oninit)
- [`onTestRunStart`](#ontestrunstart)
- [`onTestModuleQueued`](#ontestmodulequeued)
- [`onTestModuleCollected`](#ontestmodulecollected)
- [`onTestModuleStart`](#ontestmodulestart)
- [`onTestSuiteReady`](#ontestsuiteready)
- [`onHookStart(beforeAll)`](#onhookstart)
- [`onHookEnd(beforeAll)`](#onhookend)
- [`onTestCaseReady`](#ontestcaseready)
- [`onTestAnnotate`](#ontestannotate) <Version>3.2.0</Version>
- [`onHookStart(beforeEach)`](#onhookstart)
- [`onHookEnd(beforeEach)`](#onhookend)
- [`onHookStart(afterEach)`](#onhookstart)
- [`onHookEnd(afterEach)`](#onhookend)
- [`onTestCaseResult`](#ontestcaseresult)
- [`onHookStart(afterAll)`](#onhookstart)
- [`onHookEnd(afterAll)`](#onhookend)
- [`onTestSuiteResult`](#ontestsuiteresult)
- [`onTestModuleEnd`](#ontestmoduleend)
- [`onCoverage`](#oncoverage)
- [`onTestRunEnd`](#ontestrunend)
28 changes: 7 additions & 21 deletions advanced/api/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,7 @@

Vitest 拥有自己的测试运行生命周期。这些生命周期通过报告器的方法来表示:

- [`onInit`](#oninit)
- [`onTestRunStart`](#ontestrunstart)
- [`onTestModuleQueued`](#ontestmodulequeued)
- [`onTestModuleCollected`](#ontestmodulecollected)
- [`onTestModuleStart`](#ontestmodulestart)
- [`onTestSuiteReady`](#ontestsuiteready)
- [`onHookStart(beforeAll)`](#onhookstart)
- [`onHookEnd(beforeAll)`](#onhookend)
- [`onTestCaseReady`](#ontestcaseready)
- [`onTestAnnotate`](#ontestannotate) <Version>3.2.0</Version>
- [`onHookStart(beforeEach)`](#onhookstart)
- [`onHookEnd(beforeEach)`](#onhookend)
- [`onHookStart(afterEach)`](#onhookstart)
- [`onHookEnd(afterEach)`](#onhookend)
- [`onTestCaseResult`](#ontestcaseresult)
- [`onHookStart(afterAll)`](#onhookstart)
- [`onHookEnd(afterAll)`](#onhookend)
- [`onTestSuiteResult`](#ontestsuiteresult)
- [`onTestModuleEnd`](#ontestmoduleend)
- [`onCoverage`](#oncoverage)
- [`onTestRunEnd`](#ontestrunend)
<!--@include: ./reporters-life-cycle.md-->

除非被跳过,否则单个模块中的测试和 reporters 将按顺序报告。所有跳过的测试将在 reporters /模块的末尾报告。

Expand Down Expand Up @@ -60,6 +40,7 @@ function onInit(vitest: Vitest): Awaitable<void>
请注意,我们还可以通过 [`project`](/advanced/api/test-project) 属性从测试用例、套件和测试模块中访问 `vitest` 实例,但在此方法中存储对 `vitest` 的引用也可能有用。

::: details 示例

```ts
import type { Reporter, TestSpecification, Vitest } from 'vitest/node'

Expand All @@ -81,6 +62,7 @@ class MyReporter implements Reporter {

export default new MyReporter()
```

:::

## onBrowserInit <Badge type="warning">实验性</Badge> {#onbrowserinit}
Expand All @@ -104,6 +86,7 @@ function onTestRunStart(
如果 Vitest 没有找到任何要运行的测试文件,此事件将以空数组调用,然后 [`onTestRunEnd`](#ontestrunend) 将立即被调用。

::: details 示例

```ts
import type { Reporter, TestSpecification } from 'vitest/node'

Expand All @@ -115,6 +98,7 @@ class MyReporter implements Reporter {

export default new MyReporter()
```

:::

::: tip 弃用通知
Expand Down Expand Up @@ -146,6 +130,7 @@ function onTestRunEnd(
如果 Vitest 没有找到任何要运行的测试文件,此事件将以空的模块和错误数组调用,状态将取决于 [`config.passWithNoTests`](/config/#passwithnotests) 的值。

::: details 示例

```ts
import type {
Reporter,
Expand Down Expand Up @@ -182,6 +167,7 @@ class MyReporter implements Reporter {

export default new MyReporter()
```

:::

::: tip 弃用通知
Expand Down
4 changes: 2 additions & 2 deletions advanced/api/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
```

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

```ts {3,6}
import { test } from 'vitest'
Expand All @@ -146,7 +146,7 @@ test('the validation works correctly', ({ task }) => {
function result(): TestResult
```

测试结果。如果测试尚未完成或刚刚开始收集,等于 `TestResultPending`
测试结果。如果测试尚未完成或刚刚开始收集,等于 `TestResultPending`:

```ts
export interface TestResultPending {
Expand Down
1 change: 1 addition & 0 deletions advanced/api/test-collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ for (const child of module.children) {
console.log(child.type, child.name)
}
```

:::

## size
Expand Down
11 changes: 10 additions & 1 deletion advanced/api/test-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ title: TestProject
名称是由用户分配或由 Vitest 解析的唯一字符串。如果用户没有提供名称,Vitest 会尝试加载项目根目录中的 `package.json` 并从中获取 `name` 属性。如果没有 `package.json`,Vitest 默认使用文件夹的名称。内联项目使用数字作为名称(转换为字符串)。

::: code-group

```ts [node.js]
import { createVitest } from 'vitest/node'

Expand All @@ -24,6 +25,7 @@ vitest.projects.map(p => p.name) === [
'custom'
]
```

```ts [vitest.config.js]
import { defineConfig } from 'vitest/config'

Expand All @@ -48,6 +50,7 @@ export default defineConfig({
},
})
```

:::

::: info
Expand All @@ -74,6 +77,7 @@ const config: SerializedConfig = vitest.projects[0].serializedConfig
```ts
project.serializedConfig === project.serializedConfig // ❌
```

:::

## globalConfig
Expand Down Expand Up @@ -120,6 +124,7 @@ function provide<T extends keyof ProvidedContext & string>(
除了 [`config.provide`](/config/#provide) 字段外,还提供了一种向测试提供自定义值的方法。所有值在存储之前都通过 [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone) 进行验证,但 `providedContext` 上的值本身不会被克隆。

::: code-group

```ts [node.js]
import { createVitest } from 'vitest/node'

Expand All @@ -128,10 +133,12 @@ const project = vitest.projects.find(p => p.name === 'custom')
project.provide('key', 'value')
await vitest.start()
```

```ts [test.spec.js]
import { inject } from 'vitest'
const value = inject('key')
```

:::

这些值可以动态提供。测试中提供的值将在下次运行时更新。
Expand All @@ -144,6 +151,7 @@ export default function setup({ provide }) {
provide('wsPort', 3000)
}
```

:::

## getProvidedContext
Expand Down Expand Up @@ -240,7 +248,7 @@ Vitest 使用 [fast-glob](https://www.npmjs.com/package/fast-glob) 来查找测
- `test.include`、`test.exclude` 用于查找常规测试文件
- `test.includeSource`、`test.exclude` 用于查找源代码中的测试
- `test.typecheck.include`、`test.typecheck.exclude` 用于查找类型检查测试
:::
:::

## matchesTestGlob

Expand Down Expand Up @@ -286,6 +294,7 @@ const dynamicExample = await project.import('./example.js')

dynamicExample !== staticExample // ✅
```

:::

::: info
Expand Down
3 changes: 3 additions & 0 deletions advanced/api/test-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ Vite 模块图中的模块 ID。通常,它是一个使用 POSIX 分隔符的
请注意,如果这些行中的至少一行没有测试,整个测试套件将会失败。以下是一个正确的 `testLines` 配置示例:

::: code-group

```ts [script.js]
const specification = project.createSpecification(
resolve('./example.test.ts'),
[3, 8, 9],
)
```

```ts:line-numbers{3,8,9} [example.test.js]
import { test, describe } from 'vitest'

Expand All @@ -68,6 +70,7 @@ describe('a group of tests', () => { // [!code error]
test.skip('skipped test')
})
```

:::

## toJSON
Expand Down
3 changes: 2 additions & 1 deletion advanced/api/test-suite.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ describe('collection failed', () => {
```ts
function meta(): TaskMeta
```
在执行或收集过程中附加到套件的自定义[元数据](/advanced/metadata)。在测试运行期间,可以通过向 `task.meta` 对象分配属性来附加 meta:

在执行或收集过程中附加到套件的自定义 [元数据](/advanced/metadata)。在测试运行期间,可以通过向 `task.meta` 对象分配属性来附加 meta:

```ts {5,10}
import { test } from 'vitest'
Expand Down
14 changes: 8 additions & 6 deletions advanced/api/vitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Vitest 4 新增了多个 API(它们都标记有 "4.0.0+" 徽章),并移除
- `globTestSpecs`(使用 [`globTestSpecifications`](#globtestspecifications) 代替)
- `globTestFiles`(使用 [`globTestSpecifications`](#globtestspecifications) 代替)
- `listFile`(使用 [`getRelevantTestSpecifications`](#getrelevanttestspecifications) 代替)
:::
:::

## mode

Expand Down Expand Up @@ -74,7 +74,7 @@ const testCase = vitest.state.getReportedEntity(task) // 新 API

## projects

这是一个数组,里面包含了所有 [测试项目](/advanced/api/test-project) ,这些项目是用户自己定义的。如果用户没有显式指定任何项目,那么这个数组中只会包含一个 [根项目](#getrootproject)
这是一个数组,里面包含了所有 [测试项目](/advanced/api/test-project),这些项目是用户自己定义的。如果用户没有显式指定任何项目,那么这个数组中只会包含一个 [根项目](#getrootproject)。

Vitest 会保证这个数组里至少有一个项目可用。如果用户在命令行里通过 --project 参数指定了不存在的项目名称,Vitest 会在创建这个数组前就报错。

Expand Down Expand Up @@ -145,7 +145,7 @@ function getProvidedContext(): ProvidedContext
function getProjectByName(name: string): TestProject
```

此方法通过名称返回项目。类似于调用 `vitest.projects.find`
此方法通过名称返回项目。类似于调用 `vitest.projects.find`。

::: warning
如果项目不存在,此方法将返回根项目 - 请确保再次检查返回的项目是否是我们要找的项目。
Expand Down Expand Up @@ -190,7 +190,7 @@ function getRelevantTestSpecifications(

- 如果我们需要获取已知测试文件的规范列表,请使用 [`getModuleSpecifications`](#getmodulespecifications) 代替。
- 如果我们需要获取所有可能的测试文件列表,请使用 [`globTestSpecifications`](#globtestspecifications)。
:::
:::

## mergeReports

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

该方法会根据接收到的 [测试规范](/advanced/api/test-specification) 运行所有测试。 第二个参数 `allTestsRun` 由覆盖率提供程序使用,用于确定是否需要对根目录下的每个文件进行覆盖率插桩(仅在启用覆盖率且 `coverage.all` 设置为 `true`时生效)。
该方法会根据接收到的 [测试规范](/advanced/api/test-specification) 运行所有测试。 第二个参数 `allTestsRun` 由覆盖率提供程序使用,用于确定是否需要对根目录下的每个文件进行覆盖率插桩(仅在启用覆盖率且 `coverage.all` 设置为 `true` 时生效)。

::: warning
此方法不会触发 `onWatcherRerun`、`onWatcherStart` 和 `onTestsRerun` 回调。如果我们基于文件更改重新运行测试,请考虑使用 [`rerunTestSpecifications`](#reruntestspecifications) 代替。
Expand Down Expand Up @@ -403,6 +403,7 @@ const dynamicExample = await vitest.import('./example.js')

dynamicExample !== staticExample // ✅
```

:::

::: info
Expand Down Expand Up @@ -474,6 +475,7 @@ function onFilterWatchedSpecification(
fn: (specification: TestSpecification) => boolean
): void
```

注册一个处理程序,当文件更改时调用。此回调应返回 `true` 或 `false`,指示是否需要重新运行测试文件。

通过此方法,我们可以挂钩到默认的观察器逻辑,以延迟或丢弃用户当前不想跟踪的测试:
Expand All @@ -498,6 +500,6 @@ Vitest 可以根据 `pool` 或 `locations` 选项为同一文件创建不同的
function matchesProjectFilter(name: string): boolean
```

检查名称是否与当前 [项目过滤器](/guide/cli#project) 匹配。如果没有项目过滤器,则始终返回 `true`
检查名称是否与当前 [项目过滤器](/guide/cli#project) 匹配。如果没有项目过滤器,则始终返回 `true`。

无法通过编程方式更改 `--project` CLI 选项。
Loading
Loading