




















@@ -11,6 +11,14 @@ vi.mock("../logging/subsystem.js", () => ({
11111212import { buildTimeoutAbortSignal } from "./fetch-timeout.js";
131314+function requireWarnRecord(callIndex: number): Record<string, unknown> {
15+const record = warn.mock.calls[callIndex]?.[1] as Record<string, unknown> | undefined;
16+if (!record) {
17+throw new Error(`missing warning record ${callIndex}`);
18+}
19+return record;
20+}
21+1422describe("buildTimeoutAbortSignal", () => {
1523beforeEach(() => {
1624warn.mockClear();
@@ -31,20 +39,16 @@ describe("buildTimeoutAbortSignal", () => {
3139await vi.advanceTimersByTimeAsync(25);
32403341expect(signal?.aborted).toBe(true);
34-expect(signal?.reason).toMatchObject({
35-name: "TimeoutError",
36-message: "request timed out",
37-});
42+expect((signal?.reason as Error | undefined)?.name).toBe("TimeoutError");
43+expect((signal?.reason as Error | undefined)?.message).toBe("request timed out");
3844expect(warn).toHaveBeenCalledTimes(1);
39-expect(warn).toHaveBeenCalledWith(
40-"fetch timeout reached; aborting operation",
41-expect.objectContaining({
42-timeoutMs: 25,
43-operation: "unit-test",
44-url: "https://example.com/v1/responses",
45-consoleMessage:
46-"fetch timeout after 25ms (elapsed 25ms) operation=unit-test url=https://example.com/v1/responses",
47-}),
45+expect(warn.mock.calls[0]?.[0]).toBe("fetch timeout reached; aborting operation");
46+const record = requireWarnRecord(0);
47+expect(record.timeoutMs).toBe(25);
48+expect(record.operation).toBe("unit-test");
49+expect(record.url).toBe("https://example.com/v1/responses");
50+expect(record.consoleMessage).toBe(
51+"fetch timeout after 25ms (elapsed 25ms) operation=unit-test url=https://example.com/v1/responses",
4852);
49535054cleanup();
@@ -74,17 +78,15 @@ describe("buildTimeoutAbortSignal", () => {
7478Symbol.asyncIterator
7579]();
768077-await expect(iterator.next()).resolves.toMatchObject({
78-done: false,
79-value: { ok: true },
80-});
81+const firstChunk = await iterator.next();
82+expect(firstChunk.done).toBe(false);
83+expect(firstChunk.value).toEqual({ ok: true });
8184const pending = iterator.next().catch((error: unknown) => error);
8285await vi.advanceTimersByTimeAsync(25);
838684-await expect(pending).resolves.toMatchObject({
85-name: "TimeoutError",
86-message: "request timed out",
87-});
87+const timeoutError = (await pending) as Error;
88+expect(timeoutError.name).toBe("TimeoutError");
89+expect(timeoutError.message).toBe("request timed out");
88908991cleanup();
9092});
@@ -100,15 +102,12 @@ describe("buildTimeoutAbortSignal", () => {
100102vi.setSystemTime(2_000);
101103await vi.advanceTimersByTimeAsync(25);
102104103-expect(warn).toHaveBeenCalledWith(
104-"fetch timeout reached; aborting operation",
105-expect.objectContaining({
106-timerDelayMs: 2000,
107-eventLoopDelayHint: "timer delayed 2000ms, likely event-loop starvation",
108-consoleMessage: expect.stringContaining(
109-"timer delayed 2000ms, likely event-loop starvation",
110-),
111-}),
105+expect(warn.mock.calls[0]?.[0]).toBe("fetch timeout reached; aborting operation");
106+const record = requireWarnRecord(0);
107+expect(record.timerDelayMs).toBe(2000);
108+expect(record.eventLoopDelayHint).toBe("timer delayed 2000ms, likely event-loop starvation");
109+expect(String(record.consoleMessage)).toContain(
110+"timer delayed 2000ms, likely event-loop starvation",
112111);
113112114113cleanup();
@@ -123,12 +122,8 @@ describe("buildTimeoutAbortSignal", () => {
123122124123await vi.advanceTimersByTimeAsync(25);
125124126-expect(warn).toHaveBeenCalledWith(
127-"fetch timeout reached; aborting operation",
128-expect.objectContaining({
129-url: "/api/responses",
130-}),
131-);
125+expect(warn.mock.calls[0]?.[0]).toBe("fetch timeout reached; aborting operation");
126+expect(requireWarnRecord(0).url).toBe("/api/responses");
132127133128cleanup();
134129});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。