Files
crawler-plugin/admin-frontend-vue/tests/mount/company-tabs-failure.mount.test.ts
T
huangzd1997 f142744ed4 feat(护栏): 后管补挂载测试与 readSource 逐文件台账,另加一支 CI job(runner 现状见正文)
这面此前 0 个挂载测试(@vue/test-utils 不在 package.json、node_modules 是空目录),
.vite 页面全靠"读源码做正则断言"钉住,改法一旦不是那个字面写法就失明。
装上 @vue/test-utils + vitest + jsdom 后补了 2 文件 7 例挂载测试,靶子选两处只 console
不给用户提示的失败路径(公司目录没取到时原先只剩"tab 条没画出来")。

source-pin 台账用锚点分段口径:实测这棵树 1,704 个 test(/it( 全部行首出现、无循环生成,
而括号配对切块会被正则字面量里的 ) 提前收口、漏 48 处——分母悄悄变小对"只许减"是沉默的,
所以另加一条"与独立出现次数对平"的断言。基线落在每个文件上:只钉"总数+文件数"两个标量时,
A 加一条配 B 减一条就能双双不动地绕过去。

⚠ 这支 CI job 今天是纸面的:Gitea Actions 只列得出 3 次 run,末次停在 2026-08-11 且全是
failure,此后 origin/master 又推进 1,131 笔没有产生任何 run。同一个 workflow 里 backend 的
mvn test、frontend 的 npm test 也一并不会执行——"CI 强制"这一项要成立,得先恢复 runner。
2026-10-08 17:37:00 +08:00

83 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 首批组件挂载用例(四面9分口径 §2 后管「0 组件挂载测试」)。
*
* 靶子是改掉的那处静默 catch:CompanyTabs 的 `tab.initialize().catch(...)`。
* 这三支跑的是真组件、真 pinia store、真 watch ⇒ 判的是运行时行为,不是读源码的形状钉子。
*
* 跑法:`npm run test:mount`(vitest + jsdom,配置见 vitest.config.ts)。
* 放在 tests/mount/ 且 `node --test tests/*.test.ts` 的 glob 不递归 ⇒ 两套 runner 互不干扰。
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { flushPromises, mount } from '@vue/test-utils'
import { createPinia, setActivePinia, type Pinia } from 'pinia'
import CompanyTabs from '@/layout/CompanyTabs.vue'
import { useAdminSessionStore } from '@/stores/admin-session'
import { COMPANY_STATUS_ACTIVE } from '@/api/companies-dto'
const feedback = vi.hoisted(() => ({ showAdminFeedback: vi.fn(), runBusy: vi.fn() }))
const companiesApi = vi.hoisted(() => ({ fetchCompanies: vi.fn() }))
vi.mock('@/components/admin-feedback-ui', () => feedback)
vi.mock('@/api/companies', () => companiesApi)
vi.mock('vue-router', () => ({
useRoute: () => ({ meta: { menuKey: 'admin_users' } }),
}))
const company = (id: number, name: string) => ({
id,
companyName: name,
status: COMPANY_STATUS_ACTIVE,
remark: null,
createdAt: null,
updatedAt: null,
})
describe('CompanyTabs:公司目录取数失败必须让用户看得见', () => {
let pinia: Pinia
beforeEach(() => {
vi.clearAllMocks()
pinia = createPinia()
setActivePinia(pinia)
useAdminSessionStore().user = { id: 1, username: 'root', role: 'super_admin' }
})
it('GET /companies 抛错时经统一出口给一条 warning,而不是只剩 console.warn', async () => {
companiesApi.fetchCompanies.mockRejectedValue(new Error('503 Service Unavailable'))
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
mount(CompanyTabs, { global: { plugins: [pinia] } })
await flushPromises()
expect(feedback.showAdminFeedback).toHaveBeenCalledTimes(1)
const [message, kind] = feedback.showAdminFeedback.mock.calls[0]
expect(message).toContain('公司目录没取到')
expect(message).toContain('503 Service Unavailable')
expect(kind).toBe('warning')
expect(warn).toHaveBeenCalledTimes(1)
warn.mockRestore()
})
it('反向钉:目录取到时一句提示都不许有,而 tab 条要真画出公司名', async () => {
companiesApi.fetchCompanies.mockResolvedValue([company(1, '甲公司'), company(2, '乙公司')])
const wrapper = mount(CompanyTabs, { global: { plugins: [pinia] } })
await flushPromises()
expect(feedback.showAdminFeedback).not.toHaveBeenCalled()
expect(wrapper.findAll('button[role="tab"]').map((b) => b.text())).toEqual(['甲公司', '乙公司'])
})
it('非超管根本不发这个请求,也就不会凭空弹一条"取不到目录"', async () => {
useAdminSessionStore().user = { id: 2, username: 'ops', role: 'admin' }
companiesApi.fetchCompanies.mockRejectedValue(new Error('不该被调用'))
mount(CompanyTabs, { global: { plugins: [pinia] } })
await flushPromises()
expect(companiesApi.fetchCompanies).not.toHaveBeenCalled()
expect(feedback.showAdminFeedback).not.toHaveBeenCalled()
})
})