1
+ // Flags: --expose-internals
1
2
'use strict' ;
2
3
process . env . NODE_TEST_KNOWN_GLOBALS = 0 ;
3
4
const common = require ( '../common' ) ;
@@ -6,6 +7,7 @@ const assert = require('node:assert');
6
7
const { it, mock, describe } = require ( 'node:test' ) ;
7
8
const nodeTimers = require ( 'node:timers' ) ;
8
9
const nodeTimersPromises = require ( 'node:timers/promises' ) ;
10
+ const { TIMEOUT_MAX } = require ( 'internal/timers' ) ;
9
11
10
12
describe ( 'Mock Timers Test Suite' , ( ) => {
11
13
describe ( 'MockTimers API' , ( ) => {
@@ -252,10 +254,10 @@ describe('Mock Timers Test Suite', () => {
252
254
} ) , timeout ) ;
253
255
} ) ;
254
256
255
- it ( 'should change timeout to 1ms when it is >= 2 ** 31 ' , ( t ) => {
257
+ it ( 'should change timeout to 1ms when it is > TIMEOUT_MAX ' , ( t ) => {
256
258
t . mock . timers . enable ( { apis : [ 'setTimeout' ] } ) ;
257
259
const fn = t . mock . fn ( ) ;
258
- global . setTimeout ( fn , 2 ** 31 ) ;
260
+ global . setTimeout ( fn , TIMEOUT_MAX + 1 ) ;
259
261
t . mock . timers . tick ( 1 ) ;
260
262
assert . strictEqual ( fn . mock . callCount ( ) , 1 ) ;
261
263
} ) ;
@@ -791,240 +793,6 @@ describe('Mock Timers Test Suite', () => {
791
793
} ) ;
792
794
} ) ;
793
795
794
- describe ( 'scheduler Suite' , ( ) => {
795
- describe ( 'scheduler.wait' , ( ) => {
796
- it ( 'should advance in time and trigger timers when calling the .tick function' , ( t ) => {
797
- t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
798
-
799
- const now = Date . now ( ) ;
800
- const durationAtMost = 100 ;
801
-
802
- const p = nodeTimersPromises . scheduler . wait ( 4000 ) ;
803
- t . mock . timers . tick ( 4000 ) ;
804
-
805
- return p . then ( common . mustCall ( ( result ) => {
806
- assert . strictEqual ( result , undefined ) ;
807
- assert . ok (
808
- Date . now ( ) - now < durationAtMost ,
809
- `time should be advanced less than the ${ durationAtMost } ms`
810
- ) ;
811
- } ) ) ;
812
- } ) ;
813
-
814
- it ( 'should advance in time and trigger timers when calling the .tick function multiple times' , async ( t ) => {
815
- t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
816
-
817
- const fn = t . mock . fn ( ) ;
818
-
819
- nodeTimersPromises . scheduler . wait ( 9999 ) . then ( fn ) ;
820
-
821
- t . mock . timers . tick ( 8999 ) ;
822
- assert . strictEqual ( fn . mock . callCount ( ) , 0 ) ;
823
- t . mock . timers . tick ( 500 ) ;
824
-
825
- await nodeTimersPromises . setImmediate ( ) ;
826
-
827
- assert . strictEqual ( fn . mock . callCount ( ) , 0 ) ;
828
- t . mock . timers . tick ( 500 ) ;
829
-
830
- await nodeTimersPromises . setImmediate ( ) ;
831
- assert . strictEqual ( fn . mock . callCount ( ) , 1 ) ;
832
- } ) ;
833
-
834
- it ( 'should work with the same params as the original timers/promises/scheduler.wait' , async ( t ) => {
835
- t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
836
- const controller = new AbortController ( ) ;
837
- const p = nodeTimersPromises . scheduler . wait ( 2000 , {
838
- ref : true ,
839
- signal : controller . signal ,
840
- } ) ;
841
-
842
- t . mock . timers . tick ( 1000 ) ;
843
- t . mock . timers . tick ( 500 ) ;
844
- t . mock . timers . tick ( 500 ) ;
845
- t . mock . timers . tick ( 500 ) ;
846
-
847
- const result = await p ;
848
- assert . strictEqual ( result , undefined ) ;
849
- } ) ;
850
-
851
- it ( 'should abort operation if timers/promises/scheduler.wait received an aborted signal' , async ( t ) => {
852
- t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
853
- const controller = new AbortController ( ) ;
854
- const p = nodeTimersPromises . scheduler . wait ( 2000 , {
855
- ref : true ,
856
- signal : controller . signal ,
857
- } ) ;
858
-
859
- t . mock . timers . tick ( 1000 ) ;
860
- controller . abort ( ) ;
861
- t . mock . timers . tick ( 500 ) ;
862
- t . mock . timers . tick ( 500 ) ;
863
- t . mock . timers . tick ( 500 ) ;
864
-
865
- await assert . rejects ( ( ) => p , {
866
- name : 'AbortError' ,
867
- } ) ;
868
- } ) ;
869
- it ( 'should abort operation even if the .tick was not called' , async ( t ) => {
870
- t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
871
- const controller = new AbortController ( ) ;
872
- const p = nodeTimersPromises . scheduler . wait ( 2000 , {
873
- ref : true ,
874
- signal : controller . signal ,
875
- } ) ;
876
-
877
- controller . abort ( ) ;
878
-
879
- await assert . rejects ( ( ) => p , {
880
- name : 'AbortError' ,
881
- } ) ;
882
- } ) ;
883
-
884
- it ( 'should abort operation when .abort is called before calling setInterval' , async ( t ) => {
885
- t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
886
- const controller = new AbortController ( ) ;
887
- controller . abort ( ) ;
888
- const p = nodeTimersPromises . scheduler . wait ( 2000 , {
889
- ref : true ,
890
- signal : controller . signal ,
891
- } ) ;
892
-
893
- await assert . rejects ( ( ) => p , {
894
- name : 'AbortError' ,
895
- } ) ;
896
- } ) ;
897
-
898
- it ( 'should reject given an an invalid signal instance' , async ( t ) => {
899
- t . mock . timers . enable ( { apis : [ 'scheduler.wait' ] } ) ;
900
- const p = nodeTimersPromises . scheduler . wait ( 2000 , {
901
- ref : true ,
902
- signal : { } ,
903
- } ) ;
904
-
905
- await assert . rejects ( ( ) => p , {
906
- name : 'TypeError' ,
907
- code : 'ERR_INVALID_ARG_TYPE' ,
908
- } ) ;
909
- } ) ;
910
-
911
- } ) ;
912
- } ) ;
913
-
914
- describe ( 'Date Suite' , ( ) => {
915
- it ( 'should return the initial UNIX epoch if not specified' , ( t ) => {
916
- t . mock . timers . enable ( { apis : [ 'Date' ] } ) ;
917
- const date = new Date ( ) ;
918
- assert . strictEqual ( date . getTime ( ) , 0 ) ;
919
- assert . strictEqual ( Date . now ( ) , 0 ) ;
920
- } ) ;
921
-
922
- it ( 'should throw an error if setTime is called without enabling timers' , ( t ) => {
923
- assert . throws (
924
- ( ) => {
925
- t . mock . timers . setTime ( 100 ) ;
926
- } ,
927
- { code : 'ERR_INVALID_STATE' }
928
- ) ;
929
- } ) ;
930
-
931
- it ( 'should throw an error if epoch passed to enable is not valid' , ( t ) => {
932
- assert . throws (
933
- ( ) => {
934
- t . mock . timers . enable ( { now : - 1 } ) ;
935
- } ,
936
- { code : 'ERR_INVALID_ARG_VALUE' }
937
- ) ;
938
-
939
- assert . throws (
940
- ( ) => {
941
- t . mock . timers . enable ( { now : 'string' } ) ;
942
- } ,
943
- { code : 'ERR_INVALID_ARG_TYPE' }
944
- ) ;
945
-
946
- assert . throws (
947
- ( ) => {
948
- t . mock . timers . enable ( { now : NaN } ) ;
949
- } ,
950
- { code : 'ERR_INVALID_ARG_VALUE' }
951
- ) ;
952
- } ) ;
953
-
954
- it ( 'should replace the original Date with the mocked one' , ( t ) => {
955
- t . mock . timers . enable ( { apis : [ 'Date' ] } ) ;
956
- assert . ok ( Date . isMock ) ;
957
- } ) ;
958
-
959
- it ( 'should return the ticked time when calling Date.now after tick' , ( t ) => {
960
- t . mock . timers . enable ( { apis : [ 'Date' ] } ) ;
961
- const time = 100 ;
962
- t . mock . timers . tick ( time ) ;
963
- assert . strictEqual ( Date . now ( ) , time ) ;
964
- } ) ;
965
-
966
- it ( 'should return the Date as string when calling it as a function' , ( t ) => {
967
- t . mock . timers . enable ( { apis : [ 'Date' ] } ) ;
968
- const returned = Date ( ) ;
969
- // Matches the format: 'Mon Jan 01 1970 00:00:00'
970
- // We don't care about the date, just the format
971
- assert . ok ( / \w { 3 } \s \w { 3 } \s \d { 1 , 2 } \s \d { 2 , 4 } \s \d { 1 , 2 } : \d { 2 } : \d { 2 } / . test ( returned ) ) ;
972
- } ) ;
973
-
974
- it ( 'should return the date with different argument calls' , ( t ) => {
975
- t . mock . timers . enable ( { apis : [ 'Date' ] } ) ;
976
- assert . strictEqual ( new Date ( 0 ) . getTime ( ) , 0 ) ;
977
- assert . strictEqual ( new Date ( 100 ) . getTime ( ) , 100 ) ;
978
- assert . strictEqual ( new Date ( '1970-01-01T00:00:00.000Z' ) . getTime ( ) , 0 ) ;
979
- assert . strictEqual ( new Date ( 1970 , 0 ) . getFullYear ( ) , 1970 ) ;
980
- assert . strictEqual ( new Date ( 1970 , 0 ) . getMonth ( ) , 0 ) ;
981
- assert . strictEqual ( new Date ( 1970 , 0 , 1 ) . getDate ( ) , 1 ) ;
982
- assert . strictEqual ( new Date ( 1970 , 0 , 1 , 11 ) . getHours ( ) , 11 ) ;
983
- assert . strictEqual ( new Date ( 1970 , 0 , 1 , 11 , 10 ) . getMinutes ( ) , 10 ) ;
984
- assert . strictEqual ( new Date ( 1970 , 0 , 1 , 11 , 10 , 45 ) . getSeconds ( ) , 45 ) ;
985
- assert . strictEqual ( new Date ( 1970 , 0 , 1 , 11 , 10 , 45 , 898 ) . getMilliseconds ( ) , 898 ) ;
986
- assert . strictEqual ( new Date ( 1970 , 0 , 1 , 11 , 10 , 45 , 898 ) . toDateString ( ) , 'Thu Jan 01 1970' ) ;
987
- } ) ;
988
-
989
- it ( 'should return native code when calling Date.toString' , ( t ) => {
990
- t . mock . timers . enable ( { apis : [ 'Date' ] } ) ;
991
- assert . strictEqual ( Date . toString ( ) , 'function Date() { [native code] }' ) ;
992
- } ) ;
993
-
994
- it ( 'should start with a custom epoch if the second argument is specified' , ( t ) => {
995
- t . mock . timers . enable ( { apis : [ 'Date' ] , now : 100 } ) ;
996
- const date1 = new Date ( ) ;
997
- assert . strictEqual ( date1 . getTime ( ) , 100 ) ;
998
-
999
- t . mock . timers . reset ( ) ;
1000
- t . mock . timers . enable ( { apis : [ 'Date' ] , now : new Date ( 200 ) } ) ;
1001
- const date2 = new Date ( ) ;
1002
- assert . strictEqual ( date2 . getTime ( ) , 200 ) ;
1003
- } ) ;
1004
-
1005
- it ( 'should replace epoch if setTime is lesser than now and not tick' , ( t ) => {
1006
- t . mock . timers . enable ( ) ;
1007
- const fn = t . mock . fn ( ) ;
1008
- const id = setTimeout ( fn , 1000 ) ;
1009
- t . mock . timers . setTime ( 800 ) ;
1010
- assert . strictEqual ( Date . now ( ) , 800 ) ;
1011
- t . mock . timers . setTime ( 500 ) ;
1012
- assert . strictEqual ( Date . now ( ) , 500 ) ;
1013
- assert . strictEqual ( fn . mock . callCount ( ) , 0 ) ;
1014
- clearTimeout ( id ) ;
1015
- } ) ;
1016
-
1017
- it ( 'should not tick time when setTime is called' , ( t ) => {
1018
- t . mock . timers . enable ( ) ;
1019
- const fn = t . mock . fn ( ) ;
1020
- const id = setTimeout ( fn , 1000 ) ;
1021
- t . mock . timers . setTime ( 1200 ) ;
1022
- assert . strictEqual ( Date . now ( ) , 1200 ) ;
1023
- assert . strictEqual ( fn . mock . callCount ( ) , 0 ) ;
1024
- clearTimeout ( id ) ;
1025
- } ) ;
1026
- } ) ;
1027
-
1028
796
describe ( 'Api should have same public properties as original' , ( ) => {
1029
797
it ( 'should have hasRef' , ( t ) => {
1030
798
t . mock . timers . enable ( ) ;
0 commit comments