42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { afterEach, describe, expect, mock, test } from "bun:test";
|
|
import { fetchHtml } from "../src/utils/http";
|
|
|
|
describe("fetchHtml", () => {
|
|
const originalFetch = global.fetch;
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
const originalSetTimeout = globalThis.setTimeout;
|
|
const originalClearTimeout = globalThis.clearTimeout;
|
|
|
|
afterEach(() => {
|
|
global.fetch = originalFetch;
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
globalThis.setTimeout = originalSetTimeout;
|
|
globalThis.clearTimeout = originalClearTimeout;
|
|
});
|
|
|
|
test("does not schedule throttle timers during tests", async () => {
|
|
process.env.NODE_ENV = "test";
|
|
const scheduledDelays: number[] = [];
|
|
|
|
global.fetch = mock(() =>
|
|
Promise.resolve({
|
|
ok: true,
|
|
headers: { get: () => null },
|
|
text: () => Promise.resolve("<html></html>"),
|
|
}),
|
|
) as unknown as typeof fetch;
|
|
globalThis.setTimeout = mock((handler: TimerHandler, timeout?: number) => {
|
|
scheduledDelays.push(Number(timeout));
|
|
if (timeout !== 30_000 && typeof handler === "function") {
|
|
handler();
|
|
}
|
|
return 0 as unknown as ReturnType<typeof setTimeout>;
|
|
}) as unknown as typeof setTimeout;
|
|
globalThis.clearTimeout = mock(() => {}) as unknown as typeof clearTimeout;
|
|
|
|
await fetchHtml("https://example.com", 1000, { timeoutMs: 30_000 });
|
|
|
|
expect(scheduledDelays).not.toContain(1000);
|
|
});
|
|
});
|