30 行
800 B
TypeScript
30 行
800 B
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { fetchWithTimeout } from "./fetch";
|
|
|
|
describe("fetchWithTimeout", () => {
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it("retries timeout-like errors for allowed methods", async () => {
|
|
const fetchMock = vi.fn()
|
|
.mockRejectedValueOnce(new Error("Request timed out after 100ms"))
|
|
.mockResolvedValueOnce(new Response("ok", { status: 200 }));
|
|
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
|
|
const response = await fetchWithTimeout("https://example.com", {
|
|
method: "POST",
|
|
}, {
|
|
timeoutMs: 100,
|
|
retries: 1,
|
|
retryMethods: ["POST"],
|
|
baseDelayMs: 1,
|
|
});
|
|
|
|
expect(response.ok).toBe(true);
|
|
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
});
|
|
});
|