补齐请求层取消与错误边界测试
This commit is contained in:
1 parent
2a018eb6dc
commit
2c94fddab5
2 files changed
+72
-1
No files matched your search
@@ -658,3 +658,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-15 | 完成测试覆盖率提升切片“React Accounts 子模块编辑、AI 与密码登录覆盖” | 新增账号子模块 Hook 的编辑弹窗初始化、通知渠道加载/切换、长期登录保存、AI 设置保存、暂停重启、密码登录成功/取消以及绑定/长登录/AI 请求失败隔离测试;前端 232 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 52.59%;真实账号凭证、密码和平台登录流程仍不触网,仅验证可控接口契约、请求取消和生命周期状态机 | 下一步:继续补齐页面组件行为与剩余非账号外部错误分支 |
|
||||
| 2026-08-15 | 调整测试覆盖率目标为业务代码边界 | 根据最新要求移除本轮新增的纯 UI 页面组件测试,不再把 Sidebar、页面空状态、页面结构和展示分支作为覆盖率目标;保留账号、聊天、商品批量、通知、订单、设置、规则等 Hook、状态机、请求编排和领域工具测试;后续覆盖率统计以业务模块为主,真实账号/外部平台流程仍仅在确实无法确定性模拟时跳过 | 下一步:继续补齐业务 Hook、请求编排与领域状态的未覆盖分支 |
|
||||
| 2026-08-15 | 完成业务测试覆盖切片“AMap 地点查询适配边界” | 新增高德地点查询的无效坐标、无数据、失败响应、POI 字段校验和结果映射测试;仅使用浏览器全局与 PlaceSearch 可控替身,不触发真实地图网络请求;前端测试、类型和注释门禁通过,纯 UI 组件不纳入覆盖率目标 | 下一步:补齐请求层与业务 Hook 的未覆盖错误分支 |
|
||||
| 2026-08-15 | 完成业务测试覆盖切片“请求层方法与取消/错误边界” | 新增 PUT/DELETE 请求、纯文本错误、损坏 JSON 错误体、外部 AbortSignal 及上传取消测试;验证统一错误消息、认证失败通知和超时/取消区分;前端请求测试、类型和注释门禁通过,纯 UI 组件不纳入覆盖率目标 | 下一步:补齐业务 Hook 的未覆盖错误分支 |
|
||||
@@ -1,5 +1,5 @@
|
||||
import { afterEach, describe, expect, test, vi } from 'vitest';
|
||||
import { get, post, postForm } from './request';
|
||||
import { del, get, post, postForm, put } from './request';
|
||||
|
||||
afterEach(() => vi.unstubAllGlobals() /* 回调函数负责当前业务流程。 */);
|
||||
|
||||
@@ -33,6 +33,20 @@ describe('request helpers', () => {
|
||||
}));
|
||||
} /* 回调函数负责当前业务流程。 */);
|
||||
|
||||
test('支持 PUT 和 DELETE 请求及查询参数', async () => {
|
||||
// fetchMock 是 PUT/DELETE 请求的网络替身。
|
||||
const fetchMock = vi.fn().mockImplementation(/* responseFactory 为每次调用生成独立响应体。 */ () => Promise.resolve(new Response(JSON.stringify({ ok: true }), {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
})));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
await expect(put('/items/1', { enabled: true })).resolves.toEqual({ ok: true });
|
||||
await expect(del('/items/1', { force: true })).resolves.toEqual({ ok: true });
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(1, '/items/1', expect.objectContaining({ method: 'PUT', body: JSON.stringify({ enabled: true }) }));
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(2, '/items/1?force=true', expect.objectContaining({ method: 'DELETE' }));
|
||||
} /* 回调函数负责当前业务流程。 */);
|
||||
|
||||
test('surfaces unified backend errors', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ code: 'bad_request', message: 'bad request' }), {
|
||||
status: 400,
|
||||
@@ -41,6 +55,28 @@ describe('request helpers', () => {
|
||||
await expect(get('/bad')).rejects.toThrow('bad request');
|
||||
} /* 回调函数负责当前业务流程。 */);
|
||||
|
||||
test('使用非 JSON 错误文本作为失败消息', async () => {
|
||||
// fetchMock 是返回纯文本错误的网络替身。
|
||||
const fetchMock = vi.fn().mockResolvedValue(new Response('网关暂时不可用', {
|
||||
status: 502,
|
||||
headers: { 'content-type': 'text/plain' },
|
||||
}));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
await expect(get('/gateway')).rejects.toThrow('网关暂时不可用');
|
||||
} /* 回调函数负责当前业务流程。 */);
|
||||
|
||||
test('JSON 错误体无法解析时使用 HTTP 状态兜底', async () => {
|
||||
// fetchMock 是返回损坏 JSON 错误体的网络替身。
|
||||
const fetchMock = vi.fn().mockResolvedValue(new Response('{broken', {
|
||||
status: 500,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
}));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
await expect(get('/broken')).rejects.toThrow('请求失败: 500');
|
||||
} /* 回调函数负责当前业务流程。 */);
|
||||
|
||||
test('notifies the app when an authenticated request returns 401', async () => {
|
||||
const events = new EventTarget(); /* events 表示events。 */
|
||||
const listener = vi.fn(); /* listener 表示listener。 */
|
||||
@@ -94,4 +130,38 @@ describe('request helpers', () => {
|
||||
await rejection;
|
||||
vi.useRealTimers();
|
||||
} /* 回调函数负责当前业务流程。 */);
|
||||
|
||||
test('外部 AbortSignal 取消请求时返回取消错误', async () => {
|
||||
// controller 是调用方主动取消请求的控制器。
|
||||
const controller = new AbortController();
|
||||
// fetchMock 是等待外部取消信号的网络替身。
|
||||
const fetchMock = vi.fn((_url: string, init: RequestInit) => new Promise<Response>(
|
||||
/* fetchPromiseExecutor 等待请求信号触发取消。 */ (_resolve, reject) => {
|
||||
init.signal?.addEventListener('abort', /* abortCallback 将取消转换为网络异常。 */ () => reject(new DOMException('aborted', 'AbortError')), { once: true });
|
||||
},
|
||||
));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
// pending 是等待外部取消结果的请求 Promise。
|
||||
const pending = get('/cancelled', undefined, { signal: controller.signal });
|
||||
controller.abort();
|
||||
await expect(pending).rejects.toThrow('请求已取消');
|
||||
} /* 回调函数负责当前业务流程。 */);
|
||||
|
||||
test('上传请求支持外部取消并返回取消错误', async () => {
|
||||
// controller 是上传调用方主动取消请求的控制器。
|
||||
const controller = new AbortController();
|
||||
// fetchMock 是等待上传取消信号的网络替身。
|
||||
const fetchMock = vi.fn((_url: string, init: RequestInit) => new Promise<Response>(
|
||||
/* uploadPromiseExecutor 等待上传信号触发取消。 */ (_resolve, reject) => {
|
||||
init.signal?.addEventListener('abort', /* abortCallback 将上传取消转换为网络异常。 */ () => reject(new DOMException('aborted', 'AbortError')), { once: true });
|
||||
},
|
||||
));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
// pending 是等待上传取消结果的请求 Promise。
|
||||
const pending = postForm('/upload', new FormData(), { signal: controller.signal });
|
||||
controller.abort();
|
||||
await expect(pending).rejects.toThrow('请求已取消');
|
||||
} /* 回调函数负责当前业务流程。 */);
|
||||
} /* 回调函数负责当前业务流程。 */);
|
||||
Reference in new issue
Block a user