@@ -10,7 +10,7 @@ import {
10
10
getWorkflowRunJobSteps ,
11
11
getWorkflowRunUrl ,
12
12
init ,
13
- retryOrDie ,
13
+ retryOrTimeout ,
14
14
} from "./api.ts" ;
15
15
16
16
vi . mock ( "@actions/core" ) ;
@@ -427,7 +427,7 @@ describe("API", () => {
427
427
} ) ;
428
428
} ) ;
429
429
430
- describe ( "retryOrDie " , ( ) => {
430
+ describe ( "retryOrTimeout " , ( ) => {
431
431
beforeEach ( ( ) => {
432
432
vi . useFakeTimers ( ) ;
433
433
} ) ;
@@ -436,38 +436,54 @@ describe("API", () => {
436
436
vi . useRealTimers ( ) ;
437
437
} ) ;
438
438
439
- it ( "should return a populated array" , async ( ) => {
440
- const attempt = ( ) => Promise . resolve ( [ 0 ] ) ;
441
- expect ( await retryOrDie ( attempt , 1000 ) ) . toHaveLength ( 1 ) ;
439
+ it ( "should return a result" , async ( ) => {
440
+ const attemptResult = [ 0 ] ;
441
+ const attempt = ( ) => Promise . resolve ( attemptResult ) ;
442
+
443
+ const result = await retryOrTimeout ( attempt , 1000 ) ;
444
+ if ( result . timeout ) {
445
+ expect . fail ( "expected retryOrTimeout not to timeout" ) ;
446
+ }
447
+
448
+ expect ( result . timeout ) . toStrictEqual ( false ) ;
449
+ expect ( result . value ) . toStrictEqual ( attemptResult ) ;
442
450
} ) ;
443
451
444
- it ( "should throw if the given timeout is exceeded" , async ( ) => {
452
+ it ( "should return a timeout result if the given timeout is exceeded" , async ( ) => {
445
453
// Never return data.
446
454
const attempt = ( ) => Promise . resolve ( [ ] ) ;
447
455
448
- const retryOrDiePromise = retryOrDie ( attempt , 1000 ) ;
449
- vi . advanceTimersByTime ( 2000 ) ;
456
+ const retryOrTimeoutPromise = retryOrTimeout ( attempt , 1000 ) ;
450
457
// eslint-disable-next-line @typescript-eslint/no-floating-promises
451
458
vi . advanceTimersByTimeAsync ( 2000 ) ;
452
459
453
- await expect ( retryOrDiePromise ) . rejects . toThrow (
454
- "Timed out while attempting to fetch data" ,
455
- ) ;
460
+ const result = await retryOrTimeoutPromise ;
461
+ if ( ! result . timeout ) {
462
+ expect . fail ( "expected retryOrTimeout to timeout" ) ;
463
+ }
464
+
465
+ expect ( result . timeout ) . toStrictEqual ( true ) ;
456
466
} ) ;
457
467
458
468
it ( "should retry to get a populated array" , async ( ) => {
469
+ const attemptResult = [ 0 ] ;
459
470
const attempt = vi
460
471
. fn ( )
461
- . mockResolvedValue ( [ 0 ] )
472
+ . mockResolvedValue ( attemptResult )
462
473
. mockResolvedValueOnce ( [ ] )
463
474
. mockResolvedValueOnce ( [ ] ) ;
464
475
465
- const retryOrDiePromise = retryOrDie ( attempt , 5000 ) ;
466
- vi . advanceTimersByTime ( 3000 ) ;
476
+ const retryOrDiePromise = retryOrTimeout ( attempt , 5000 ) ;
467
477
// eslint-disable-next-line @typescript-eslint/no-floating-promises
468
478
vi . advanceTimersByTimeAsync ( 3000 ) ;
469
479
470
- expect ( await retryOrDiePromise ) . toHaveLength ( 1 ) ;
480
+ const result = await retryOrDiePromise ;
481
+ if ( result . timeout ) {
482
+ expect . fail ( "expected retryOrTimeout not to timeout" ) ;
483
+ }
484
+
485
+ expect ( result . timeout ) . toStrictEqual ( false ) ;
486
+ expect ( result . value ) . toStrictEqual ( attemptResult ) ;
471
487
expect ( attempt ) . toHaveBeenCalledTimes ( 3 ) ;
472
488
} ) ;
473
489
} ) ;
0 commit comments