Skip to content

test: reuse jsdom per worker to halve the suite runtime - #388

Open
sunxiaobin89 wants to merge 1 commit into
GOODBOY008:mainfrom
sunxiaobin89:perf/vitest-pool
Open

sunxiaobin89 wants to merge 1 commit into
GOODBOY008:mainfrom
sunxiaobin89:perf/vitest-pool

Conversation

@sunxiaobin89

Copy link
Copy Markdown
Contributor

概要

Vitest 会为 181 个测试文件各重建一次 jsdom(79 秒环境创建时间,占 27 秒墙钟时间的 36%)。改用 pool: 'vmThreads' 后每个 worker 复用同一个环境,同时保留 per-file 隔离。

结果:27.1 s → 11.6–13.0 s(约 55% 提速),2181 个测试全部通过(连续两次运行)。

附带修复

共享 jsdom 暴露了一个潜在的 stub 缺陷:i18n-setup.ts 定义 window.matchMedia 时没有 configurable: true,同一 worker 中后续文件再定义就会报 Cannot redefine property: matchMedia。两处 matchMedia stub 都改为 configurable。

测试计划

  • 基线(默认池):pnpm test → 27.08 s,jsdom 创建 181 次 / 79.09 s
  • 改动后:pnpm test → 11.64 s,随后 12.99 s,两次均 178 files / 2181 tests passed
  • 未修改任何产品代码,仅测试配置与测试 stub

Vitest rebuilt jsdom 181 times (79 s of the 27 s wall clock across
workers). pool: 'vmThreads' keeps per-file isolation but reuses the
environment per worker: 27.1 s -> 11.6-13.0 s with all 2181 tests green.

The shared window exposed a latent stub bug: i18n-setup defined
window.matchMedia without configurable: true, so a later file in the
same worker failed with "Cannot redefine property: matchMedia". Mark
both matchMedia stubs configurable.
@sunxiaobin89

Copy link
Copy Markdown
Contributor Author

评估结论:保留

  • 实测提速(同机 macOS 对比):基线 upstream/main 两次 25.06 s / 25.27 s;本分支两次 11.20 s / 10.76 s,约 56% 提速,结果稳定。environment 占比从 36% 降到 5–6%。
  • 全绿:默认顺序连续两次 2181 tests passed;CI 三平台此前已全绿。
  • 单 worker 压力测试:--maxWorkers=1 让同一 worker 依次复用同一 jsdom 跑完 181 个文件,2181 tests 全绿,environment 降到 2%,未发现跨文件状态泄漏。
  • flaky 风险:--sequence.shuffle 下 update-checker.test.tsx > shows brew guidance toast with a copyable command... 偶发失败(clipboard 写入与 toast 的时序);该失败在未改动的 upstream/main 上以同样方式复现(3 次中 1 次),属既有顺序相关不稳定,与本 PR 的 pool 改动无关。
  • 已 rebase 到最新 upstream/main(282d6ea)并重跑 pre-push 门禁(tsc / i18n / 全套测试)通过。

This branch has not been deployed

No deployments
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.

1 participant