Skip to content

Commit e79cdf6

Browse files
authored
Merge pull request #965 from vitest-dev/sync-ec964f36-1
docs(en): merge docs-cn/sync-docs into docs-cn/dev @ ec964f3
2 parents a981f33 + 484c532 commit e79cdf6

27 files changed

Lines changed: 638 additions & 108 deletions

.vitepress/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,11 @@ export default ({ mode }: { mode: string }) => {
807807
link: '/guide/browser/trace-view',
808808
docFooterText: '追踪查看器 | 浏览器模式',
809809
},
810+
{
811+
text: 'Playwright Traces',
812+
link: '/guide/browser/playwright-traces',
813+
docFooterText: 'Playwright Traces | Browser Mode',
814+
},
810815
{
811816
text: 'ARIA Snapshots',
812817
link: '/guide/browser/aria-snapshots',

api/advanced/vitest.md

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function start(filters?: string[]): Promise<TestRunResult>
244244
function standalone(): Promise<void>
245245
```
246246

247-
- **别名:**: `init` <Deprecated />
247+
- **别名:** `init` <Deprecated />
248248

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

@@ -696,3 +696,120 @@ export interface SourceModuleDiagnostic {
696696
::: warning
697697
[浏览器模式](/guide/browser/) 暂不支持。
698698
:::
699+
<!-- TODO: translation -->
700+
## createReport <Version>5.0.0</Version> {#createreport}
701+
702+
```ts
703+
function createReport(scope: string): Report
704+
```
705+
706+
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).
707+
708+
`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.
709+
710+
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`.
711+
712+
```ts
713+
import type { Report } from 'vitest/node'
714+
715+
const scope = 'example-yaml-reporter'
716+
717+
// Automatically creates `<project-root>/.vitest/example-yaml-reporter/`
718+
// directory if it does not exist already
719+
const report: Report = vitest.createReport(scope)
720+
```
721+
722+
### Report.root
723+
724+
```ts
725+
const root: string
726+
```
727+
728+
The root directory for this scope.
729+
730+
```ts
731+
const report = vitest.createReport('my-json-reporter')
732+
733+
// Is <project-root>/.vitest/my-json-reporter
734+
const root = report.root
735+
```
736+
737+
### Report.clean
738+
739+
```ts
740+
function clean(): Promise<void>
741+
```
742+
743+
Clean up the report directory for this scope.
744+
745+
```ts
746+
const report = vitest.createReport('my-json-reporter')
747+
748+
// Removes everything inside <project-root>/.vitest/my-json-reporter/
749+
await report.clean()
750+
```
751+
752+
### Report.writeFile
753+
754+
```ts
755+
function writeFile(
756+
filename: string,
757+
content: string | Uint8Array,
758+
encoding?: BufferEncoding
759+
): Promise<void>
760+
```
761+
762+
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.
763+
764+
```ts
765+
const report = vitest.createReport('my-json-reporter')
766+
767+
// Writes file to .vitest/my-json-reporter/test-report.json
768+
await report.writeFile('test-report.json', JSON.stringify(results))
769+
```
770+
771+
### Report.readFile
772+
773+
```ts
774+
function readFile(filename: string, encoding?: BufferEncoding): Promise<string>
775+
```
776+
777+
Read a file from the report directory for this scope.
778+
779+
```ts
780+
const report = vitest.createReport('my-json-reporter')
781+
782+
// Reads file from .vitest/my-json-reporter/test-report.json
783+
const content: string = await report.readFile('test-report.json')
784+
```
785+
786+
### Report.readdir
787+
788+
```ts
789+
function readdir(): Promise<string[]>
790+
```
791+
792+
Read contents of the report directory for this scope.
793+
794+
```ts
795+
const report = vitest.createReport('my-json-reporter')
796+
797+
// Reads contents from .vitest/my-json-reporter
798+
const filenames: string[] = await report.readdir()
799+
```
800+
801+
### Report.delete
802+
803+
<!-- eslint-skip -->
804+
```ts
805+
function delete(filename: string): Promise<void>
806+
```
807+
808+
Delete a file from the report directory for this scope.
809+
810+
```ts
811+
const report = vitest.createReport('my-json-reporter')
812+
813+
// Deletes file from .vitest/my-json-reporter/test-report.json
814+
await report.delete('test-report.json')
815+
```

api/assert.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ test('assert.fail', () => {
3636
## isOk
3737

3838
- **类型:** `<T>(value: T, message?: string) => asserts value`
39-
- **Alias** `ok`
39+
- **别名:** `ok`
4040

4141
断言给定的 `value` 是 true 。
4242

@@ -52,7 +52,7 @@ test('assert.isOk', () => {
5252
## isNotOk
5353

5454
- **类型:** `<T>(value: T, message?: string) => void`
55-
- **Alias** `notOk`
55+
- **别名:** `notOk`
5656

5757
断言给定的 `value` 是 false 。
5858

api/browser/context.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ export const utils: {
267267
* Creates "Cannot find element" error. Useful for custom locators.
268268
*/
269269
getElementError(selector: string, container?: Element): Error
270+
/**
271+
* Utilities for generating and working with ARIA trees and templates.
272+
* @experimental
273+
*/
274+
aria: {
275+
generateAriaTree(rootElement: Element): AriaNode
276+
renderAriaTree(root: AriaNode): string
277+
renderAriaTemplate(template: AriaTemplateNode): string
278+
parseAriaTemplate(text: string): AriaTemplateNode
279+
matchAriaTree(root: AriaNode, template: AriaTemplateNode): { pass: boolean; resolved: string }
280+
}
270281
}
271282
```
272283

@@ -337,3 +348,22 @@ utils.configurePrettyDOM({
337348
::: tip
338349
This feature is inspired by Testing Library's [`defaultIgnore`](https://testing-library.com/docs/dom-testing-library/api-configuration/#defaultignore) configuration.
339350
:::
351+
352+
### aria <Version type="experimental">5.0.0</Version> {#aria}
353+
354+
The `aria` namespace exposes low-level utilities used by Vitest's ARIA snapshot matchers.
355+
356+
```ts
357+
import { utils } from 'vitest/browser'
358+
359+
document.body.innerHTML = `
360+
<h1>Hello, World!</h1>
361+
<button aria-hidden="true">Hidden</button>
362+
<button>Visible</button>
363+
`
364+
const tree = utils.aria.generateAriaTree(document.body)
365+
const yaml = utils.aria.renderAriaNode(tree)
366+
console.log(yaml)
367+
// - heading "Hello, World!" [level=1]
368+
// - button "Visible""
369+
```

api/expect.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ test('spy function', () => {
10771077

10781078
## toHaveBeenCalledTimes
10791079

1080-
- **类型**: `(amount: number) => Awaitable<void>`
1080+
- **类型:** `(amount: number) => Awaitable<void>`
10811081

10821082
这个断言检查函数是否被调用了特定次数。需要将一个 spy 函数传递给 `expect`
10831083

@@ -1102,7 +1102,7 @@ test('spy function called two times', () => {
11021102

11031103
## toHaveBeenCalledWith
11041104

1105-
- **类型**: `(...args: any[]) => Awaitable<void>`
1105+
- **类型:** `(...args: any[]) => Awaitable<void>`
11061106

11071107
这个断言检查函数是否至少一次被调用,并带有特定的参数。需要将一个 spy 函数传递给 `expect`
11081108

@@ -1128,7 +1128,7 @@ test('spy function', () => {
11281128

11291129
## toHaveBeenCalledBefore
11301130

1131-
- **类型**: `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable<void>`
1131+
- **类型:** `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable<void>`
11321132

11331133
这个断言检查一个 `Mock` 是否在另一个 `Mock` 之前被调用。
11341134

@@ -1147,7 +1147,7 @@ test('calls mock1 before mock2', () => {
11471147

11481148
## toHaveBeenCalledAfter
11491149

1150-
- **类型**: `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable<void>`
1150+
- **类型:** `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable<void>`
11511151

11521152
这个断言检查一个 `Mock` 是否在另一个 `Mock` 之后被调用。
11531153

@@ -1166,7 +1166,7 @@ test('calls mock1 after mock2', () => {
11661166

11671167
## toHaveBeenCalledExactlyOnceWith
11681168

1169-
- **类型**: `(...args: any[]) => Awaitable<void>`
1169+
- **类型:** `(...args: any[]) => Awaitable<void>`
11701170

11711171
这个断言检查函数是否恰好被调用了一次,并且带有特定的参数。需要将一个 spy 函数传递给 `expect`
11721172

@@ -1190,7 +1190,7 @@ test('spy function', () => {
11901190

11911191
## toHaveBeenLastCalledWith
11921192

1193-
- **类型**: `(...args: any[]) => Awaitable<void>`
1193+
- **类型:** `(...args: any[]) => Awaitable<void>`
11941194

11951195
这个断言检查函数在其最后一次调用时是否被传入了特定的参数。需要将一个 spy 函数传递给 `expect`
11961196

@@ -1216,7 +1216,7 @@ test('spy function', () => {
12161216

12171217
## toHaveBeenNthCalledWith
12181218

1219-
- **类型**: `(time: number, ...args: any[]) => Awaitable<void>`
1219+
- **类型:** `(time: number, ...args: any[]) => Awaitable<void>`
12201220

12211221
这个断言检查函数是否在特定的次数被调用时带有特定的参数。计数从 1 开始。因此,要检查第二次调用,我们需要写成 `.toHaveBeenNthCalledWith(2, ...)`
12221222

@@ -1243,7 +1243,7 @@ test('first call of spy function called with right params', () => {
12431243

12441244
## toHaveReturned
12451245

1246-
- **类型**: `() => Awaitable<void>`
1246+
- **类型:** `() => Awaitable<void>`
12471247

12481248
这个断言检查函数是否至少成功返回了一次值( i.e. ,没有抛出错误)。需要将一个 spy 函数传递给 `expect`
12491249

@@ -1267,7 +1267,7 @@ test('spy function returned a value', () => {
12671267

12681268
## toHaveReturnedTimes
12691269

1270-
- **类型**: `(amount: number) => Awaitable<void>`
1270+
- **类型:** `(amount: number) => Awaitable<void>`
12711271

12721272
这个断言检查函数是否在确切的次数内成功返回了值( i.e. ,没有抛出错误)。需要将一个 spy 函数传递给 `expect`
12731273

@@ -1286,7 +1286,7 @@ test('spy function returns a value two times', () => {
12861286

12871287
## toHaveReturnedWith
12881288

1289-
- **类型**: `(returnValue: any) => Awaitable<void>`
1289+
- **类型:** `(returnValue: any) => Awaitable<void>`
12901290

12911291
我们可以调用这个断言来检查函数是否至少一次成功返回了带有特定参数的值。需要将一个 spy 函数传递给 `expect`
12921292

@@ -1304,7 +1304,7 @@ test('spy function returns a product', () => {
13041304

13051305
## toHaveLastReturnedWith
13061306

1307-
- **类型**: `(returnValue: any) => Awaitable<void>`
1307+
- **类型:** `(returnValue: any) => Awaitable<void>`
13081308

13091309
我们可以使用这个断言来检查函数在最后一次被调用时是否成功返回了特定的值。需要将一个 spy 函数传递给 `expect`
13101310

@@ -1323,7 +1323,7 @@ test('spy function returns bananas on a last call', () => {
13231323

13241324
## toHaveNthReturnedWith
13251325

1326-
- **类型**: `(time: number, returnValue: any) => Awaitable<void>`
1326+
- **类型:** `(time: number, returnValue: any) => Awaitable<void>`
13271327

13281328
我们可以调用这个断言来检查函数是否在特定的调用中成功返回了带有特定参数的值。需要将一个 spy 函数传递给 `expect`
13291329

@@ -1344,7 +1344,7 @@ test('spy function returns bananas on second call', () => {
13441344

13451345
## toHaveResolved
13461346

1347-
- **类型**: `() => Awaitable<void>`
1347+
- **类型:** `() => Awaitable<void>`
13481348

13491349
这个断言检查函数是否至少一次成功地解析了一个值( i.e. ,没有被拒绝)。需要将一个 spy 函数传递给 `expect`
13501350

@@ -1370,7 +1370,7 @@ test('spy function resolved a value', async () => {
13701370

13711371
## toHaveResolvedTimes
13721372

1373-
- **类型**: `(amount: number) => Awaitable<void>`
1373+
- **类型:** `(amount: number) => Awaitable<void>`
13741374

13751375
此断言检查函数是否已成功解析值精确次数(即未 reject)。需要将 spy 函数传递给`expect`
13761376

api/test.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,9 @@ test.each`
614614
expect(a + b).toBe(expected)
615615
})
616616
```
617-
617+
<!-- TODO: translation -->
618618
::: tip
619-
Vitest 使用 Chai 的 `format` 方法处理 `$values`。如果值被截断,可在配置文件中调大 [chaiConfig.truncateThreshold](/config/chaiconfig#chaiconfig-truncatethreshold)
619+
Vitest formats interpolated title values with its display formatter. If the value is too truncated, you can increase [taskTitleValueFormatTruncate](/config/tasktitlevalueformattruncate) in your config file.
620620
:::
621621

622622
## test.for

config/allowonly.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ outline: deep
55

66
# allowOnly
77

8-
- **类型:**: `boolean`
9-
- **默认值:**: `!process.env.CI`
8+
- **类型:** `boolean`
9+
- **默认值:** `!process.env.CI`
1010
- **命令行终端:** `--allowOnly`, `--allowOnly=false`
1111

1212
默认情况下,Vitest 不允许在持续集成(CI)环境中运行带有 [`only`](/api/test#test-only) 标记的测试。相反,在本地开发环境中,Vitest 允许运行这些测试。

config/browser/trace.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ outline: deep
1010
- **默认值:** `'off'`
1111

1212
捕获浏览器测试运行的追踪记录。你可以通过 [Playwright Trace Viewer](https://trace.playwright.dev/) 预览追踪文件。
13+
<!-- TODO: translation -->
14+
See [Playwright Traces](/guide/browser/playwright-traces) for the full workflow.
1315

1416
该选项支持以下取值:
1517

0 commit comments

Comments
 (0)