用途:为 Fusion Framework React 应用编写和运行 Vitest 测试,覆盖 hook、组件、路由或完整应用,也用于搭建与排查测试工程本身。
主要能力:按被测行为选择最小测试层级——纯函数用标准 Vitest;消费 app 模块的 React hook 用 renderAppHook;组件、路由或完整应用用 test/render fixture;需要可复用 app fixture 用 testApp;模块配置与框架级模块的 mock 交由 fusion-framework-mocking。测试工具只替换外部边界,运行器、断言、生命周期、spy、定时器与覆盖率仍由 Vitest 负责。
使用方式:安装依赖并配置。
pnpm add -D vitest playwright @vitest/browser-playwright vitest-browser-react
@equinor/fusion-framework-vitest-plugin-react-app
pnpm exec playwright install chromium
vitest.config.ts 使用 defineProject(来自 @equinor/fusion-framework-vitest-plugin-react-app/config),它注册 appTestVitePlugin、通过 Playwright 启动无头 Chromium、包含 src/**/*.{test,spec}.{ts,tsx} 并做懒加载预热。测试从 /test 导入 test,使用 render 渲染并断言;所有 Fusion 渲染/hook 辅助函数(render、renderHook、renderAppComponent、renderAppHook)都必须 await。多个用例共享种子状态时,可扩展 test 或用 test.override(...)。
适用场景:编写组件/hook/路由测试、配置 vitest.config.ts、排查测试挂起或命中网络、判断 hook 测试还是组件测试、跨文件共享 fixture、从 Testing Library/jsdom 迁移。
限制:不负责模块状态种子(登录用户、上下文、特性开关、HTTP 响应),需转 fusion-framework-mocking;不覆盖 dev-server.config.ts 的本地 API mock/代理;不适用于非 Fusion 测试或后端/服务仓库测试。默认 Browser Mode 为保真而非速度,仅在组件/hook 可证明不依赖真实浏览器行为时才改用 happy-dom/jsdom。未匹配的 HTTP 请求会到达真实网络。不要臆造未文档化的 Vitest/Playwright 配置项,测试文件移动时保持 server.warmup.clientFiles 与 test.include 一致。