Skip to content
Draft
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
38 changes: 17 additions & 21 deletions advanced/api/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,23 @@

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)
- [报告器 {#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)

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

Expand Down
4 changes: 2 additions & 2 deletions advanced/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { DefaultReporter } from 'vitest/reporters'

export default class MyDefaultReporter extends DefaultReporter {
// do something
// 执行操作
}
```

Expand All @@ -40,7 +40,7 @@ import type { Reporter } from 'vitest/node'

export default class CustomReporter implements Reporter {
onTestModuleCollected() {
// print something
// 打印内容
}
}
```
Expand Down
6 changes: 3 additions & 3 deletions api/expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ test('matches snapshot', () => {
})
```

我们还可以提供一个对象的形状,如果我们只是测试对象的形状,而不需要它完全兼容
当我们不需要完全兼容时,可以仅对对象的接口规范进行测试

```ts
import { expect, test } from 'vitest'
Expand Down Expand Up @@ -828,7 +828,7 @@ test('matches inline snapshot', () => {
})
```

如果我么只是在测试对象的形状,而不需要它 100% 兼容,我们也可以提供一个对象的形状。
当我们不需要完全兼容时,可以仅对对象的接口规范进行测试:

```ts
import { expect, test } from 'vitest'
Expand Down Expand Up @@ -1552,7 +1552,7 @@ test('basket includes fuji', () => {

- **类型:** `(expected: any) => any`

当与相等检查一起使用时,如果值的形状相似,该非对称匹配器将返回 `true`。
当与相等检查一起使用时,如果值的接口规范相似,该非对称匹配器将返回 `true`。

```ts
import { expect, test } from 'vitest'
Expand Down
44 changes: 22 additions & 22 deletions api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ test.runIf(isDev)('dev only test', () => {
- **类型:** `(name: string | Function, fn: TestFunction, timeout?: number) => void`
- **别名:** `it.only`

使用 `test.only` 仅运行给定 测试套件 中的某些测试。这在调试时非常有用。
使用 `test.only` 仅运行给定测试套件中的某些测试。这在调试时非常有用。

可选择提供超时(以毫秒为单位),用于指定终止前的等待时间。默认值为 5 秒,可通过 [testTimeout](/config/#testtimeout) 进行全局配置。

Expand Down Expand Up @@ -284,9 +284,9 @@ describe('suite', () => {

```ts
test.concurrent(/* ... */)
test.skip.concurrent(/* ... */) // or test.concurrent.skip(/* ... */)
test.only.concurrent(/* ... */) // or test.concurrent.only(/* ... */)
test.todo.concurrent(/* ... */) // or test.concurrent.todo(/* ... */)
test.skip.concurrent(/* ... */) // test.concurrent.skip(/* ... */)
test.only.concurrent(/* ... */) // test.concurrent.only(/* ... */)
test.todo.concurrent(/* ... */) // test.concurrent.todo(/* ... */)
```

运行并发测试时,快照和断言必须使用本地 [测试上下文](/guide/test-context.md) 中的 `expect`,以确保检测到正确的测试。
Expand Down Expand Up @@ -376,15 +376,15 @@ test.fails('fail test', async () => {
当需要使用不同变量运行同一测试时,请使用 `test.each`。
我们可以按照测试功能参数的顺序,在测试名称中注入带有 [printf formatting](https://nodejs.org/api/util.html#util_util_format_format_args) 的参数。

- `%s`: string
- `%d`: number
- `%i`: integer
- `%f`: floating point value
- `%j`: json
- `%o`: object
- `%#`: 0-based index of the test case
- `%$`: 1-based index of the test case
- `%%`: single percent sign ('%')
- `%s`:字符串
- `%d`:数字
- `%i`:整数
- `%f`:浮点数
- `%j`:JSON
- `%o`:对象
- `%#`:测试用例从 0 开始的索引
- `%$`:测试用例从 1 开始的索引
- `%%`:百分号字面量(`%`)

```ts
import { expect, test } from 'vitest'
Expand Down Expand Up @@ -480,7 +480,7 @@ Vitest 使用 chai `format` 方法处理 `$values`。如果数值太短,可以

`test.each` 是一种能同时提供 [`TestContext`](/guide/test-context) 的替代用法。

它和 `test.each` 的主要区别在于:当你需要传递数组参数时,二者的写法和处理方式不同。而对于非数组参数(包括模板字符串的用法),`test.each` 和 `test.each` 的使用方法是一致的。
它和 `test.each` 的主要区别在于:当你需要传递数组参数时,二者的写法和处理方式不同。而对于非数组参数(包括模板字符串的用法),`test.for` 和 `test.each` 的使用方法是一致的。

```ts
// `each` 展开数组用例
Expand Down Expand Up @@ -849,7 +849,7 @@ import { assert, describe, test } from 'vitest'

describe.skip('skipped suite', () => {
test('sqrt', () => {
// 套件跳过,没有错误
// 跳过该测试套件,不报错
assert.equal(Math.sqrt(4), 3)
})
})
Expand Down Expand Up @@ -889,7 +889,7 @@ import { assert, describe, test } from 'vitest'
const isDev = process.env.NODE_ENV === 'development'

describe.runIf(isDev)('dev only test suite', () => {
// 此测试套件仅在开发环境中运行
// 此测试套件仅在开发环境中运行
})
```

Expand All @@ -906,15 +906,15 @@ describe.runIf(isDev)('dev only test suite', () => {
```ts
import { assert, describe, test } from 'vitest'

// 只有此测试套件(以及其他标记为 `only` 的测试套件)会被运行
// 只有此测试套件(以及其他标记为 `only` 的测试套件)会被运行
describe.only('suite', () => {
test('sqrt', () => {
assert.equal(Math.sqrt(4), 3)
})
})

describe('other suite', () => {
// ... 将被跳过
// 将被跳过...
})
```

Expand Down Expand Up @@ -1118,7 +1118,7 @@ describe.each`
其他非数组情况(包括模板字符串的使用)的工作方式完全相同。

```ts
// `each` 展开数组用例
// `each` 会展开数组用例
describe.each([
[1, 1, 2],
[1, 2, 3],
Expand Down Expand Up @@ -1161,7 +1161,7 @@ describe.for([
import { beforeEach } from 'vitest'

beforeEach(async () => {
// 每次执行测试前,先重置所有 mock,然后准备好需要用到的测试数据
// 每次执行测试前,先重置所有 mock,然后准备好需要用到的测试数据
await stopMocking()
await addUser({ name: 'John' })
})
Expand All @@ -1175,10 +1175,10 @@ beforeEach(async () => {
import { beforeEach } from 'vitest'

beforeEach(async () => {
// 在每个测试运行之前调用一次
// 在每个测试运行之前调用一次
await prepareSomething()

// 清理函数,在每个测试运行之后调用一次
// 清理函数,在每个测试运行之后调用一次
return async () => {
await resetSomething()
}
Expand Down
Loading
Loading