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

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

- [`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)
- [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)
- [onTestCaseAnnotate 3.2.0 {#ontestcaseannotate}](#ontestcaseannotate-320-ontestcaseannotate)
- [onTestCaseArtifactRecord 4.0.11 {#ontestcaseartifactrecord}](#ontestcaseartifactrecord-4011-ontestcaseartifactrecord)

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

Expand Down Expand Up @@ -55,7 +51,7 @@ function onInit(vitest: Vitest): Awaitable<void>
当 [Vitest](/api/advanced/vitest) 初始化或启动时,但在测试被过滤之前,会调用此方法。

::: info
在内部,这个方法在 [`vitest.start`](/api/advanced/vitest#start)、[`vitest.init`](/api/advanced/vitest#init) 或 [`vitest.mergeReports`](/api/advanced/vitest#mergereports) 中调用。例如,如果我们使用 API,请确保根据我们的需要调用其中一个,然后再调用 [`vitest.runTestSpecifications`](/api/advanced/vitest#runtestspecifications)。内置的 CLI 将始终按正确的顺序运行方法。
在内部,这个方法在 [`vitest.start`](/api/advanced/vitest#start)、[`vitest.standalone`](/api/advanced/vitest#standalone) 或 [`vitest.mergeReports`](/api/advanced/vitest#mergereports) 中调用。例如,如果我们使用 API,请确保根据我们的需要调用其中一个,然后再调用 [`vitest.runTestSpecifications`](/api/advanced/vitest#runtestspecifications)。内置的 CLI 将始终按正确的顺序运行方法。
:::

请注意,我们还可以通过 [`project`](/api/advanced/test-project) 属性从测试用例、套件和测试模块中访问 `vitest` 实例,但在此方法中存储对 `vitest` 的引用也可能有用。
Expand Down
10 changes: 6 additions & 4 deletions api/advanced/vitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,19 @@ function start(filters?: string[]): Promise<TestRunResult>
初始化报告器、覆盖率提供者并运行测试。此方法接受字符串过滤器以匹配测试文件 - 这些过滤器与 [CLI 支持的过滤器](/guide/filtering#cli) 相同。

::: warning
如果还调用了 [`vitest.init()`](#init),则不应调用此方法。如果我们需要在 Vitest 初始化后运行测试,请使用 [`runTestSpecifications`](#runtestspecifications) 或 [`rerunTestSpecifications`](#reruntestspecifications)。
如果还调用了 [`vitest.standalone()`](#standalone),则不应调用此方法。如果我们需要在 Vitest 初始化后运行测试,请使用 [`runTestSpecifications`](#runtestspecifications) 或 [`rerunTestSpecifications`](#reruntestspecifications)。
:::

如果未设置 `config.mergeReports` 和 `config.standalone`,则此方法由 [`startVitest`](/guide/advanced/tests) 自动调用。

## init
## standalone <Version type="experimental">4.1.1</Version> {#standalone}

```ts
function init(): Promise<void>
function standalone(): Promise<void>
```

- **别名:**: `init` <Deprecated />

初始化报告器和覆盖率提供者。此方法不运行任何测试。如果提供了 `--watch` 标志,Vitest 仍将运行更改的测试,即使未调用此方法。

在内部,仅当启用了 [`--standalone`](/guide/cli#standalone) 标志时才会调用此方法。
Expand Down Expand Up @@ -548,7 +550,7 @@ function waitForTestRunEnd(): Promise<void>
function createCoverageProvider(): Promise<CoverageProvider | null>
```

当配置中启用了 `coverage` 时,创建覆盖率提供器。若使用 [`start`](#start) 或 [`init`](#init) 方法启动测试,这一步会自动完成。
当配置中启用了 `coverage` 时,创建覆盖率提供器。若使用 [`start`](#start) 或 [`standalone`](#standalone) 方法启动测试,这一步会自动完成。

::: warning
若未将 [`coverage.clean`](/config/coverage#coverage-clean) 显式设为 false ,此方法还会清空之前的所有报告。
Expand Down
2 changes: 1 addition & 1 deletion guide/cli-generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ watch 模式下重新运行测试时清除终端屏幕(默认值:`true`)

- **命令行终端:** `--standalone`

单独启动 Vitest,且不运行任何测试。仅在文件变更时才运行测试。如果通过命令行参数过滤文件,此选项将被忽略。(默认值:`false`)
单独启动 Vitest,且不运行任何测试。仅在文件变更时才运行测试。若启用浏览器模式,UI 界面将自动打开。如果通过命令行参数过滤文件,此选项将被忽略。(默认值:`false`)

### listTags

Expand Down
Loading