From 8ff888030f642c0c96a09c070c56e017ab32033c Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 20 Aug 2025 14:16:43 +0000 Subject: [PATCH] feat: add unit tests for src/timeout.ts Adds unit tests for the `timeoutFallback` and `timeoutError` functions in `src/timeout.ts`. The tests cover the following scenarios: - Promise resolves before timeout - Timeout occurs before promise resolves - Custom error messages and objects for `timeoutError` --- src/timeout.spec.ts | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/timeout.spec.ts diff --git a/src/timeout.spec.ts b/src/timeout.spec.ts new file mode 100644 index 00000000000..a0ae4c7417a --- /dev/null +++ b/src/timeout.spec.ts @@ -0,0 +1,56 @@ +import { expect } from "chai"; +import { timeoutFallback, timeoutError } from "./timeout"; + +describe("timeoutFallback", () => { + it("should resolve with the promise value when it completes before timeout", async () => { + const promise = new Promise((resolve) => setTimeout(() => resolve("success"), 10)); + const result = await timeoutFallback(promise, "fallback", 20); + expect(result).to.equal("success"); + }); + + it("should resolve with the fallback value when timeout occurs", async () => { + const promise = new Promise((resolve) => setTimeout(() => resolve("success"), 30)); + const result = await timeoutFallback(promise, "fallback", 20); + expect(result).to.equal("fallback"); + }); +}); + +describe("timeoutError", () => { + it("should resolve with the promise value when it completes before timeout", async () => { + const promise = new Promise((resolve) => setTimeout(() => resolve("success"), 10)); + const result = await timeoutError(promise, "error", 20); + expect(result).to.equal("success"); + }); + + it("should reject with a default error when timeout occurs", async () => { + const promise = new Promise((resolve) => setTimeout(() => resolve("success"), 30)); + try { + await timeoutError(promise, undefined, 20); + expect.fail("should have thrown"); + } catch (e: any) { + expect(e.message).to.equal("Operation timed out."); + } + }); + + it("should reject with a custom error message when timeout occurs", async () => { + const promise = new Promise((resolve) => setTimeout(() => resolve("success"), 30)); + const errorMessage = "custom error"; + try { + await timeoutError(promise, errorMessage, 20); + expect.fail("should have thrown"); + } catch (e: any) { + expect(e.message).to.equal(errorMessage); + } + }); + + it("should reject with a custom error object when timeout occurs", async () => { + const promise = new Promise((resolve) => setTimeout(() => resolve("success"), 30)); + const error = new Error("custom error object"); + try { + await timeoutError(promise, error, 20); + expect.fail("should have thrown"); + } catch (e: any) { + expect(e).to.equal(error); + } + }); +});