Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -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<string>((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<string>((resolve) => setTimeout(() => resolve("success"), 30));
const result = await timeoutFallback(promise, "fallback", 20);
expect(result).to.equal("fallback");
});
});
Comment on lines +4 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These tests rely on setTimeout with real timers, which can sometimes lead to flaky tests on slower environments or under heavy load. Consider using fake timers (e.g., from sinon.js or Jest's built-in fake timers if you were using Jest) to make these tests deterministic and faster. With fake timers, you can control the passage of time manually (e.g., clock.tick(30)) without actually waiting.


describe("timeoutError", () => {
it("should resolve with the promise value when it completes before timeout", async () => {
const promise = new Promise<string>((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<string>((resolve) => setTimeout(() => resolve("success"), 30));
try {
await timeoutError(promise, undefined, 20);
expect.fail("should have thrown");
} catch (e: any) {

Check warning on line 30 in src/timeout.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
expect(e.message).to.equal("Operation timed out.");

Check warning on line 31 in src/timeout.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .message on an `any` value
}
Comment on lines +30 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While using e: any in a catch block works, it's generally better for type safety to catch the error as unknown (the default in strict mode) and then perform type checks. This makes the test more robust.

This same improvement can be applied to the other rejection tests in this file (lines 41-43 and 52-54).

} catch (e) {
      expect(e).to.be.an.instanceof(Error);
      expect((e as Error).message).to.equal("Operation timed out.");
    }

});

it("should reject with a custom error message when timeout occurs", async () => {
const promise = new Promise<string>((resolve) => setTimeout(() => resolve("success"), 30));
const errorMessage = "custom error";
try {
await timeoutError(promise, errorMessage, 20);
expect.fail("should have thrown");
} catch (e: any) {

Check warning on line 41 in src/timeout.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
expect(e.message).to.equal(errorMessage);

Check warning on line 42 in src/timeout.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .message on an `any` value
}
});

it("should reject with a custom error object when timeout occurs", async () => {
const promise = new Promise<string>((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) {

Check warning on line 52 in src/timeout.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
expect(e).to.equal(error);
}
});
});
Loading