fix(core): ensure retry sets defaults for nullish values passed into options (#9540)

Co-authored-by: Abhi <43648792+abhipatel12@users.noreply.github.com>
This commit is contained in:
anthony bushong
2025-09-25 10:33:00 -07:00
committed by GitHub
parent c463d47fa8
commit 4caaa2a8e8
2 changed files with 22 additions and 1 deletions

View File

@@ -116,6 +116,23 @@ describe('retryWithBackoff', () => {
expect(mockFn).toHaveBeenCalledTimes(5);
});
it('should default to 5 maxAttempts if options.maxAttempts is undefined', async () => {
// This function will fail more than 5 times to ensure all retries are used.
const mockFn = createFailingFunction(10);
const promise = retryWithBackoff(mockFn, { maxAttempts: undefined });
// Expect it to fail with the error from the 5th attempt.
// eslint-disable-next-line vitest/valid-expect
const assertionPromise = expect(promise).rejects.toThrow(
'Simulated error attempt 5',
);
await vi.runAllTimersAsync();
await assertionPromise;
expect(mockFn).toHaveBeenCalledTimes(5);
});
it('should not retry if shouldRetry returns false', async () => {
const mockFn = vi.fn(async () => {
throw new NonRetryableError('Non-retryable error');

View File

@@ -78,6 +78,10 @@ export async function retryWithBackoff<T>(
throw new Error('maxAttempts must be a positive number.');
}
const cleanOptions = options
? Object.fromEntries(Object.entries(options).filter(([_, v]) => v != null))
: {};
const {
maxAttempts,
initialDelayMs,
@@ -87,7 +91,7 @@ export async function retryWithBackoff<T>(
shouldRetry,
} = {
...DEFAULT_RETRY_OPTIONS,
...options,
...cleanOptions,
};
let attempt = 0;