Skip to content

test(web): DeptsPage 等 Semi 浮层异步链走完再结束,修偶发 Tooltip unhandled error - #120

Open
supdger wants to merge 3 commits into
mainfrom
fix/deptspage-tooltip-flaky
Open

supdger wants to merge 3 commits into
mainfrom
fix/deptspage-tooltip-flaky

Conversation

@supdger

@supdger supdger commented Nov 28, 2026

Copy link
Copy Markdown
Owner

根因

「更多」下拉是 Semi Tooltip(trigger=click) 的受控浮层。Semi 2.104.0 的显示链路是跨多个宏任务的异步链(已通过官方 @douyinfe/semi-mcp 核对 2.104.0 源码):

  1. 点击后 show() → insertPortal 先把浮层离屏插入(isInsert=true,内容此刻已进 DOM);
  2. insertPortal 等浮层尺寸稳定后才 emit portalInserted:优先 ResizeObserver + 32ms 稳定定时器,兜底是 100ms 安全网 setTimeout(该定时器不存引用、不可取消);
  3. portalInserted → calcPosition → positionUpdated → togglePortalVisible(true) → notifyVisibleChange → 受控 visible 翻转,链路才走完。

测试环境 jsdom 无布局(offsetWidth 恒 0)且 setup 里的 ResizeObserver 是永不回调的空桩,所以这条链只能靠 100ms 安全网定时器推进。原测试断言(findByText 只依赖 DOM 存在性,插入后即满足)通常在几十毫秒内完成并结束,cleanup() 卸载组件树后,挂起的安全网定时器及其后续链在卸载状态/半卸载状态下继续触发,偶发在 React/jsdom 内部抛出 unhandled error(CI 记录为 Cannot read properties of null (reading 'nodeType')),vitest 将其判为整个测试文件失败——表现为偶发红、重跑即过(见 ci-tier-1117 / gates-sync-1117 的 RESULT 记录,push run 44183770429 命中过一次)。

修改(只改测试文件)

web/src/pages/org/depts/DeptsPage.test.tsx:打开「更多」下拉、完成原有断言后,用 waitFor 等触发按钮的 aria-expanded 翻成 true。该属性由 Dropdown 在 notifyVisibleChange 之后才更新,是整条异步链最末端的信号;等到它即保证所有挂起定时器都在挂载状态下耗尽。实测 aria-expanded 翻 true 后 400ms 内 DOM 零 mutation,无第二代定时器链。

不加测试重试、不 skip/跳过用例、不放宽断言(反而多了一条断言)。

验证

  • 目标测试 npx vitest run src/pages/org/depts/DeptsPage.test.tsx 连跑 50 次全绿(50/50,退出码全 0);
  • pnpm typecheck:退出码 0;
  • pnpm lint:退出码 0;
  • pnpm test(全量 vitest):125 个文件、536 passed / 2 skipped,退出码 0;
  • pnpm test:smoke(playwright,msw mock 起 dev server,无后端依赖):1 passed,退出码 0。

基线:origin/main 5360129459241f4e0531fe3483f7c1f19119f7cd。

「更多」下拉是 Semi Tooltip(trigger=click) 受控浮层:点击后 insertPortal
先离屏插入,jsdom 无布局且 ResizeObserver 是空桩,portalInserted 只能靠
100ms 安全网定时器触发,随后 calcPosition → positionUpdated → 可见,整条
链跨多个宏任务。原测试断言完即结束,链路常在卸载后才继续触发,偶发在
React/jsdom 内部抛 unhandled error(CI 记录 nodeType of null)判红整个文件。

aria-expanded 在 notifyVisibleChange 之后才翻成 true,是链路最末端信号;
等到它即保证挂起定时器全部在挂载状态下耗尽(实测 aria-expanded 翻 true
后 400ms 内 DOM 零 mutation)。不加测试重试、不跳过用例、不放宽断言。

验证:目标测试连跑 50 次全绿;typecheck/lint/全量 vitest(536 passed)/
playwright smoke 均通过。
supdger added a commit that referenced this pull request Nov 29, 2026
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