timeout.test.ts 702 B

123456789101112131415161718192021
  1. import { describe, expect, test } from "bun:test"
  2. import { withTimeout } from "../../src/util/timeout"
  3. describe("util.timeout", () => {
  4. test("should resolve when promise completes before timeout", async () => {
  5. const fastPromise = new Promise<string>((resolve) => {
  6. setTimeout(() => resolve("fast"), 10)
  7. })
  8. const result = await withTimeout(fastPromise, 100)
  9. expect(result).toBe("fast")
  10. })
  11. test("should reject when promise exceeds timeout", async () => {
  12. const slowPromise = new Promise<string>((resolve) => {
  13. setTimeout(() => resolve("slow"), 200)
  14. })
  15. await expect(withTimeout(slowPromise, 50)).rejects.toThrow("Operation timed out after 50ms")
  16. })
  17. })