@@ -11,13 +11,23 @@ outline: deep
1111
1212一次完整的 Vitest 测试运行通常经历以下几个主要阶段:
1313
14+ <<<<<<< HEAD
14151 . ** 初始化:** 加载配置并初始化项目
15162 . ** 全局初始化:** 在所有测试运行之前执行一次初始化
16173 . ** 创建 Worker:** 根据 [ pool] ( /config/pool ) 配置创建测试 Worker
17184 . ** 收集测试文件:** 发现并整理测试文件
18195 . ** 执行测试:** 运行测试及其钩子和断言
19206 . ** 报告:** 收集并输出测试结果
20217 . ** 全局清理:** 所有测试完成后执行最终清理
22+ =======
23+ 1 . ** Initialization:** Configuration loading and project setup
24+ 2 . ** Global Setup:** One-time setup before any tests run
25+ 3 . ** Worker Creation:** Test workers are spawned based on the [ pool] ( /config/pool ) configuration
26+ 4 . ** Test File Collection:** Test files are discovered and organized
27+ 5 . ** Test Execution:** Tests run with their hooks and assertions
28+ 6 . ** Reporting:** Results are collected and reported
29+ 7 . ** Global Teardown:** Final cleanup after all tests complete
30+ >>>>>>> c0fcb140c1c79f419ea6578e689607cdf4218036
2131
2232第 4–6 阶段针对每个测试文件各执行一次,因此在整个测试套件中会执行多次;如果你使用了多于 [ 1 个 worker] ( /config/maxworkers ) ,这些阶段还会在不同文件间并行执行。
2333
@@ -124,6 +134,7 @@ afterEach(() => {
124134
125135执行顺序如下:
126136
137+ <<<<<<< HEAD
1271381 . ** 文件级代码:** ` describe ` 块外的所有代码立即执行
1281392 . ** 测试收集:** 处理 ` describe ` 块,导入测试文件时以副作用的形式注册测试
1291403 . ** [ ` aroundAll ` ] ( /api/hooks#aroundall ) 钩子:** 包裹套件中的所有测试(须调用 ` runSuite() ` )
@@ -137,6 +148,21 @@ afterEach(() => {
137148 - 如果测试失败:[ ` onTestFailed ` ] ( /api/hooks#ontestfailed ) 回调执行
138149 - 注意:如果设置了 ` repeats ` 或 ` retry ` ,上述所有步骤会再次执行
1391506 . ** [ ` afterAll ` ] ( /api/hooks#afterall ) 钩子:** 套件中所有测试完成后执行一次
151+ =======
152+ 1 . ** File-level code:** All code outside ` describe ` blocks runs immediately
153+ 2 . ** Test collection:** ` describe ` blocks are processed, and tests are registered as side effects of importing the test file
154+ 3 . ** [ ` aroundAll ` ] ( /api/hooks#aroundall ) hooks:** Wrap around all tests in the suite (must call ` runSuite() ` )
155+ 4 . ** [ ` beforeAll ` ] ( /api/hooks#beforeall ) hooks:** Run once before any tests in the suite
156+ 5 . ** For each test:**
157+ - [ ` aroundEach ` ] ( /api/hooks#aroundeach ) hooks wrap around the test (must call ` runTest() ` )
158+ - ` beforeEach ` hooks execute (in order defined, or based on [ ` sequence.hooks ` ] ( /config/sequence#sequence-hooks ) )
159+ - Test function executes
160+ - ` afterEach ` hooks execute (reverse order by default with ` sequence.hooks: 'stack' ` )
161+ - [ ` onTestFinished ` ] ( /api/hooks#ontestfinished ) callbacks run (always in reverse order)
162+ - If test failed: [ ` onTestFailed ` ] ( /api/hooks#ontestfailed ) callbacks run
163+ - Note: if ` repeats ` or ` retry ` are set, all of these steps are executed again
164+ 6 . ** [ ` afterAll ` ] ( /api/hooks#afterall ) hooks:** Run once after all tests in the suite complete
165+ >>>>>>> c0fcb140c1c79f419ea6578e689607cdf4218036
140166
141167** 执行流程示例:**
142168
@@ -347,6 +373,7 @@ export function teardown() {
347373
348374在 watch 模式下,生命周期会重复执行,但有一些差异:
349375
376+ <<<<<<< HEAD
3503771 . ** 首次运行:** 完整生命周期如上所述
3513782 . ** 文件变更时:**
352379 - 启动新的 [ 测试运行器] ( /api/advanced/reporters#ontestrunstart )
@@ -356,6 +383,17 @@ export function teardown() {
3563833 . ** 退出时:**
357384 - 执行全局清理
358385 - 进程终止
386+ =======
387+ 1 . ** Initial run:** Full lifecycle as described above
388+ 2 . ** On file change:**
389+ - New [ test run] ( /api/advanced/reporters#ontestrunstart ) starts
390+ - Only affected test files are re-run
391+ - [ Setup files] ( /config/setupfiles ) run again for those test files
392+ - [ Global setup] ( /config/globalsetup ) does ** not** re-run (use [ ` project.onTestsRerun ` ] ( /config/globalsetup#handling-test-reruns ) for rerun-specific logic)
393+ 3 . ** On exit:**
394+ - Global teardown executes
395+ - Process terminates
396+ >>>>>>> c0fcb140c1c79f419ea6578e689607cdf4218036
359397
360398## 性能注意事项 {#performance-considerations}
361399
0 commit comments