@@ -39,25 +39,67 @@ interface TestOptions {
39
39
40
40
:::
41
41
42
- 大多数选项都支持点语法和对象语法,允许我们使用我们喜欢的任何样式。
42
+ 你可以通过链接函数的属性来定义选项:
43
43
44
- ::: code-group
45
- ``` ts [dot-syntax]
44
+ ``` ts
46
45
import { test } from ' vitest'
47
46
48
47
test .skip (' skipped test' , () => {
49
48
// 一些现在失败的逻辑
50
49
})
50
+
51
+ test .concurrent .skip (' skipped concurrent test' , () => {
52
+ // 一些现在失败的逻辑
53
+ })
51
54
```
52
- ``` ts [object-syntax]
55
+
56
+ 但你也可以提供一个对象作为第二个参数:
57
+
58
+ ``` ts
53
59
import { test } from ' vitest'
54
60
55
61
test (' skipped test' , { skip: true }, () => {
56
62
// 一些现在失败的逻辑
57
63
})
64
+
65
+ test (' skipped concurrent test' , { skip: true , concurrent: true }, () => {
66
+ // some logic that fails right now
67
+ })
58
68
```
59
69
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
+ ```
61
103
62
104
## test
63
105
@@ -344,7 +386,7 @@ test.fails('fail test', async () => {
344
386
- ** 别名:** ` it.each `
345
387
346
388
::: 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 ) 。
348
390
:::
349
391
350
392
当需要使用不同变量运行同一测试时,请使用 ` test.each ` 。
@@ -464,7 +506,8 @@ test.each([
464
506
[1 , 1 , 2 ],
465
507
[1 , 2 , 3 ],
466
508
[2 , 1 , 3 ],
467
- ])(' add(%i, %i) -> %i' , (a , b , expected ) => { // [!code --]
509
+ ])(' add(%i, %i) -> %i' , (a , b , expected ) => {
510
+ // [!code --]
468
511
expect (a + b ).toBe (expected )
469
512
})
470
513
@@ -473,7 +516,8 @@ test.for([
473
516
[1 , 1 , 2 ],
474
517
[1 , 2 , 3 ],
475
518
[2 , 1 , 3 ],
476
- ])(' add(%i, %i) -> %i' , ([a , b , expected ]) => { // [!code ++]
519
+ ])(' add(%i, %i) -> %i' , ([a , b , expected ]) => {
520
+ // [!code ++]
477
521
expect (a + b ).toBe (expected )
478
522
})
479
523
```
@@ -565,12 +609,14 @@ export interface Options {
565
609
teardown? : Hook
566
610
}
567
611
```
612
+
568
613
测试用例运行后,输出结构信息如下:
569
614
570
615
```
571
616
name hz min max mean p75 p99 p995 p999 rme samples
572
617
· normal sorting 6,526,368.12 0.0001 0.3638 0.0002 0.0002 0.0002 0.0002 0.0004 ±1.41% 652638
573
618
```
619
+
574
620
``` ts
575
621
export interface TaskResult {
576
622
/*
@@ -990,20 +1036,34 @@ import { describe, test } from 'vitest'
990
1036
991
1037
// 或 `describe('suite', { shuffle: true }, ...)`
992
1038
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
+ })
996
1048
997
1049
// `shuffle` 是继承的
998
1050
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
+ })
1001
1057
})
1002
1058
1003
1059
// 禁用内部的 shuffle
1004
1060
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
+ })
1007
1067
})
1008
1068
})
1009
1069
// 顺序取决于配置中的 `sequence.seed` 选项(默认为 `Date.now()`)
@@ -1098,7 +1158,8 @@ describe.each([
1098
1158
[1 , 1 , 2 ],
1099
1159
[1 , 2 , 3 ],
1100
1160
[2 , 1 , 3 ],
1101
- ])(' add(%i, %i) -> %i' , (a , b , expected ) => { // [!code --]
1161
+ ])(' add(%i, %i) -> %i' , (a , b , expected ) => {
1162
+ // [!code --]
1102
1163
test (' test' , () => {
1103
1164
expect (a + b ).toBe (expected )
1104
1165
})
@@ -1109,7 +1170,8 @@ describe.for([
1109
1170
[1 , 1 , 2 ],
1110
1171
[1 , 2 , 3 ],
1111
1172
[2 , 1 , 3 ],
1112
- ])(' add(%i, %i) -> %i' , ([a , b , expected ]) => { // [!code ++]
1173
+ ])(' add(%i, %i) -> %i' , ([a , b , expected ]) => {
1174
+ // [!code ++]
1113
1175
test (' test' , () => {
1114
1176
expect (a + b ).toBe (expected )
1115
1177
})
@@ -1297,6 +1359,7 @@ test('performs an organization query', async () => {
1297
1359
此 hook 始终以相反的顺序调用,并且不受 [ ` sequence.hooks ` ] ( /config/#sequence-hooks ) 选项的影响。
1298
1360
1299
1361
<!-- TODO: should it be called? https://github.com/vitest-dev/vitest/pull/7069 -->
1362
+
1300
1363
请注意,如果测试是通过动态 ` ctx.skip() ` 调用跳过的,则不会调用此钩子。:
1301
1364
1302
1365
``` ts{2}
@@ -1305,6 +1368,7 @@ test('skipped dynamically', (t) => {
1305
1368
t.skip()
1306
1369
})
1307
1370
```
1371
+
1308
1372
:::
1309
1373
1310
1374
### onTestFailed
0 commit comments