@@ -132,19 +132,21 @@ describe('Retry', () => {
132132 } ) ;
133133
134134 retry ( throwFn , {
135- maxRetries : 30 ,
136135 retryDelay : 0 ,
137- // Exponential backoff starting at 1s, doubling each time, up to a maximum of 64s, yielding a total timeout of 127s
136+ // Exponential backoff starting at 1s, doubling each time, up to a maximum of 64s and max 8 retries, yielding a total timeout of 127s
137+ maxRetries : 7 ,
138138 exponentialBackoffDelay : 1000 , // 1s
139139 maxExponentialBackoffDelay : 64000 , // 64s
140- } ) ( ) ;
140+ } ) ( ) . catch ( ( ) => {
141+ // Do nothing
142+ } ) ;
141143
142- // Should call immediately (0ms total elapsed)
144+ // Should call immediately (1 total calls, 0ms total elapsed)
143145 expect ( throwFn ) . toHaveBeenCalledTimes ( 1 ) ;
144146
145147 expect ( Date . now ( ) ) . toBe ( 0 ) ;
146148
147- // Call 2nd time after 1s (1000ms total elapsed)
149+ // 1st retry after 1s (2 total calls, 1000ms total elapsed)
148150 jest . advanceTimersByTime ( 999 ) ;
149151 await Promise . resolve ( ) ;
150152 expect ( throwFn ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -153,7 +155,7 @@ describe('Retry', () => {
153155 expect ( throwFn ) . toHaveBeenCalledTimes ( 2 ) ;
154156 expect ( Date . now ( ) ) . toBe ( 1000 ) ;
155157
156- // Call 3rd time after 3s (3000ms total elapsed)
158+ // 2nd retry after 3s (3 total calls, 3000ms total elapsed)
157159 jest . advanceTimersByTime ( 1999 ) ;
158160 await Promise . resolve ( ) ;
159161 expect ( throwFn ) . toHaveBeenCalledTimes ( 2 ) ;
@@ -162,7 +164,7 @@ describe('Retry', () => {
162164 expect ( throwFn ) . toHaveBeenCalledTimes ( 3 ) ;
163165 expect ( Date . now ( ) ) . toBe ( 3000 ) ;
164166
165- // Call 4th time after 4s (7000ms total elapsed)
167+ // 3rd retry after 4s (4 total calls, 7000ms total elapsed)
166168 jest . advanceTimersByTime ( 3999 ) ;
167169 await Promise . resolve ( ) ;
168170 expect ( throwFn ) . toHaveBeenCalledTimes ( 3 ) ;
@@ -171,7 +173,7 @@ describe('Retry', () => {
171173 expect ( throwFn ) . toHaveBeenCalledTimes ( 4 ) ;
172174 expect ( Date . now ( ) ) . toBe ( 7000 ) ;
173175
174- // Call 5th time after 8s (15000ms total elapsed)
176+ // 4th retry after 8s (5 total calls, 15000ms total elapsed)
175177 jest . advanceTimersByTime ( 7999 ) ;
176178 await Promise . resolve ( ) ;
177179 expect ( throwFn ) . toHaveBeenCalledTimes ( 4 ) ;
@@ -180,7 +182,7 @@ describe('Retry', () => {
180182 expect ( throwFn ) . toHaveBeenCalledTimes ( 5 ) ;
181183 expect ( Date . now ( ) ) . toBe ( 15000 ) ;
182184
183- // Call 6th time after 16s (31000ms total elapsed)
185+ // 5th retry after 16s (6 total calls, 31000ms total elapsed)
184186 jest . advanceTimersByTime ( 15999 ) ;
185187 await Promise . resolve ( ) ;
186188 expect ( throwFn ) . toHaveBeenCalledTimes ( 5 ) ;
@@ -189,7 +191,7 @@ describe('Retry', () => {
189191 expect ( throwFn ) . toHaveBeenCalledTimes ( 6 ) ;
190192 expect ( Date . now ( ) ) . toBe ( 31000 ) ;
191193
192- // Call 7th time after 32s (63000ms total elapsed)
194+ // 6th retry after 32s (7 total calls, 63000ms total elapsed)
193195 jest . advanceTimersByTime ( 31999 ) ;
194196 await Promise . resolve ( ) ;
195197 expect ( throwFn ) . toHaveBeenCalledTimes ( 6 ) ;
@@ -198,7 +200,7 @@ describe('Retry', () => {
198200 expect ( throwFn ) . toHaveBeenCalledTimes ( 7 ) ;
199201 expect ( Date . now ( ) ) . toBe ( 63000 ) ;
200202
201- // Call 8th time after 64s (127000ms total elapsed)
203+ // 7th retry after 64s (8 total calls, 127000ms total elapsed)
202204 jest . advanceTimersByTime ( 63999 ) ;
203205 await Promise . resolve ( ) ;
204206 expect ( throwFn ) . toHaveBeenCalledTimes ( 7 ) ;
@@ -207,6 +209,12 @@ describe('Retry', () => {
207209 expect ( throwFn ) . toHaveBeenCalledTimes ( 8 ) ;
208210 expect ( Date . now ( ) ) . toBe ( 127000 ) ;
209211
212+ // No further retries
213+ jest . advanceTimersByTime ( 1000000000 ) ;
214+ await Promise . resolve ( ) ;
215+ expect ( throwFn ) . toHaveBeenCalledTimes ( 8 ) ;
216+ expect ( Date . now ( ) ) . toBe ( 1000127000 ) ;
217+
210218 jest . useRealTimers ( ) ;
211219 } ) ;
212220} ) ;
0 commit comments