Skip to content

Commit a46d607

Browse files
sheremet-vawatonyweng
authored andcommitted
docs: show both test options examples (#7786)
1 parent d462a0b commit a46d607

File tree

1 file changed

+81
-17
lines changed

1 file changed

+81
-17
lines changed

api/index.md

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,67 @@ interface TestOptions {
3939

4040
:::
4141

42-
大多数选项都支持点语法和对象语法,允许我们使用我们喜欢的任何样式。
42+
你可以通过链接函数的属性来定义选项:
4343

44-
:::code-group
45-
```ts [dot-syntax]
44+
```ts
4645
import { test } from 'vitest'
4746

4847
test.skip('skipped test', () => {
4948
// 一些现在失败的逻辑
5049
})
50+
51+
test.concurrent.skip('skipped concurrent test', () => {
52+
// 一些现在失败的逻辑
53+
})
5154
```
52-
```ts [object-syntax]
55+
56+
但你也可以提供一个对象作为第二个参数:
57+
58+
```ts
5359
import { test } from 'vitest'
5460

5561
test('skipped test', { skip: true }, () => {
5662
// 一些现在失败的逻辑
5763
})
64+
65+
test('skipped concurrent test', { skip: true, concurrent: true }, () => {
66+
// some logic that fails right now
67+
})
5868
```
5969

60-
:::
70+
两者的工作方式完全相同。使用其中任何一种纯粹是个人风格选择。
71+
72+
请注意,如果你将超时设置为最后一个参数,则不能再使用选项:
73+
74+
```ts
75+
import { test } from 'vitest'
76+
77+
// ✅ this works
78+
test.skip('heavy test', () => {
79+
// ...
80+
}, 10_000)
81+
82+
// ❌ this doesn't work
83+
test(
84+
'heavy test',
85+
{ skip: true },
86+
() => {
87+
// ...
88+
},
89+
10_000
90+
)
91+
```
92+
93+
但是,你可以在对象内提供超时:
94+
95+
```ts
96+
import { test } from 'vitest'
97+
98+
// ✅ this works
99+
test('heavy test', { skip: true, timeout: 10_000 }, () => {
100+
// ...
101+
})
102+
```
61103

62104
## test
63105

@@ -344,7 +386,7 @@ test.fails('fail test', async () => {
344386
- **别名:** `it.each`
345387

346388
::: tip
347-
`test.each` 是为了与 Jest 兼容而提供的,Vitest 还提供了 [`test.for`](#test-for),并集成了 [`TestContext`](/guide/test-context)
389+
`test.each` 是为了与 Jest 兼容而提供的,Vitest 还提供了 [`test.for`](#test-for),并集成了 [`TestContext`](/guide/test-context)
348390
:::
349391

350392
当需要使用不同变量运行同一测试时,请使用 `test.each`
@@ -464,7 +506,8 @@ test.each([
464506
[1, 1, 2],
465507
[1, 2, 3],
466508
[2, 1, 3],
467-
])('add(%i, %i) -> %i', (a, b, expected) => { // [!code --]
509+
])('add(%i, %i) -> %i', (a, b, expected) => {
510+
// [!code --]
468511
expect(a + b).toBe(expected)
469512
})
470513

@@ -473,7 +516,8 @@ test.for([
473516
[1, 1, 2],
474517
[1, 2, 3],
475518
[2, 1, 3],
476-
])('add(%i, %i) -> %i', ([a, b, expected]) => { // [!code ++]
519+
])('add(%i, %i) -> %i', ([a, b, expected]) => {
520+
// [!code ++]
477521
expect(a + b).toBe(expected)
478522
})
479523
```
@@ -565,12 +609,14 @@ export interface Options {
565609
teardown?: Hook
566610
}
567611
```
612+
568613
测试用例运行后,输出结构信息如下:
569614

570615
```
571616
name hz min max mean p75 p99 p995 p999 rme samples
572617
· normal sorting 6,526,368.12 0.0001 0.3638 0.0002 0.0002 0.0002 0.0002 0.0004 ±1.41% 652638
573618
```
619+
574620
```ts
575621
export interface TaskResult {
576622
/*
@@ -990,20 +1036,34 @@ import { describe, test } from 'vitest'
9901036

9911037
// 或 `describe('suite', { shuffle: true }, ...)`
9921038
describe.shuffle('suite', () => {
993-
test('random test 1', async () => { /* ... */ })
994-
test('random test 2', async () => { /* ... */ })
995-
test('random test 3', async () => { /* ... */ })
1039+
test('random test 1', async () => {
1040+
/* ... */
1041+
})
1042+
test('random test 2', async () => {
1043+
/* ... */
1044+
})
1045+
test('random test 3', async () => {
1046+
/* ... */
1047+
})
9961048

9971049
// `shuffle` 是继承的
9981050
describe('still random', () => {
999-
test('random 4.1', async () => { /* ... */ })
1000-
test('random 4.2', async () => { /* ... */ })
1051+
test('random 4.1', async () => {
1052+
/* ... */
1053+
})
1054+
test('random 4.2', async () => {
1055+
/* ... */
1056+
})
10011057
})
10021058

10031059
// 禁用内部的 shuffle
10041060
describe('not random', { shuffle: false }, () => {
1005-
test('in order 5.1', async () => { /* ... */ })
1006-
test('in order 5.2', async () => { /* ... */ })
1061+
test('in order 5.1', async () => {
1062+
/* ... */
1063+
})
1064+
test('in order 5.2', async () => {
1065+
/* ... */
1066+
})
10071067
})
10081068
})
10091069
// 顺序取决于配置中的 `sequence.seed` 选项(默认为 `Date.now()`)
@@ -1098,7 +1158,8 @@ describe.each([
10981158
[1, 1, 2],
10991159
[1, 2, 3],
11001160
[2, 1, 3],
1101-
])('add(%i, %i) -> %i', (a, b, expected) => { // [!code --]
1161+
])('add(%i, %i) -> %i', (a, b, expected) => {
1162+
// [!code --]
11021163
test('test', () => {
11031164
expect(a + b).toBe(expected)
11041165
})
@@ -1109,7 +1170,8 @@ describe.for([
11091170
[1, 1, 2],
11101171
[1, 2, 3],
11111172
[2, 1, 3],
1112-
])('add(%i, %i) -> %i', ([a, b, expected]) => { // [!code ++]
1173+
])('add(%i, %i) -> %i', ([a, b, expected]) => {
1174+
// [!code ++]
11131175
test('test', () => {
11141176
expect(a + b).toBe(expected)
11151177
})
@@ -1297,6 +1359,7 @@ test('performs an organization query', async () => {
12971359
此 hook 始终以相反的顺序调用,并且不受 [`sequence.hooks`](/config/#sequence-hooks) 选项的影响。
12981360

12991361
<!-- TODO: should it be called? https://github.com/vitest-dev/vitest/pull/7069 -->
1362+
13001363
请注意,如果测试是通过动态 `ctx.skip()` 调用跳过的,则不会调用此钩子。:
13011364

13021365
```ts{2}
@@ -1305,6 +1368,7 @@ test('skipped dynamically', (t) => {
13051368
t.skip()
13061369
})
13071370
```
1371+
13081372
:::
13091373

13101374
### onTestFailed

0 commit comments

Comments
 (0)