From a295fd3e47c4803f5d8251840d39881b1de3ebaa Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Wed, 16 Sep 2026 06:36:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(test):=20Header.test.tsx=20antd/icons=20moc?= =?UTF-8?q?k=20=E4=BD=BF=E7=94=A8=20importActual=20=E9=80=8F=E4=BC=A0?= =?UTF-8?q?=E6=9C=AA=E6=98=BE=E5=BC=8F=E8=A6=86=E7=9B=96=E7=9A=84=E5=AF=BC?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Header 现在渲染 PointsBadge,后者从 antd 解构 Popover/Button/Tag/Space/Typography/Badge,从 @ant-design/icons 解构 ThunderboltOutlined/CrownOutlined/RightOutlined/WarningOutlined。 原 mock 只返回 Avatar/Dropdown/Space/Drawer + 4 个图标,vitest 遇到 Typography 等未 mock 的导出会报: [vitest] No "Typography" export is defined on the "antd" mock 修复:改用 vi.importActual 透传真实 antd / @ant-design/icons 的全部导出,只对测试中需要断言的 Avatar/Dropdown/Space/Drawer + 4 个图标做局部覆盖。 其他 17 个 API 单测的 vi.mock("antd", () => ({message:{error,success}})) 模式是纯逻辑测试,不渲染 React 组件,不会触发其他 antd 导出加载,保持现状不影响稳定性。 --- .../test/components/layout/Header.test.tsx | 89 +++++++++++-------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/apps/web/src/test/components/layout/Header.test.tsx b/apps/web/src/test/components/layout/Header.test.tsx index 6f7d7086a..fff71291b 100644 --- a/apps/web/src/test/components/layout/Header.test.tsx +++ b/apps/web/src/test/components/layout/Header.test.tsx @@ -45,49 +45,60 @@ vi.mock("@/config/navigation", () => ({ ], })) -// mock antd icons -vi.mock("@ant-design/icons", () => ({ - LogoutOutlined: () => , - SettingOutlined: () => , - UserOutlined: () => , - MenuOutlined: () => , -})) +// mock antd icons — 透传未显式 mock 的图标,避免 PointsBadge 等子组件引用新图标时报错 +vi.mock("@ant-design/icons", async () => { + const actual = (await vi.importActual( + "@ant-design/icons", + )) as Record + return { + ...actual, + LogoutOutlined: () => , + SettingOutlined: () => , + UserOutlined: () => , + MenuOutlined: () => , + } +}) -// mock antd components -vi.mock("antd", () => ({ - Avatar: ({ children, className }: any) => ( - - {children} - - ), - Dropdown: ({ children, menu }: any) => ( -
- {children} -
- {menu.items?.map((item: any, idx: number) => ( -
- {item.label} -
- ))} +// mock antd components — 用 importActual 透传未显式覆盖的组件(Popover/Button/Tag/Typography/Badge 等), +// 避免 Header 子组件(PointsBadge)使用新 antd 导出时出现 "No xxx export is defined on the antd mock" +vi.mock("antd", async () => { + const actual = (await vi.importActual("antd")) as Record + return { + ...actual, + Avatar: ({ children, className }: any) => ( + + {children} + + ), + Dropdown: ({ children, menu }: any) => ( +
+ {children} +
+ {menu.items?.map((item: any, idx: number) => ( +
+ {item.label} +
+ ))} +
-
- ), - Space: ({ children, className }: any) => ( -
- {children} -
- ), - Drawer: ({ title, open, children, onClose, placement }: any) => - open ? ( -
-
{title}
- + ), + Space: ({ children, className }: any) => ( +
{children}
- ) : null, -})) + ), + Drawer: ({ title, open, children, onClose, placement }: any) => + open ? ( +
+
{title}
+ + {children} +
+ ) : null, + } +}) // mock CSS vi.mock("@/components/layout/Header.css", () => ({})) -- 2.54.0