fix: tolerate null retryable check

This commit is contained in:
Kai (Tam Nhu) Tran
2026-05-30 14:54:54 -04:00
parent c21f589247
commit 54f3b6860a
2 changed files with 21 additions and 1 deletions
@@ -25,6 +25,26 @@ describe('withRetry', () => {
expect(fn).toHaveBeenCalledTimes(3);
});
it('uses the default retryability check when retryableCheck is null at runtime', async () => {
let attempt = 0;
const fn = mock(() => {
attempt++;
if (attempt < 2) {
return Promise.reject(new RetryableError('transient failure'));
}
return Promise.resolve('ok');
});
const result = await withRetry(fn, {
maxRetries: 3,
baseDelayMs: 1,
retryableCheck: null,
} as unknown as RetryOptions);
expect(result).toBe('ok');
expect(fn).toHaveBeenCalledTimes(2);
});
it('throws after max retries exhausted', async () => {
const fn = mock(() => Promise.reject(new RetryableError('always fails')));
await expect(withRetry(fn, { maxRetries: 2, baseDelayMs: 1 })).rejects.toThrow('always fails');
+1 -1
View File
@@ -99,7 +99,7 @@ export async function withRetry<T>(fn: () => Promise<T>, options: RetryOptions):
throw new Error('withRetry: baseDelayMs must be >= 0');
}
const isRetryable = retryableCheck;
const isRetryable = retryableCheck ?? defaultRetryableCheck;
let lastError: unknown;
for (let attempt = 0; attempt <= maxRetries; attempt++) {