import { expect, test } from "bun:test"; import { LazyPromise } from "./LazyPromise"; test("normal `Promise` is *not* lazy", async () => { let started = false; const promise = new Promise((resolve, _reject) => { started = true; resolve("foo"); }); expect(started).toBe(true); expect(await promise).toEqual("foo"); }); test("`LazyPromise` is lazy", async () => { let started = false; const promise = new LazyPromise(() => { started = true; return "foo"; }); expect(started).toBe(false); await promise; expect(started).toBe(true); expect(await promise).toEqual("foo"); }); test("`LazyPromise` can reject", async () => { let started = false; const promise = new LazyPromise(() => { started = true; throw new Error("oops"); }); expect(started).toBe(false); expect(async () => promise).toThrow("oops"); expect(started).toBe(true); }); test("`LazyPromise` supports `.finally(…)` and `.catch(…)`", async () => { let started = false; const { promise: reachedFinally, resolve: resolveFinally } = Promise.withResolvers(); const promise = new LazyPromise(() => { started = true; throw new Error("oops"); }); expect(started).toBe(false); expect(async () => promise).toThrow("oops"); expect(started).toBe(true); void promise .finally(() => { resolveFinally("hello"); }) .catch(() => {}); expect(await reachedFinally).toEqual("hello"); });