@@ -407,6 +407,16 @@ describe('Mock Timers Test Suite', () =>{
407407assert . 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+
410420it ( 'should abort operation if timers/promises/setTimeout received an aborted signal' , async ( t ) => {
411421t . mock . timers . enable ( { apis : [ 'setTimeout' ] } ) ;
412422const expectedResult = 'result' ;
@@ -505,10 +515,11 @@ describe('Mock Timers Test Suite', () =>{
505515
506516const expectedIterations = 5 ;
507517const interval = 1000 ;
508- const startedAt = Date . now ( ) ;
518+ let time = 0 ;
509519async function run ( ) {
510520const 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 ;
512523times . push ( time ) ;
513524if ( times . length === expectedIterations ) break ;
514525}
@@ -525,7 +536,20 @@ describe('Mock Timers Test Suite', () =>{
525536const timeResults = await r ;
526537assert . strictEqual ( timeResults . length , expectedIterations ) ;
527538for ( 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', () =>{
579603const signal = controller . signal ;
580604const interval = 200 ;
581605const expectedIterations = 2 ;
582- const startedAt = Date . now ( ) ;
583- const timeResults = [ ] ;
606+ let numIterations = 0 ;
584607async 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', () =>{
601624await assert . rejects ( ( ) => r , {
602625name : '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