|
| 1 | +import { describe, expect, test, vi } from 'vitest'; |
| 2 | + |
| 3 | +import * as retryModule from '../../retry'; |
| 4 | +import { safeImport } from '../index'; |
| 5 | + |
| 6 | +describe('safeImport', () => { |
| 7 | + test('calls retry with correct configuration', async () => { |
| 8 | + const retrySpy = vi.spyOn(retryModule, 'retry'); |
| 9 | + const testModule = './test-module'; |
| 10 | + |
| 11 | + try { |
| 12 | + await safeImport(testModule); |
| 13 | + } catch { |
| 14 | + // Ignore import errors since we're just testing the retry configuration |
| 15 | + } |
| 16 | + |
| 17 | + expect(retrySpy).toHaveBeenCalledWith( |
| 18 | + expect.any(Function), |
| 19 | + expect.objectContaining({ |
| 20 | + initialDelay: 100, |
| 21 | + retryImmediately: true, |
| 22 | + factor: 2, |
| 23 | + }), |
| 24 | + ); |
| 25 | + |
| 26 | + retrySpy.mockRestore(); |
| 27 | + }); |
| 28 | + |
| 29 | + test('returns imported module on success', async () => { |
| 30 | + const mockModule = { default: 'test-module', namedExport: 'value' }; |
| 31 | + |
| 32 | + // Mock the retry to immediately return our mock module |
| 33 | + const retrySpy = vi.spyOn(retryModule, 'retry').mockResolvedValueOnce(mockModule); |
| 34 | + |
| 35 | + const result = await safeImport('./test-module'); |
| 36 | + |
| 37 | + expect(result).toBe(mockModule); |
| 38 | + expect(retrySpy).toHaveBeenCalledTimes(1); |
| 39 | + |
| 40 | + retrySpy.mockRestore(); |
| 41 | + }); |
| 42 | + |
| 43 | + test('propagates import errors after retries', async () => { |
| 44 | + const importError = new Error('Module not found'); |
| 45 | + |
| 46 | + // Mock retry to reject with our error |
| 47 | + const retrySpy = vi.spyOn(retryModule, 'retry').mockRejectedValueOnce(importError); |
| 48 | + |
| 49 | + await expect(safeImport('./non-existent-module')).rejects.toThrow('Module not found'); |
| 50 | + |
| 51 | + retrySpy.mockRestore(); |
| 52 | + }); |
| 53 | + |
| 54 | + test('configures shouldRetry to allow up to 3 retries', async () => { |
| 55 | + const retrySpy = vi.spyOn(retryModule, 'retry'); |
| 56 | + |
| 57 | + try { |
| 58 | + await safeImport('./test-module'); |
| 59 | + } catch { |
| 60 | + // Ignore errors |
| 61 | + } |
| 62 | + |
| 63 | + const options = retrySpy.mock.calls[0][1]; |
| 64 | + const shouldRetry = options?.shouldRetry; |
| 65 | + |
| 66 | + expect(shouldRetry).toBeDefined(); |
| 67 | + if (shouldRetry) { |
| 68 | + // Test the shouldRetry logic |
| 69 | + expect(shouldRetry(new Error('test'), 1)).toBe(true); // First retry |
| 70 | + expect(shouldRetry(new Error('test'), 2)).toBe(true); // Second retry |
| 71 | + expect(shouldRetry(new Error('test'), 3)).toBe(true); // Third retry |
| 72 | + expect(shouldRetry(new Error('test'), 4)).toBe(false); // Fourth attempt should not retry |
| 73 | + } |
| 74 | + |
| 75 | + retrySpy.mockRestore(); |
| 76 | + }); |
| 77 | +}); |
0 commit comments