|
| 1 | +import { describe, it, expect, jest, beforeEach, afterEach } from '@jest/globals'; |
| 2 | +import { withRetry } from './withRetry'; |
| 3 | + |
| 4 | +// Patch setTimeout so retries are instant during tests. |
| 5 | +const realSetTimeout = global.setTimeout; |
| 6 | + |
| 7 | +describe('withRetry', () => { |
| 8 | + beforeEach(() => { |
| 9 | + // Make setTimeout fire instantly to keep tests fast. |
| 10 | + (global as any).setTimeout = (fn: () => void) => realSetTimeout(fn, 0); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + global.setTimeout = realSetTimeout; |
| 15 | + }); |
| 16 | + |
| 17 | + it('returns the result on the first successful attempt', async () => { |
| 18 | + const fn = jest.fn<() => Promise<string>>().mockResolvedValue('ok'); |
| 19 | + const result = await withRetry(fn); |
| 20 | + expect(result).toBe('ok'); |
| 21 | + expect(fn).toHaveBeenCalledTimes(1); |
| 22 | + }); |
| 23 | + |
| 24 | + it('retries on "socket hang up" and succeeds on second attempt', async () => { |
| 25 | + const socketError = Object.assign(new Error('socket hang up'), { |
| 26 | + code: 'ECONNRESET', |
| 27 | + }); |
| 28 | + const fn = jest |
| 29 | + .fn<() => Promise<string>>() |
| 30 | + .mockRejectedValueOnce(socketError) |
| 31 | + .mockResolvedValue('ok'); |
| 32 | + |
| 33 | + const result = await withRetry(fn, { maxAttempts: 3, initialDelayMs: 100 }); |
| 34 | + expect(result).toBe('ok'); |
| 35 | + expect(fn).toHaveBeenCalledTimes(2); |
| 36 | + }); |
| 37 | + |
| 38 | + it('retries on ECONNRESET and succeeds on third attempt', async () => { |
| 39 | + const connError = Object.assign(new Error('read ECONNRESET'), { |
| 40 | + code: 'ECONNRESET', |
| 41 | + }); |
| 42 | + const fn = jest |
| 43 | + .fn<() => Promise<number>>() |
| 44 | + .mockRejectedValueOnce(connError) |
| 45 | + .mockRejectedValueOnce(connError) |
| 46 | + .mockResolvedValue(42); |
| 47 | + |
| 48 | + const result = await withRetry(fn, { maxAttempts: 5, initialDelayMs: 1 }); |
| 49 | + expect(result).toBe(42); |
| 50 | + expect(fn).toHaveBeenCalledTimes(3); |
| 51 | + }); |
| 52 | + |
| 53 | + it('throws immediately on non-transient errors without retrying', async () => { |
| 54 | + const notFound = new Error('Not Found'); |
| 55 | + const fn = jest.fn<() => Promise<string>>().mockRejectedValue(notFound); |
| 56 | + |
| 57 | + await expect( |
| 58 | + withRetry(fn, { maxAttempts: 5, initialDelayMs: 1 }), |
| 59 | + ).rejects.toThrow('Not Found'); |
| 60 | + expect(fn).toHaveBeenCalledTimes(1); |
| 61 | + }); |
| 62 | + |
| 63 | + it('exhausts all attempts and re-throws the last transient error', async () => { |
| 64 | + const hangUp = new Error('socket hang up'); |
| 65 | + const fn = jest.fn<() => Promise<string>>().mockRejectedValue(hangUp); |
| 66 | + |
| 67 | + await expect( |
| 68 | + withRetry(fn, { maxAttempts: 3, initialDelayMs: 1 }), |
| 69 | + ).rejects.toThrow('socket hang up'); |
| 70 | + expect(fn).toHaveBeenCalledTimes(3); |
| 71 | + }); |
| 72 | +}); |
0 commit comments