|
| 1 | +import { retry, wait } from '../src/shared/retry'; |
| 2 | + |
| 3 | +describe('retry function', () => { |
| 4 | + it('should retry the function for the specified number of times', async () => { |
| 5 | + let counter = 0; |
| 6 | + const fn = () => { |
| 7 | + counter++; |
| 8 | + if (counter < 3) { |
| 9 | + throw new Error('Error'); |
| 10 | + } |
| 11 | + return 'Success'; |
| 12 | + }; |
| 13 | + const result = await retry(fn, 3, wait); |
| 14 | + expect(result).toBe('Success'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should throw an error if the function fails after the specified number of retries', async () => { |
| 18 | + const fn = () => { |
| 19 | + throw new Error('Error'); |
| 20 | + }; |
| 21 | + await expect(retry(fn, 3, wait)).rejects.toThrow('Error'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should pass the number of retries to the wait function', async () => { |
| 25 | + const fn = () => { |
| 26 | + throw new Error('Error'); |
| 27 | + }; |
| 28 | + const waitFn = jest.fn(wait); |
| 29 | + await expect(retry(fn, 3, waitFn)).rejects.toThrow('Error'); |
| 30 | + expect(waitFn).toHaveBeenCalledTimes(3); |
| 31 | + expect(waitFn).toHaveBeenNthCalledWith(1, 1); |
| 32 | + expect(waitFn).toHaveBeenNthCalledWith(2, 2); |
| 33 | + expect(waitFn).toHaveBeenNthCalledWith(3, 3); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +describe('wait function', () => { |
| 38 | + it('should wait for the specified number of milliseconds', async () => { |
| 39 | + const start = Date.now(); |
| 40 | + await wait(100); |
| 41 | + const end = Date.now(); |
| 42 | + expect(end - start).toBeGreaterThanOrEqual(100); |
| 43 | + }); |
| 44 | +}); |
0 commit comments