@@ -154,3 +154,69 @@ test('delay: fn', async () => {
154154 ) . rejects . toThrowError ( 'test' )
155155 expect ( end > 1000 && end < 1020 ) . toBe ( true )
156156} )
157+
158+ test ( 'signal: already aborted' , async ( ) => {
159+ let retryTimes = 0
160+ const controller = new AbortController ( )
161+ controller . abort ( )
162+
163+ await expect (
164+ withRetry (
165+ async ( ) => {
166+ retryTimes ++
167+ throw new Error ( 'test' )
168+ } ,
169+ { signal : controller . signal } ,
170+ ) ,
171+ ) . rejects . toThrow ( 'This operation was aborted' )
172+ expect ( retryTimes ) . toBe ( 0 )
173+ } )
174+
175+ test ( 'signal: aborted during retries' , async ( ) => {
176+ let retryTimes = 0
177+ const controller = new AbortController ( )
178+
179+ await expect (
180+ withRetry (
181+ async ( ) => {
182+ retryTimes ++
183+ if ( retryTimes === 1 ) controller . abort ( )
184+ throw new Error ( 'test' )
185+ } ,
186+ { signal : controller . signal , delay : 0 } ,
187+ ) ,
188+ ) . rejects . toThrow ( 'This operation was aborted' )
189+ expect ( retryTimes ) . toBe ( 1 )
190+ } )
191+
192+ test ( 'signal: not aborted' , async ( ) => {
193+ let retryTimes = 0
194+ const controller = new AbortController ( )
195+
196+ const result = await withRetry (
197+ async ( ) => {
198+ retryTimes ++
199+ if ( retryTimes < 2 ) throw new Error ( 'test' )
200+ return 'success'
201+ } ,
202+ { signal : controller . signal , delay : 0 } ,
203+ )
204+
205+ expect ( result ) . toBe ( 'success' )
206+ expect ( retryTimes ) . toBe ( 2 )
207+ } )
208+
209+ test ( 'signal: aborted with custom reason' , async ( ) => {
210+ const controller = new AbortController ( )
211+ const customError = new Error ( 'Custom abort reason' )
212+ controller . abort ( customError )
213+
214+ await expect (
215+ withRetry (
216+ async ( ) => {
217+ throw new Error ( 'test' )
218+ } ,
219+ { signal : controller . signal } ,
220+ ) ,
221+ ) . rejects . toThrow ( 'Custom abort reason' )
222+ } )
0 commit comments