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
50 changes: 27 additions & 23 deletions api/advanced/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@

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

- [报告器 {#reporters}](#报告器-reporters)
- [onInit](#oninit)
- [onBrowserInit {#onbrowserinit}](#onbrowserinit-onbrowserinit)
- [onTestRunStart](#ontestrunstart)
- [onTestRunEnd](#ontestrunend)
- [onCoverage](#oncoverage)
- [onTestModuleQueued](#ontestmodulequeued)
- [onTestModuleCollected](#ontestmodulecollected)
- [onTestModuleStart](#ontestmodulestart)
- [onTestModuleEnd](#ontestmoduleend)
- [onHookStart](#onhookstart)
- [onHookEnd](#onhookend)
- [onTestSuiteReady](#ontestsuiteready)
- [onTestSuiteResult](#ontestsuiteresult)
- [onTestCaseReady](#ontestcaseready)
- [onTestCaseResult](#ontestcaseresult)
- [onTestAnnotate 3.2.0 {#ontestannotate}](#ontestannotate-320-ontestannotate)
- [onTestCaseArtifactRecord 4.0.11 {#ontestcaseartifactrecord}](#ontestcaseartifactrecord-4011-ontestcaseartifactrecord)

除非被跳过,否则单个模块中的测试和 reporters 将按顺序报告。所有跳过的测试将在 reporters /模块的末尾报告。
- [`onInit`](#oninit)
- [`onTestRunStart`](#ontestrunstart)
- [`onTestModuleQueued`](#ontestmodulequeued)
- [`onTestModuleCollected`](#ontestmodulecollected)
- [`onTestModuleStart`](#ontestmodulestart)
- [`onTestSuiteReady`](#ontestsuiteready)
- [`onHookStart(beforeAll)`](#onhookstart)
- [`onHookEnd(beforeAll)`](#onhookend)
- [`onTestCaseReady`](#ontestcaseready)
- [`onTestCaseAnnotate`](#ontestcaseannotate) <Version>3.2.0</Version>
- [`onTestCaseArtifactRecord`](#ontestcaseartifactrecord) <Version type="experimental">4.0.11</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)

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

请注意,由于测试模块可以并行运行,Vitest 将并行报告它们。

Expand Down Expand Up @@ -309,16 +313,16 @@ function onTestCaseResult(testCase: TestCase): Awaitable<void>

此时,[`testCase.result()`](/api/advanced/test-case#result) 已不再是挂起状态。

## onTestAnnotate <Version>3.2.0</Version> {#ontestannotate}
## onTestCaseAnnotate <Version>3.2.0</Version> {#ontestcaseannotate}

```ts
function onTestAnnotate(
function onTestCaseAnnotate(
testCase: TestCase,
annotation: TestAnnotation,
): Awaitable<void>
```

onTestAnnotate 是与 [`context.annotate`](/guide/test-context#annotate) 方法配套使用的钩子。当你在测试中调用 annotate 后, Vitest 会将注解内容序列化,并将其发送到主线程,从而让报告器可以处理这些附加信息。
`onTestCaseAnnotate` 是与 [`context.annotate`](/guide/test-context#annotate) 方法配套使用的钩子。当你在测试中调用 annotate 后, Vitest 会将注解内容序列化,并将其发送到主线程,从而让报告器可以处理这些附加信息。

如果在注解中指定了文件路径, Vitest 会将附件保存到一个独立的目录(该目录通过 [`attachmentsDir`](/config/#attachmentsdir) 配置),并自动更新 path 属性,使其指向存储后的文件位置。

Expand Down
6 changes: 5 additions & 1 deletion api/advanced/test-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,14 @@ interface ImportDuration {
}
```

## viteEnvironment <Version type="experimental">4.0.15</Version> <Experimental /> {#viteenvironment}
## 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.

::: details History
- `v4.0.15`: added as experimental
:::

## toTestSpecification <Version>4.1.0</Version> {#totestspecification}

```ts
Expand Down
2 changes: 1 addition & 1 deletion api/advanced/test-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Vite 模块图中的模块 ID。通常,它是一个使用 POSIX 分隔符的

与规范相关联的 [`TestModule`](/api/advanced/test-module) 实例。如果测试还未加入队列,则将是 `undefined`。

## pool <Badge type="warning">experimental</Badge> {#pool}
## pool {#pool}

测试模块将运行的 [`pool`](/config/#pool)。

Expand Down
5 changes: 2 additions & 3 deletions api/advanced/vitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,8 @@ function onCancel(fn: (reason: CancelReason) => Awaitable<void>): () => void

注册一个处理程序,当测试运行被 [`vitest.cancelCurrentRun`](#cancelcurrentrun) 取消时调用。

::: warning 实验性
自 4.0.10 起,`onCancel` 会返回一个用于移除监听器的清理函数。
:::
<!-- TODO: translation -->
Since 4.0.10, `onCancel` experimentally returns a teardown function that will remove the listener. Since 4.1.0 this behaviour is considered stable.

## onClose

Expand Down
1 change: 0 additions & 1 deletion api/browser/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ export const utils: {
/**
* Configures default options of `prettyDOM` and `debug` functions.
* This will also affect `vitest-browser-{framework}` package.
* @experimental
*/
configurePrettyDOM(options: StringifyOptions): void
/**
Expand Down
4 changes: 0 additions & 4 deletions guide/advanced/tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ for (const testModule of testModules) {
}
```

::: tip
[`TestModule`](/api/advanced/test-module), [`TestSuite`](/api/advanced/test-suite) 和 [`TestCase`](/api/advanced/test-case) API 从 Vitest 2.1 开始不再是实验性的,并且遵循 SemVer。
:::

## `createVitest`

创建一个 [Vitest](/api/advanced/vitest) 实例而不运行测试。
Expand Down
2 changes: 1 addition & 1 deletion guide/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import type { Environment } from 'vitest/runtime'
export default <Environment>{
name: 'custom',
viteEnvironment: 'ssr',
// 可选 - 仅在支持 "experimental-vm" 的情况下使用
// 可选 - 仅在支持 "vmForks" 或 "vmThreads" 线程池时需要设置
async setupVM() {
const vm = await import('node:vm')
const context = vm.createContext()
Expand Down
4 changes: 2 additions & 2 deletions guide/snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ it('render basic', async () => {

## 图像快照 {#visual-snapshots}

对于 UI 组件和页面的视觉回归测试,Vitest 通过[浏览器模式](/guide/browser/)提供了内置支持,使用 [`toMatchScreenshot()`](/api/browser/assertions#tomatchscreenshot-experimental) 断言:
对于 UI 组件和页面的视觉回归测试,Vitest 通过 [浏览器模式](/guide/browser/) 提供了内置支持,使用 [`toMatchScreenshot()`](/api/browser/assertions#tomatchscreenshot) 断言:

```ts
import { expect, test } from 'vitest'
Expand All @@ -110,7 +110,7 @@ test('button looks correct', async () => {
})
```

它会捕获屏幕截图并与参考图像进行比较,以检测意外的视觉变化。在[视觉回归测试指南](/guide/browser/visual-regression-testing)中了解更多内容。
它会捕获屏幕截图并与参考图像进行比较,以检测意外的视觉变化。在 [视觉回归测试指南](/guide/browser/visual-regression-testing)中了解更多内容。

## 自定义序列化程序 {#custom-serializer}

Expand Down