-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add unit tests for src/timeout.ts #9013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"); | ||
}); | ||
}); | ||
|
||
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) { | ||
expect(e.message).to.equal("Operation timed out."); | ||
} | ||
Comment on lines
+30
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While using 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) { | ||
expect(e.message).to.equal(errorMessage); | ||
} | ||
}); | ||
|
||
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) { | ||
expect(e).to.equal(error); | ||
} | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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., fromsinon.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.