@@ -3,8 +3,6 @@ import assert from "node:assert/strict";
33
44const isNode20OrLower = process . versions . node . split ( "." ) [ 0 ] <= "20" ;
55
6- const getCurrentDate = ( ) => new Date ( ) ;
7-
86afterEach ( ( ) => {
97 mock . timers . reset ( ) ;
108} ) ;
@@ -19,19 +17,38 @@ test(
1917 now : new Date ( "2023-05-14T11:01:58.135Z" ) ,
2018 } ) ;
2119
22- assert . deepEqual ( getCurrentDate ( ) , new Date ( "2023-05-14T11:01:58.135Z" ) ) ;
20+ assert . deepEqual ( new Date ( ) , new Date ( "2023-05-14T11:01:58.135Z" ) ) ;
2321
2422 t . mock . timers . reset ( ) ;
2523 assert ( new Date ( ) . getFullYear ( ) > 2023 ) ;
2624 } ,
2725) ;
2826
29- test ( "mock.timers can mock Date.now()" , { skip : isNode20OrLower } , ( t ) => {
30- t . mock . timers . enable ( {
31- // @ts -ignore
32- apis : [ "Date" ] ,
33- now : new Date ( "2023-05-14T11:01:58.135Z" ) ,
34- } ) ;
27+ test (
28+ "mock.timers can mock Date.now() and be ticked forward" ,
29+ { skip : isNode20OrLower } ,
30+ ( t ) => {
31+ t . mock . timers . enable ( {
32+ // @ts -ignore
33+ apis : [ "Date" ] ,
34+ now : new Date ( "2023-05-14T11:01:58.135Z" ) ,
35+ } ) ;
3536
36- assert . deepEqual ( Date . now ( ) , 1684062118135 ) ;
37- } ) ;
37+ assert . deepEqual (
38+ Date . now ( ) ,
39+ new Date ( "2023-05-14T11:01:58.135Z" ) . valueOf ( ) ,
40+ ) ;
41+
42+ // advance by 24h
43+ t . mock . timers . tick ( 1000 * 60 * 60 * 24 ) ;
44+
45+ assert . deepEqual (
46+ Date . now ( ) ,
47+ new Date ( "2023-05-15T11:01:58.135Z" ) . valueOf ( ) ,
48+ ) ;
49+
50+ // @ts -ignore
51+ t . mock . timers . setTime ( 20000 ) ;
52+ assert . deepEqual ( Date . now ( ) , 20000 ) ;
53+ } ,
54+ ) ;
0 commit comments