perf(test): 优化 TSX 单元测试执行性能 - #1532
Merged
Merged
Conversation
… Please use "vitest/node" instead.`
- 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>
Collaborator
Author
|
Related: capricorn86/happy-dom#2228 |
CodFrm
reviewed
Jul 5, 2026
Collaborator
Author
There was a problem hiding this comment.
happy-dom v20.11.1 or above
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist / 检查清单
Description / 描述
本 PR 优化 TSX 单元测试的执行性能,减少测试运行中的重复初始化、慢查询和不必要的异步轮询,同时补充高效单元测试编写规范,方便后续维护测试性能。
主要变更:
beforeEach调整为beforeAll,避免每个用例重复初始化测试语言环境。waitFor,降低轮询带来的测试耗时。querySelector、getByLabelText、getByText或已存在的data-testid。docs/DEVELOP.md,补充高效单元测试编写建议,包括查询范围、异步断言、waitFor使用边界和 TSX 慢测试排查方式。Screenshots / 截图
无界面改动。