import { flushPromises, mount, shallowMount } from '@vue/test-utils' import type { Component } from 'vue' import { appPlugins, loginAsAdmin, mockBackend } from '../helpers/app' /** * 页面冒烟测试:逐个挂载 src/views 下的页面与页面组件(子组件浅渲染), * 走真实的 request 封装(网络层替换为空数据),断言能渲染出 DOM 且不抛异常。 * 用于发现导入错误、setup 运行时异常、模板引用了不存在的变量等回归。 */ const pages = import.meta.glob<{ default: Component }>('/src/views/**/*.vue') interface SmokeOptions { /** 必填 props 的最小合法值 */ props?: Record /** 父组件通过 provide 注入的数据 */ provide?: Record /** 通过 ref 调用子组件方法的页面需完整渲染子组件 */ deep?: boolean /** 无法在 jsdom 中渲染的页面,写明原因 */ skip?: string } const options: Record = { '/src/views/Home/Index2.vue': { skip: 'echarts-wordcloud 依赖 canvas,jsdom 不支持' }, '/src/views/Redirect/Redirect.vue': { skip: '依赖路由 params.path,由路由守卫在浏览器中验证' }, '/src/views/Profile/components/BasicInfo.vue': { deep: true } } const entries = Object.keys(pages) .sort() .filter((path) => !options[path]?.skip) describe('views 冒烟渲染', () => { it('跳过的页面都写明了原因', () => { for (const [path, option] of Object.entries(options)) { expect(Object.keys(pages)).toContain(path) if (option.skip !== undefined) expect(option.skip.length).toBeGreaterThan(5) } }) beforeEach(async () => { await loginAsAdmin() }) it.each(entries)('%s', async (path) => { mockBackend() const errors: unknown[] = [] const mod = await pages[path]() const option = options[path] ?? {} const wrapper = (option.deep ? mount : shallowMount)(mod.default, { props: option.props, global: { plugins: appPlugins(), provide: option.provide, config: { errorHandler: (err) => { errors.push(err) } } } }) await flushPromises() expect(errors).toEqual([]) expect(wrapper.html().length).toBeGreaterThan(0) wrapper.unmount() }) })