Skip to content

Commit 4af6b22

Browse files
committed
test: add unit tests for retryWithBackoff
1 parent 84e8052 commit 4af6b22

2 files changed

Lines changed: 25 additions & 60 deletions

File tree

js/__tests__/retryWithBackoff.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,24 @@ describe("retryWithBackoff", () => {
154154
describe("max retries exceeded", () => {
155155
it("should reject with an error when max retries are exhausted", async () => {
156156
const check = jest.fn(() => null);
157+
const onRetry = jest.fn();
158+
const delayFn = jest.fn(() => Promise.resolve());
157159

158160
await expect(
159161
retryWithBackoff({
160162
check,
161163
onSuccess: jest.fn(),
162-
delayFn: instantDelay,
164+
onRetry,
165+
delayFn,
163166
maxRetries: 3,
164167
errorMessage: "COULD NOT CREATE CACHE"
165168
})
166169
).rejects.toThrow("COULD NOT CREATE CACHE");
167170

168171
// check called for attempts 0, 1, 2, 3 then fail on count=4 > maxRetries=3
169172
expect(check).toHaveBeenCalledTimes(4);
173+
expect(onRetry).toHaveBeenCalledTimes(3);
174+
expect(delayFn).toHaveBeenCalledTimes(3);
170175
});
171176

172177
it("should use default error message when none provided", async () => {
@@ -226,6 +231,25 @@ describe("retryWithBackoff", () => {
226231
// First delay should be 50 * 2^0 = 50
227232
expect(delayFn).toHaveBeenCalledWith(50);
228233
});
234+
235+
it("should use default delay function if none provided", async () => {
236+
jest.useFakeTimers();
237+
const check = jest.fn().mockReturnValueOnce(false).mockReturnValueOnce(true);
238+
239+
const promise = retryWithBackoff({
240+
check,
241+
onSuccess: jest.fn(),
242+
initialDelay: 50
243+
});
244+
245+
// Advance timers by 50ms to resolve the default delay (50 * 2^0 = 50ms)
246+
jest.advanceTimersByTime(50);
247+
248+
await promise;
249+
expect(check).toHaveBeenCalledTimes(2);
250+
251+
jest.useRealTimers();
252+
});
229253
});
230254

231255
describe("error propagation", () => {

js/utils/__tests__/retryWithBackoff.test.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)