|
1 | 1 | import jsTestValues from "./__fixtures__/curriculum-helpers-javascript"; |
2 | 2 |
|
3 | | -import { getFunctionParams } from "./../helpers/lib/index"; |
| 3 | +import { getFunctionParams, retryingTest } from "./../helpers/lib/index"; |
4 | 4 |
|
5 | 5 | const { |
6 | 6 | functionDeclaration, |
@@ -46,4 +46,128 @@ describe("js-help", () => { |
46 | 46 | expect(parameters[3].name).toBe("...rest"); |
47 | 47 | }); |
48 | 48 | }); |
| 49 | + |
| 50 | + describe("retryingTest", () => { |
| 51 | + beforeEach(() => { |
| 52 | + vi.useFakeTimers(); |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + vi.useRealTimers(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should resolve immediately when test passes on first try", async () => { |
| 60 | + const testFn = vi.fn().mockReturnValue(true); |
| 61 | + |
| 62 | + const promise = retryingTest(testFn, "Test failed"); |
| 63 | + |
| 64 | + await expect(promise).resolves.toBeUndefined(); |
| 65 | + expect(testFn).toHaveBeenCalledTimes(1); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should resolve when test passes after retries", async () => { |
| 69 | + const testFn = vi |
| 70 | + .fn() |
| 71 | + .mockReturnValueOnce(false) |
| 72 | + .mockReturnValueOnce(false) |
| 73 | + .mockReturnValueOnce(true); |
| 74 | + |
| 75 | + const promise = retryingTest(testFn, "Test failed"); |
| 76 | + |
| 77 | + // Run all timers and wait for the promise to settle |
| 78 | + vi.runAllTimers(); |
| 79 | + |
| 80 | + await expect(promise).resolves.toBeUndefined(); |
| 81 | + expect(testFn).toHaveBeenCalledTimes(3); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should reject with error message when all retries fail", async () => { |
| 85 | + const testFn = vi.fn().mockReturnValue(false); |
| 86 | + const errorMessage = "Element not found"; |
| 87 | + |
| 88 | + const promise = retryingTest(testFn, errorMessage, 3); |
| 89 | + |
| 90 | + // Run all timers and wait for the promise to settle |
| 91 | + vi.runAllTimers(); |
| 92 | + |
| 93 | + await expect(promise).rejects.toThrow(errorMessage); |
| 94 | + expect(testFn).toHaveBeenCalledTimes(3); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should reject immediately when tries is less than 1", async () => { |
| 98 | + const testFn = vi.fn(); |
| 99 | + const errorMessage = "Invalid tries"; |
| 100 | + |
| 101 | + const promise = retryingTest(testFn, errorMessage, 0); |
| 102 | + |
| 103 | + await expect(promise).rejects.toThrow(errorMessage); |
| 104 | + expect(testFn).not.toHaveBeenCalled(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should use default tries value of 20", async () => { |
| 108 | + const testFn = vi.fn().mockReturnValue(false); |
| 109 | + |
| 110 | + const promise = retryingTest(testFn, "Test failed"); |
| 111 | + |
| 112 | + // Run all timers and wait for the promise to settle |
| 113 | + vi.runAllTimers(); |
| 114 | + |
| 115 | + await expect(promise).rejects.toThrow("Test failed"); |
| 116 | + expect(testFn).toHaveBeenCalledTimes(20); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should work with truthy values (not just boolean true)", async () => { |
| 120 | + const testFn = vi |
| 121 | + .fn() |
| 122 | + .mockReturnValueOnce(null) |
| 123 | + .mockReturnValueOnce(undefined) |
| 124 | + .mockReturnValueOnce(0) |
| 125 | + .mockReturnValueOnce("") |
| 126 | + .mockReturnValueOnce("found element"); // Truthy |
| 127 | + |
| 128 | + const promise = retryingTest(testFn, "Test failed"); |
| 129 | + |
| 130 | + vi.runAllTimers(); |
| 131 | + |
| 132 | + await expect(promise).resolves.toBeUndefined(); |
| 133 | + expect(testFn).toHaveBeenCalledTimes(5); |
| 134 | + }); |
| 135 | + |
| 136 | + it("should handle DOM element testing scenario", async () => { |
| 137 | + // Mock DOM element that appears after delay |
| 138 | + let mockElement: Element | null = null; |
| 139 | + const testFn = vi.fn(() => mockElement); |
| 140 | + |
| 141 | + // Simulate element appearing after 3 attempts |
| 142 | + setTimeout(() => { |
| 143 | + mockElement = { tagName: "IMG" } as Element; |
| 144 | + }, 3); |
| 145 | + |
| 146 | + const promise = retryingTest(testFn, "'img' element not found"); |
| 147 | + |
| 148 | + vi.runAllTimers(); |
| 149 | + |
| 150 | + await expect(promise).resolves.toBeUndefined(); |
| 151 | + expect(testFn).toHaveBeenCalledTimes(4); |
| 152 | + }); |
| 153 | + |
| 154 | + it("should wait 1ms between retries", async () => { |
| 155 | + const testFn = vi |
| 156 | + .fn() |
| 157 | + .mockReturnValueOnce(false) |
| 158 | + .mockReturnValueOnce(false) |
| 159 | + .mockReturnValueOnce(true); |
| 160 | + |
| 161 | + const promise = retryingTest(testFn, "Test failed"); |
| 162 | + |
| 163 | + // Step through timers manually to verify 1ms delay |
| 164 | + expect(testFn).toHaveBeenCalledTimes(1); |
| 165 | + vi.advanceTimersByTime(1); |
| 166 | + expect(testFn).toHaveBeenCalledTimes(2); |
| 167 | + vi.advanceTimersByTime(1); |
| 168 | + |
| 169 | + await expect(promise).resolves.toBeUndefined(); |
| 170 | + expect(testFn).toHaveBeenCalledTimes(3); |
| 171 | + }); |
| 172 | + }); |
49 | 173 | }); |
0 commit comments