@@ -407,6 +407,16 @@ describe('Mock Timers Test Suite', () => {
407407 assert . strictEqual ( result , expectedResult ) ;
408408 } ) ;
409409
410+ it ( 'should always return the same result as the original timers/promises/setTimeout' , async ( t ) => {
411+ t . mock . timers . enable ( { apis : [ 'setTimeout' ] } ) ;
412+ for ( const expectedResult of [ undefined , null , false , true , 0 , 0n , 1 , 1n , '' , 'result' , { } ] ) {
413+ const p = nodeTimersPromises . setTimeout ( 2000 , expectedResult ) ;
414+ t . mock . timers . tick ( 2000 ) ;
415+ const result = await p ;
416+ assert . strictEqual ( result , expectedResult ) ;
417+ }
418+ } ) ;
419+
410420 it ( 'should abort operation if timers/promises/setTimeout received an aborted signal' , async ( t ) => {
411421 t . mock . timers . enable ( { apis : [ 'setTimeout' ] } ) ;
412422 const expectedResult = 'result' ;
@@ -505,10 +515,11 @@ describe('Mock Timers Test Suite', () => {
505515
506516 const expectedIterations = 5 ;
507517 const interval = 1000 ;
508- const startedAt = Date . now ( ) ;
518+ let time = 0 ;
509519 async function run ( ) {
510520 const times = [ ] ;
511- for await ( const time of nodeTimersPromises . setInterval ( interval , startedAt ) ) {
521+ for await ( const _ of nodeTimersPromises . setInterval ( interval ) ) { // eslint-disable-line no-unused-vars
522+ time += interval ;
512523 times . push ( time ) ;
513524 if ( times . length === expectedIterations ) break ;
514525 }
@@ -525,7 +536,20 @@ describe('Mock Timers Test Suite', () => {
525536 const timeResults = await r ;
526537 assert . strictEqual ( timeResults . length , expectedIterations ) ;
527538 for ( let it = 1 ; it < expectedIterations ; it ++ ) {
528- assert . strictEqual ( timeResults [ it - 1 ] , startedAt + ( interval * it ) ) ;
539+ assert . strictEqual ( timeResults [ it - 1 ] , interval * it ) ;
540+ }
541+ } ) ;
542+
543+ it ( 'should always return the same result as the original timers/promises/setInterval' , async ( t ) => {
544+ t . mock . timers . enable ( { apis : [ 'setInterval' ] } ) ;
545+ for ( const expectedResult of [ undefined , null , false , true , 0 , 0n , 1 , 1n , '' , 'result' , { } ] ) {
546+ const intervalIterator = nodeTimersPromises . setInterval ( 2000 , expectedResult ) ;
547+ const p = intervalIterator . next ( ) ;
548+ t . mock . timers . tick ( 2000 ) ;
549+ const result = await p ;
550+ await intervalIterator . return ( ) ;
551+ assert . strictEqual ( result . done , false ) ;
552+ assert . strictEqual ( result . value , expectedResult ) ;
529553 }
530554 } ) ;
531555
@@ -579,13 +603,12 @@ describe('Mock Timers Test Suite', () => {
579603 const signal = controller . signal ;
580604 const interval = 200 ;
581605 const expectedIterations = 2 ;
582- const startedAt = Date . now ( ) ;
583- const timeResults = [ ] ;
606+ let numIterations = 0 ;
584607 async function run ( ) {
585- const it = nodeTimersPromises . setInterval ( interval , startedAt , { signal } ) ;
586- for await ( const time of it ) {
587- timeResults . push ( time ) ;
588- if ( timeResults . length === 5 ) break ;
608+ const it = nodeTimersPromises . setInterval ( interval , undefined , { signal } ) ;
609+ for await ( const _ of it ) { // eslint-disable-line no-unused-vars
610+ numIterations += 1 ;
611+ if ( numIterations === 5 ) break ;
589612 }
590613 }
591614
@@ -601,11 +624,7 @@ describe('Mock Timers Test Suite', () => {
601624 await assert . rejects ( ( ) => r , {
602625 name : 'AbortError' ,
603626 } ) ;
604- assert . strictEqual ( timeResults . length , expectedIterations ) ;
605-
606- for ( let it = 1 ; it < expectedIterations ; it ++ ) {
607- assert . strictEqual ( timeResults [ it - 1 ] , startedAt + ( interval * it ) ) ;
608- }
627+ assert . strictEqual ( numIterations , expectedIterations ) ;
609628 } ) ;
610629 } ) ;
611630 } ) ;
0 commit comments