55` TestCase ` 实例始终有一个值为 ` test ` 的 ` type ` 属性。您可以使用它来区分不同的任务类型:
66
77``` ts
8- if (task .type === " test" ) {
9- task ; // TestCase
8+ if (task .type === ' test' ) {
9+ task // TestCase
1010}
1111```
1212
@@ -23,28 +23,28 @@ if (task.type === "test") {
2323这是传递给 ` test ` 函数的测试名称。
2424
2525``` ts
26- import { test } from " vitest" ;
26+ import { test } from ' vitest'
2727
2828// [!code word:'the validation works correctly']
29- test (" the validation works correctly" , () => {
29+ test (' the validation works correctly' , () => {
3030 // ...
31- });
31+ })
3232```
3333
3434## fullName
3535
3636包括所有父套件并用 ` > ` 符号分隔的测试名称。此测试的完整名称为 "the validation logic > the validation works correctly":
3737
3838``` ts
39- import { describe , test } from " vitest" ;
39+ import { describe , test } from ' vitest'
4040
4141// [!code word:'the validation works correctly']
4242// [!code word:'the validation logic']
43- describe (" the validation logic" , () => {
44- test (" the validation works correctly" , () => {
43+ describe (' the validation logic' , () => {
44+ test (' the validation works correctly' , () => {
4545 // ...
46- });
47- });
46+ })
47+ })
4848```
4949
5050## id
@@ -64,12 +64,12 @@ ID 的格式如下:
6464你可以使用 ` vitest/node ` 中的 ` generateFileHash ` 函数来生成文件哈希,该函数自 Vitest 3 起可用:
6565
6666``` ts
67- import { generateFileHash } from " vitest/node" ;
67+ import { generateFileHash } from ' vitest/node'
6868
6969const hash = generateFileHash (
70- " /file/path.js" , // relative path
70+ ' /file/path.js' , // relative path
7171 undefined // the project name or `undefined` is not set
72- );
72+ )
7373```
7474
7575:::
@@ -100,13 +100,13 @@ test('the validation works correctly', () => {
100100
101101``` ts
102102interface TaskOptions {
103- readonly each: boolean | undefined ;
104- readonly fails: boolean | undefined ;
105- readonly concurrent: boolean | undefined ;
106- readonly shuffle: boolean | undefined ;
107- readonly retry: number | undefined ;
108- readonly repeats: number | undefined ;
109- readonly mode: " run" | " only" | " skip" | " todo" ;
103+ readonly each: boolean | undefined
104+ readonly fails: boolean | undefined
105+ readonly concurrent: boolean | undefined
106+ readonly shuffle: boolean | undefined
107+ readonly retry: number | undefined
108+ readonly repeats: number | undefined
109+ readonly mode: ' run' | ' only' | ' skip' | ' todo'
110110}
111111```
112112
@@ -115,35 +115,35 @@ interface TaskOptions {
115115## ok
116116
117117``` ts
118- function ok(): boolean ;
118+ function ok(): boolean
119119```
120120
121121检查测试是否未使套件失败。如果测试尚未完成或被跳过,它将返回 ` true ` 。
122122
123123## meta
124124
125125` ` ` ts
126- function meta(): TaskMeta ;
126+ function meta(): TaskMeta
127127` ` `
128128
129129在测试执行期间附加到测试的自定义[元数据](/ advanced / metadata )。在测试运行期间,可以通过为 ` ctx.task.meta ` 对象分配一个属性来附加元数据:
130130
131131` ` ` ts {3,6}
132- import { test } from " vitest" ;
132+ import { test } from ' vitest'
133133
134- test (" the validation works correctly" , ({ task }) => {
134+ test(' the validation works correctly' , ({ task }) => {
135135 // ...
136136
137- task .meta .decorated = false ;
138- });
137+ task.meta.decorated = false
138+ })
139139` ` `
140140
141141如果测试尚未完成运行,元数据将是一个空对象。
142142
143143## result
144144
145145` ` ` ts
146- function result(): TestResult ;
146+ function result(): TestResult
147147` ` `
148148
149149测试结果。如果测试尚未完成或刚刚开始收集,等于 ` TestResultPending ` :
@@ -153,11 +153,11 @@ export interface TestResultPending {
153153 /**
154154 * The test was collected, but didn't finish running yet.
155155 */
156- readonly state: " pending" ;
156+ readonly state: ' pending'
157157 /**
158158 * Pending tests have no errors.
159159 */
160- readonly errors: undefined ;
160+ readonly errors: undefined
161161}
162162` ` `
163163
@@ -169,15 +169,15 @@ interface TestResultSkipped {
169169 * The test was skipped with ` skip ` or ` todo ` flag.
170170 * You can see which one was used in the ` options .mode ` option.
171171 */
172- readonly state: " skipped" ;
172+ readonly state: ' skipped'
173173 /**
174174 * Skipped tests have no errors.
175175 */
176- readonly errors: undefined ;
176+ readonly errors: undefined
177177 /**
178178 * A custom note passed down to ` ctx .skip (note )` .
179179 */
180- readonly note: string | undefined ;
180+ readonly note: string | undefined
181181}
182182` ` `
183183
@@ -192,11 +192,11 @@ interface TestResultFailed {
192192 /**
193193 * The test failed to execute.
194194 */
195- readonly state: " failed" ;
195+ readonly state: ' failed'
196196 /**
197197 * Errors that were thrown during the test execution.
198198 */
199- readonly errors: ReadonlyArray <TestError >;
199+ readonly errors: ReadonlyArray<TestError>
200200}
201201` ` `
202202
@@ -207,11 +207,11 @@ interface TestResultPassed {
207207 /**
208208 * The test passed successfully.
209209 */
210- readonly state: " passed" ;
210+ readonly state: ' passed'
211211 /**
212212 * Errors that were thrown during the test execution.
213213 */
214- readonly errors: ReadonlyArray <TestError > | undefined ;
214+ readonly errors: ReadonlyArray<TestError> | undefined
215215}
216216` ` `
217217
@@ -222,7 +222,7 @@ interface TestResultPassed {
222222## diagnostic
223223
224224` ` ` ts
225- function diagnostic(): TestDiagnostic | undefined ;
225+ function diagnostic(): TestDiagnostic | undefined
226226` ` `
227227
228228有关测试的有用信息,例如持续时间、内存使用等:
@@ -232,33 +232,33 @@ interface TestDiagnostic {
232232 /**
233233 * If the duration of the test is above ` slowTestThreshold ` .
234234 */
235- readonly slow: boolean ;
235+ readonly slow: boolean
236236 /**
237237 * The amount of memory used by the test in bytes.
238238 * This value is only available if the test was executed with ` logHeapUsage ` flag.
239239 */
240- readonly heap: number | undefined ;
240+ readonly heap: number | undefined
241241 /**
242242 * The time it takes to execute the test in ms.
243243 */
244- readonly duration: number ;
244+ readonly duration: number
245245 /**
246246 * The time in ms when the test started.
247247 */
248- readonly startTime: number ;
248+ readonly startTime: number
249249 /**
250250 * The amount of times the test was retried.
251251 */
252- readonly retryCount: number ;
252+ readonly retryCount: number
253253 /**
254254 * The amount of times the test was repeated as configured by ` repeats ` option.
255255 * This value can be lower if the test failed during the repeat and no ` retry ` is configured.
256256 */
257- readonly repeatCount: number ;
257+ readonly repeatCount: number
258258 /**
259259 * If test passed on a second retry.
260260 */
261- readonly flaky: boolean ;
261+ readonly flaky: boolean
262262}
263263` ` `
264264
0 commit comments