mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
27 lines
816 B
TypeScript
27 lines
816 B
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|||
|
|
import { withTimeout } from "@/lib/with-timeout";
|
||
|
|
|
||
|
|
beforeEach(() => {
|
||
|
|
vi.useFakeTimers();
|
||
|
|
});
|
||
|
|
|
||
|
|
afterEach(() => {
|
||
|
|
vi.useRealTimers();
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("withTimeout", () => {
|
||
|
|
it("resolves with the promise value when it settles before the deadline", async () => {
|
||
|
|
await expect(withTimeout(Promise.resolve("ok"), 1000)).resolves.toBe("ok");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("rejects when the promise is still pending past the deadline", async () => {
|
||
|
|
const neverSettles = new Promise<string>(() => {});
|
||
|
|
const settled = withTimeout(neverSettles, 1000).then(
|
||
|
|
() => "resolved",
|
||
|
|
(err: Error) => `rejected:${err.message}`,
|
||
|
|
);
|
||
|
|
await vi.advanceTimersByTimeAsync(1000);
|
||
|
|
expect(await settled).toMatch(/rejected:.*timed out/i);
|
||
|
|
});
|
||
|
|
});
|