55` TestModule ` 实例始终具有一个 ` type ` 属性,其值为 ` module ` 。你可以使用它来区分不同的任务类型:
66
77``` ts
8- if (task .type === ' module' ) {
9- task // TestModule
8+ if (task .type === " module" ) {
9+ task ; // TestModule
1010}
1111```
1212
@@ -19,50 +19,50 @@ if (task.type === 'module') {
1919这通常是一个绝对的 Unix 文件路径(即使在 Windows 上也是如此)。如果文件不在磁盘上,它可以是一个虚拟 ID。此值对应于 Vite 的 ` ModuleGraph ` ID。
2020
2121``` ts
22- ' C:/Users/Documents/project/example.test.ts' // ✅
23- ' /Users/mac/project/example.test.ts' // ✅
24- ' C:\\ Users\\ Documents\\ project\\ example.test.ts' // ❌
22+ " C:/Users/Documents/project/example.test.ts" ; // ✅
23+ " /Users/mac/project/example.test.ts" ; // ✅
24+ " C:\\ Users\\ Documents\\ project\\ example.test.ts" ; // ❌
2525```
2626
2727## relativeModuleId
2828
29- Module id relative to the project. This is the same as ` task.name ` in the deprecated API.
29+ 相对于项目的模块 ID。这与已弃用 API 中的 ` task.name ` 相同。
3030
3131``` ts
32- ' project/example.test.ts' // ✅
33- ' example.test.ts' // ✅
34- ' project\\ example.test.ts' // ❌
32+ " project/example.test.ts" ; // ✅
33+ " example.test.ts" ; // ✅
34+ " project\\ example.test.ts" ; // ❌
3535```
3636
3737## state
3838
3939``` ts
40- function state(): TestModuleState
40+ function state(): TestModuleState ;
4141```
4242
4343与 [ ` testSuite.state() ` ] ( /advanced/api/test-suite#state ) 的工作方式相同,但如果模块尚未执行,还可以返回 ` queued ` 。
4444
4545## meta <Version >3.1.0</Version > {#meta}
4646
4747``` ts
48- function meta(): TaskMeta
48+ function meta(): TaskMeta ;
4949```
5050
5151在模块执行或收集过程中附加到模块的自定义[ 元数据] ( /advanced/metadata ) 。在测试运行期间,可以通过向 ` task.meta ` 对象分配属性来附加 meta:
5252
5353``` ts {5,10}
54- import { test } from ' vitest'
55-
56- describe(' the validation works correctly' , (task) => {
57- // assign "decorated" during collection
58- task.file.meta.decorated = false
59-
60- test(' some test' , ({ task }) => {
61- // assign "decorated" during test run, it will be available
62- // only in onTestCaseReady hook
63- task.file.meta.decorated = false
64- })
65- })
54+ import { test } from " vitest" ;
55+
56+ describe (" the validation works correctly" , (task ) => {
57+ // 在收集阶段分配 "decorated"
58+ task .file .meta .decorated = false ;
59+
60+ test (" some test" , ({ task }) => {
61+ // 在测试运行期间分配 "decorated",它将可用
62+ // 仅在 onTestCaseReady 钩子中
63+ task .file .meta .decorated = false ;
64+ });
65+ });
6666```
6767
6868::: tip
@@ -72,51 +72,51 @@ describe('the validation works correctly', (task) => {
7272## diagnostic
7373
7474``` ts
75- function diagnostic(): ModuleDiagnostic
75+ function diagnostic(): ModuleDiagnostic ;
7676```
7777
7878关于模块的有用信息,例如持续时间、内存使用等。如果模块尚未执行,所有诊断值将返回 ` 0 ` 。
7979
8080``` ts
8181interface ModuleDiagnostic {
8282 /**
83- * The time it takes to import and initiate an environment.
83+ * 导入和初始化环境所需的时间。
8484 */
85- readonly environmentSetupDuration: number
85+ readonly environmentSetupDuration: number ;
8686 /**
87- * The time it takes Vitest to setup test harness (runner, mocks, etc.).
87+ * Vitest 设置测试运行环境(运行器、模拟等)所需的时间。
8888 */
89- readonly prepareDuration: number
89+ readonly prepareDuration: number ;
9090 /**
91- * The time it takes to import the test module.
92- * This includes importing everything in the module and executing suite callbacks.
91+ * 导入测试模块所需的时间。
92+ * 这包括导入模块中的所有内容以及执行套件回调函数。
9393 */
94- readonly collectDuration: number
94+ readonly collectDuration: number ;
9595 /**
96- * The time it takes to import the setup module.
96+ * 导入设置模块所需的时间。
9797 */
98- readonly setupDuration: number
98+ readonly setupDuration: number ;
9999 /**
100- * Accumulated duration of all tests and hooks in the module.
100+ * 模块中所有测试和钩子函数的累计持续时间。
101101 */
102- readonly duration: number
102+ readonly duration: number ;
103103 /**
104- * The amount of memory used by the module in bytes.
105- * This value is only available if the test was executed with ` logHeapUsage ` flag.
104+ * 模块使用的内存量(以字节为单位)。
105+ * 此值仅在使用 `logHeapUsage` 标志执行测试时才可用。
106106 */
107- readonly heap: number | undefined
107+ readonly heap: number | undefined ;
108108 /**
109- * The time spent importing every non-externalized dependency that Vitest has processed.
109+ * Vitest处理的每个非外部化依赖项的导入时间。
110110 */
111- readonly importDurations: Record<string, ImportDuration>
111+ readonly importDurations: Record <string , ImportDuration >;
112112}
113113
114- /** The time spent importing & executing a non-externalized file. */
114+ /** 导入和执行非外部化文件所花费的时间。 */
115115interface ImportDuration {
116- /** The time spent importing & executing the file itself, not counting all non-externalized imports that the file does. */
117- selfTime: number
116+ /** 导入和执行文件本身所花费的时间,不包括该文件所依赖的所有非外部化导入。 */
117+ selfTime: number ;
118118
119- /** The time spent importing & executing the file and all its imports. */
120- totalTime: number
119+ /** 导入和执行文件及其所有导入项所花费的时间。 */
120+ totalTime: number ;
121121}
122122```
0 commit comments