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/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<!-- 感谢你的贡献! -->

### 在提交PR之前,请确保您执行以下操作:
### 在提交PR之前,请确保你执行以下操作:
- [ ] 曾阅读过[翻译须知](https://github.com/vitest-dev/docs-cn/issues/391)。
- [ ] 检查是否已经有PR以同样的方式解决问题,以避免创建重复。
- [ ] 在此PR中描述正在解决的问题,或引用它解决的问题(例如:`fixes #123`)。

---

### 描述
<!-- 请在此处插入您的描述,并提供有关此PR正在解决的 “内容” 的特别信息 -->
<!-- 请在此处插入你的描述,并提供有关此PR正在解决的 “内容” 的特别信息 -->

### 附加上下文
<!-- 例如,你有什么想让代码审核的人重点关注的内容。 -->
2 changes: 1 addition & 1 deletion api/advanced/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function injectTestProjects(
```ts
// 为单个项目注入自定义别名
const newProjects = await injectTestProjects({
// 您可以通过引用 `extends` 来继承当前项目配置
// 你可以通过引用 `extends` 来继承当前项目配置
// 注意,不能使用已经存在的项目名称,
// 所以定义自定义名称是一个好实践。
extends: project.vite.config.configFile,
Expand Down
24 changes: 24 additions & 0 deletions api/advanced/reporters-life-cycle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- 因经常误被错误格式化,估另抽单个文件处理-->

- [`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)
19 changes: 1 addition & 18 deletions api/advanced/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,7 @@

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)
- [onTestCaseAnnotate 3.2.0 {#ontestcaseannotate}](#ontestcaseannotate-320-ontestcaseannotate)
- [onTestCaseArtifactRecord 4.0.11 {#ontestcaseartifactrecord}](#ontestcaseartifactrecord-4011-ontestcaseartifactrecord)
<!--@include: ./reporters-life-cycle.md-->

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

Expand Down
2 changes: 1 addition & 1 deletion api/advanced/runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export interface TaskResult {

## 你的任务函数 {#your-task-function}

Vitest 提供了 `createTaskCollector` 工具来创建您自己的 `test` 方法。它的行为与测试相同,但在收集期间会调用自定义方法。
Vitest 提供了 `createTaskCollector` 工具来创建你自己的 `test` 方法。它的行为与测试相同,但在收集期间会调用自定义方法。

任务是套件的一部分对象。它会通过 `suite.task` 方法自动添加到当前套件中:

Expand Down
4 changes: 2 additions & 2 deletions api/advanced/test-case.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# TestCase

`TestCase` 类表示单个测试。此类仅在主线程中可用。如果您正在处理运行时任务,请参阅 [“运行器 API”](/api/advanced/runner#tasks)。
`TestCase` 类表示单个测试。此类仅在主线程中可用。如果你正在处理运行时任务,请参阅 [“运行器 API”](/api/advanced/runner#tasks)。

`TestCase` 实例始终有一个值为 `test` 的 `type` 属性。您可以使用它来区分不同的任务类型
`TestCase` 实例始终有一个值为 `test` 的 `type` 属性。你可以使用它来区分不同的任务类型

```ts
if (task.type === 'test') {
Expand Down
10 changes: 4 additions & 6 deletions api/advanced/test-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ Vite 模块图中的模块 ID。通常,它是一个使用 POSIX 分隔符的

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

<!-- TODO: translation -->

::: danger
It's possible to have multiple pools in a single test project with [`typecheck.enabled`](/config/typecheck#typecheck-enabled). This means it's possible to have several specifications with the same `moduleId` but different `pool`. In later versions, the project will only support a single pool.
在启用 [`typecheck.enabled`](/config/typecheck#typecheck-enabled) 配置的情况下,单个测试项目中可能存在多个运行池。这意味着可能出现多个测试规范共享相同 `moduleId` 但使用不同 `pool` 的情况。请注意,后续版本将仅支持单一运行池模式。
:::

## testLines
Expand Down Expand Up @@ -79,15 +77,15 @@ describe('a group of tests', () => { // [!code error]

## testNamePattern <Version>4.1.0</Version> {#testnamepattern}

A regexp that matches the name of the test in this module. This value will override the global [`testNamePattern`](/config/testnamepattern) option if it's set.
用于匹配当前模块中测试名称的正则表达式。如果已设置,该值将覆盖全局 [`testNamePattern`](/config/testnamepattern) 配置。

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

The ids of tasks inside of this specification to run.
当前测试规范中需要运行的任务 ID 集合。

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

The [tags filter](/guide/test-tags#syntax) that a test must pass in order to be included in the run. Multiple filters are treated as `AND`.
测试必须通过的 [标签过滤器](/guide/test-tags#syntax) 才能被纳入运行范围。多个过滤器将按 `AND` 逻辑处理。

## toJSON

Expand Down
45 changes: 21 additions & 24 deletions api/advanced/vitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,6 @@ function rerunTestSpecifications(

此方法发出 `reporter.onWatcherRerun` 和 `onTestsRerun` 事件,然后使用 [`runTestSpecifications`](#runtestspecifications) 运行测试。如果主进程中没有错误,它将发出 `reporter.onWatcherStart` 事件。

<!-- TODO: translation -->

## runTestFiles <Version>4.1.0</Version> {#runtestfiles}

```ts
Expand All @@ -315,11 +313,11 @@ function runTestFiles(
): Promise<TestRunResult>
```

This automatically creates specifications to run based on filepaths filters.
该功能会根据文件路径过滤器自动创建待运行的测试规范。

This is different from [`start`](#start) because it does not create a coverage provider, trigger `onInit` and `onWatcherStart` events, or throw an error if there are no files to run (in this case, the function will return empty arrays without triggering a test run).
这与 [`start`](#start) 的不同之处在于:它不会创建覆盖率提供程序、不会触发 `onInit` `onWatcherStart` 事件,且在无文件可运行时也不会抛出错误(此时函数将返回空数组且不会触发测试运行)。

This function accepts the same filters as [`start`](#start) and the CLI.
此函数接受的过滤器参数与 [`start`](#start) 及命令行接口完全一致。

## updateSnapshot

Expand Down Expand Up @@ -482,8 +480,7 @@ function onCancel(fn: (reason: CancelReason) => Awaitable<void>): () => void

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

<!-- 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.
自 4.0.10 起,`onCancel` 实验性地返回一个清理函数,该函数会移除监听器。自 4.1.0 起,此行为被视为稳定。

## onClose

Expand Down Expand Up @@ -696,26 +693,26 @@ export interface SourceModuleDiagnostic {
::: warning
[浏览器模式](/guide/browser/) 暂不支持。
:::
<!-- TODO: translation -->

## createReport <Version>5.0.0</Version> {#createreport}

```ts
function createReport(scope: string): Report
```

Creates a report that is limited to the given scope. `Report` follows Vitest's rules around [Storing artifacts on file system](/guide/advanced/reporters.html#storing-artifacts-on-file-system).
创建一个仅限于给定作用域的报告。`Report` 遵循 Vitest 关于 [在文件系统中存储工件](/guide/advanced/reporters.html#storing-artifacts-on-file-system) 的规则。

`Report` provides collection of utilities for writing test results, temporary files and other artifacts on the file system. It's especially intended for third party integrations like custom reporters.
`Report` 提供了一系列用于在文件系统中写入测试结果、临时文件和其他产物的工具函数。它特别适用于第三方集成,例如自定义报告器。

All operations of `Report` are limited to given `scope`. A single report cannot interfere with other reports. Internally Vitest creates `.vitest` directory where each `scope` creates their own directory. This convention of `.vitest` directory reduces the amount of entries end-users need to specify in their `.gitignore`.
`Report` 的所有操作都限制在给定的 `scope` 内。单个报告不会干扰其他报告。Vitest 内部会创建一个 `.vitest` 目录,每个 `scope` 在其中创建自己的子目录。这种 `.vitest` 目录的约定减少了最终用户需要在 `.gitignore` 中指定的条目数量。

```ts
import type { Report } from 'vitest/node'

const scope = 'example-yaml-reporter'

// Automatically creates `<project-root>/.vitest/example-yaml-reporter/`
// directory if it does not exist already
// 自动创建 `<project-root>/.vitest/example-yaml-reporter/`
// 如果目录不存在
const report: Report = vitest.createReport(scope)
```

Expand All @@ -730,7 +727,7 @@ The root directory for this scope.
```ts
const report = vitest.createReport('my-json-reporter')

// Is <project-root>/.vitest/my-json-reporter
// <project-root>/.vitest/my-json-reporter
const root = report.root
```

Expand All @@ -740,12 +737,12 @@ const root = report.root
function clean(): Promise<void>
```

Clean up the report directory for this scope.
清理此作用域的报告目录。

```ts
const report = vitest.createReport('my-json-reporter')

// Removes everything inside <project-root>/.vitest/my-json-reporter/
// 删除 <project-root>/.vitest/my-json-reporter/ 内的所有内容
await report.clean()
```

Expand All @@ -759,12 +756,12 @@ function writeFile(
): Promise<void>
```

Write a file to the report directory for this scope. By default the file will be written with UTF-8 encoding. The filename is relative to the scope directory.
向此作用域的报告目录写入文件。默认情况下,文件将以 UTF-8 编码写入。文件名是相对于作用域目录的。

```ts
const report = vitest.createReport('my-json-reporter')

// Writes file to .vitest/my-json-reporter/test-report.json
// 将文件写入 .vitest/my-json-reporter/test-report.json
await report.writeFile('test-report.json', JSON.stringify(results))
```

Expand All @@ -774,12 +771,12 @@ await report.writeFile('test-report.json', JSON.stringify(results))
function readFile(filename: string, encoding?: BufferEncoding): Promise<string>
```

Read a file from the report directory for this scope.
从此作用域的报告目录读取文件。

```ts
const report = vitest.createReport('my-json-reporter')

// Reads file from .vitest/my-json-reporter/test-report.json
// .vitest/my-json-reporter/test-report.json 读取文件
const content: string = await report.readFile('test-report.json')
```

Expand All @@ -789,12 +786,12 @@ const content: string = await report.readFile('test-report.json')
function readdir(): Promise<string[]>
```

Read contents of the report directory for this scope.
读取此作用域报告目录的内容。

```ts
const report = vitest.createReport('my-json-reporter')

// Reads contents from .vitest/my-json-reporter
// from .vitest/my-json-reporter 读取内容
const filenames: string[] = await report.readdir()
```

Expand All @@ -805,11 +802,11 @@ const filenames: string[] = await report.readdir()
function delete(filename: string): Promise<void>
```

Delete a file from the report directory for this scope.
从此作用域的报告目录删除文件。

```ts
const report = vitest.createReport('my-json-reporter')

// Deletes file from .vitest/my-json-reporter/test-report.json
// .vitest/my-json-reporter/test-report.json 删除文件
await report.delete('test-report.json')
```
2 changes: 1 addition & 1 deletion api/browser/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ declare module 'vitest/browser' {
Vitest 在命令上下文中公开了几个`playwright`特定属性。

- `page`引用包含测试 iframe 的完整页面。这是协调器 HTML,为避免出现问题,最好不要碰它。
- `frame` 是一个异步方法,用于解析测试器 [`Frame`](https://playwright.dev/docs/api/class-frame)。它的 API 与 `page` 类似,但不支持某些方法。如果您需要查询元素,应优先使用 `context.iframe` 代替,因为它更稳定、更快速。
- `frame` 是一个异步方法,用于解析测试器 [`Frame`](https://playwright.dev/docs/api/class-frame)。它的 API 与 `page` 类似,但不支持某些方法。如果你需要查询元素,应优先使用 `context.iframe` 代替,因为它更稳定、更快速。
- `iframe` 是一个 [`FrameLocator`](https://playwright.dev/docs/api/class-framelocator),用于查询页面上的其他元素。
- `context` 是指唯一的[BrowserContext](https://playwright.dev/docs/api/class-browsercontext)。

Expand Down
4 changes: 2 additions & 2 deletions api/browser/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ export const commands: BrowserCommands
页面导出提供了与当前页面交互的实用程序。

::: warning
虽然它从 Playwright 的 `page` 中获取了一些实用程序,但它与 Playwright 的 `page` 并不是同一个对象。由于浏览器上下文是在浏览器中评估的,您的测试无法访问 Playwright 的 `page`,因为它是在服务器上运行的。
虽然它从 Playwright 的 `page` 中获取了一些实用程序,但它与 Playwright 的 `page` 并不是同一个对象。由于浏览器上下文是在浏览器中评估的,你的测试无法访问 Playwright 的 `page`,因为它是在服务器上运行的。
:::

使用 [Commands API](/api/browser/commands) 如果您需要访问 Playwright 的 `page` 对象。
使用 [Commands API](/api/browser/commands) 如果你需要访问 Playwright 的 `page` 对象。
<!-- TODO: translation -->
```ts
export const page: {
Expand Down
2 changes: 1 addition & 1 deletion api/browser/interactivity.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ test('update input', async () => {
})
```

该方法聚焦元素、填充元素并在填充后触发一个 `input` 事件。您可以使用空字符串来清除字段
该方法聚焦元素、填充元素并在填充后触发一个 `input` 事件。你可以使用空字符串来清除字段

::: tip
该 API 比使用 [`userEvent.type`](#userevent-type) 或 [`userEvent.keyboard`](#userevent-keyboard) 更快,但**不支持** [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard) (例如,`{Shift}{selectall}`)。
Expand Down
Loading
Loading