2026-07-20 11:19:27 -07:00
|
|
|
import { afterEach, describe, expect, test } from "bun:test";
|
2026-09-14 17:25:30 +01:00
|
|
|
import { apiFetch, apiPost, htmlFetch } from "../src/helpers";
|
2026-07-20 11:19:27 -07:00
|
|
|
|
|
|
|
|
// A stalled upstream connection (accepted socket, no response) would otherwise
|
|
|
|
|
// hang the CLI forever - fetch has no default timeout. Assert both request
|
|
|
|
|
// wrappers carry an AbortSignal timeout. Fails on the pre-fix code (no signal).
|
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
globalThis.fetch = originalFetch;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("request timeout", () => {
|
|
|
|
|
test("apiFetch passes an AbortSignal timeout to fetch", async () => {
|
|
|
|
|
let init: RequestInit | undefined;
|
|
|
|
|
globalThis.fetch = (async (_url: string | URL | Request, i?: RequestInit) => {
|
|
|
|
|
init = i;
|
|
|
|
|
return new Response("{}", { status: 200 });
|
|
|
|
|
}) as unknown as typeof fetch;
|
|
|
|
|
|
|
|
|
|
await apiFetch("/search");
|
|
|
|
|
expect(init?.signal).toBeInstanceOf(AbortSignal);
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-14 17:25:30 +01:00
|
|
|
test("htmlFetch passes an AbortSignal timeout to fetch", async () => {
|
|
|
|
|
let init: RequestInit | undefined;
|
|
|
|
|
globalThis.fetch = (async (_url: string | URL | Request, i?: RequestInit) => {
|
|
|
|
|
init = i;
|
|
|
|
|
return new Response("<html></html>", { status: 200 });
|
|
|
|
|
}) as unknown as typeof fetch;
|
|
|
|
|
|
|
|
|
|
await htmlFetch("https://jobdanmark.dk/job/x");
|
|
|
|
|
expect(init?.signal).toBeInstanceOf(AbortSignal);
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-20 11:19:27 -07:00
|
|
|
test("apiPost passes an AbortSignal timeout to fetch", async () => {
|
|
|
|
|
let init: RequestInit | undefined;
|
|
|
|
|
globalThis.fetch = (async (_url: string | URL | Request, i?: RequestInit) => {
|
|
|
|
|
init = i;
|
|
|
|
|
return new Response("{}", { status: 200 });
|
|
|
|
|
}) as unknown as typeof fetch;
|
|
|
|
|
|
|
|
|
|
await apiPost("/search", { q: "x" });
|
|
|
|
|
expect(init?.signal).toBeInstanceOf(AbortSignal);
|
|
|
|
|
});
|
|
|
|
|
});
|