Skip to content

perf(test): 优化 TSX 单元测试执行性能 - #1532

Merged
CodFrm merged 16 commits into
scriptscat:mainfrom
cyfung1031:fix-test-tsx-performance
Jul 5, 2026
Merged

perf(test): 优化 TSX 单元测试执行性能#1532
CodFrm merged 16 commits into
scriptscat:mainfrom
cyfung1031:fix-test-tsx-performance

Conversation

@cyfung1031

@cyfung1031 cyfung1031 commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

Checklist / 检查清单

  • Fixes mentioned issues / 修复已提及的问题
  • Code reviewed by human / 代码通过人工检查
  • Changes tested / 已完成测试

Description / 描述

本 PR 优化 TSX 单元测试的执行性能,减少测试运行中的重复初始化、慢查询和不必要的异步轮询,同时补充高效单元测试编写规范,方便后续维护测试性能。

主要变更:

  • 将部分测试中的语言初始化从 beforeEach 调整为 beforeAll,避免每个用例重复初始化测试语言环境。
  • 替换部分较慢的 Testing Library 查询方式,减少不必要的全局查询和 accessibility tree 扫描。
  • 对已知同步完成的交互断言移除多余的 waitFor,降低轮询带来的测试耗时。
  • 在大范围 TSX 测试中改用更直接、更窄范围的查询方式,例如 querySelectorgetByLabelTextgetByText 或已存在的 data-testid
  • 更新 docs/DEVELOP.md,补充高效单元测试编写建议,包括查询范围、异步断言、waitFor 使用边界和 TSX 慢测试排查方式。

Screenshots / 截图

无界面改动。

@cyfung1031 cyfung1031 changed the title Fix test tsx performance perf(test): 优化 TSX 单元测试执行性能 Jul 4, 2026
cyfung1031 and others added 11 commits July 5, 2026 07:55
- encoding: 5 个百 KB 级大夹具并发用例改为顺序执行(并发时同一 worker
  互相排队,单例被放大到 337–346ms,solo 实测仅数 ms)
- encoding: 3 个 chardet 32KB 采样用例在 V8 覆盖率插桩下 solo 实测
  约 280–310ms(真实 CPU 成本,非争抢),给显式 { timeout: 850 },
  全局 340ms 预算对其余用例保持收紧
- backup: 50 项批量压缩夹具改为顺序执行
- 验证:两个分片以 CI 同参数(--test-timeout=340 --coverage --shard)
  本地全绿(1475/1475、1499/1499);改动前分片 2 本地 2 例超时

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
CPU profile 显示 SelectorParser.getSelectorGroups 每次解析 selector 都
无条件预构造 DOMException(Error 构造捕获深层 React 调用栈),占
SettingsPane 一档 TSX 测试自耗时的 ~14%(216ms/1.5s)。改为仅在解析
失败时构造。上游 20.10.6 仍存在同样写法。

实测(SettingsPane 25 例总耗时):无覆盖率 1657→1480ms,
覆盖率 2312→1988ms;重交互用例改善最大(如重置授权 147→80ms)。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
问题一(预算错配):页面层用例(React 渲染,含 .ts renderHook)solo
覆盖率下真实成本 100–200ms,倒计时类 fake-timer 用例本地满载观测
~630ms,340ms 全局预算必然偶发超时。新增 vitest "ui" project
(src/pages/**/*.test.{ts,tsx})预算 850ms;fast/isolated 保持 340ms。
预算移入配置,CLI 不再传 --test-timeout(会一刀切覆盖所有 project)。

问题二(确定性挂死):experimental.fsModuleCache 的缓存
(node_modules/.experimental-vitest-cache)在依赖变更后不失效,曾使
Logger/hooks.test.ts 与 confirm/App.test.tsx 三例按「路径+内容哈希」
确定性挂死(任意预算下都在预算处超时;字节级拷贝到别的文件名即通过;
vitest --clearCache 后恢复)。CI 冷缓存永不复现,只坑本地,予以移除;
代价为全量本地跑 transform +9s(3.4s→12s,总时长 +5s 左右)。

验证:test:ci 连续 3 次 2974/2974 全绿;两个覆盖率分片(CI 同参数)
1475/1475、1499/1499 全绿,TSX 峰值 248ms。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@cyfung1031

Copy link
Copy Markdown
Collaborator Author

Related: capricorn86/happy-dom#2228

Comment thread patches/happy-dom.patch

@CodFrm CodFrm Jul 5, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不用引入patch吧,等上游合并了再更新

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这会等很久。上游不太理会PR

@CodFrm CodFrm Jul 5, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

行吧,先这样

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

happy-dom v20.11.1 or above

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

合并了?那可以删掉patch更新

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

等7日

@CodFrm
CodFrm merged commit da49863 into scriptscat:main Jul 5, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants