-
Notifications
You must be signed in to change notification settings - Fork 2
/
jest_test.ml
95 lines (78 loc) · 2.46 KB
/
jest_test.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
open Jest
open Expect
open! Expect.Operators
external setTimeout : (unit -> unit) -> int -> unit = "setTimeout"
external setImmediate : (unit -> unit) -> unit = "setImmediate"
external nextTick : (unit -> unit) -> unit = "process.nextTick"
let () =
describe "Fake Timers" (fun () ->
test "runAllTimers" (fun () ->
let flag = ref false in
Jest.useFakeTimers ();
setTimeout (fun () -> flag := true) 0;
let before = !flag in
Jest.runAllTimers ();
expect (before, !flag) = (false, true)
);
test "runAllTicks" (fun () ->
let flag = ref false in
Jest.useFakeTimers ();
nextTick (fun () -> flag := true);
let before = !flag in
Jest.runAllTicks ();
expect (before, !flag) = (false, true)
);
test "runAllImmediates" (fun () ->
let flag = ref false in
Jest.useFakeTimers ~config:{legacyFakeTimers=true} ();
setImmediate (fun () -> flag := true);
let before = !flag in
Jest.runAllImmediates ();
expect (before, !flag) = (false, true)
);
test "runTimersToTime" (fun () ->
let flag = ref false in
Jest.useFakeTimers ();
setTimeout (fun () -> flag := true) 1500;
let before = !flag in
Jest.advanceTimersByTime 1000;
let inbetween = !flag in
Jest.advanceTimersByTime 1000;
expect (before, inbetween, !flag) = (false, false, true)
);
test "advanceTimersByTime" (fun () ->
let flag = ref false in
Jest.useFakeTimers ();
setTimeout(fun () -> flag := true) 1500;
let before = !flag in
Jest.advanceTimersByTime 1000;
let inbetween = !flag in
Jest.advanceTimersByTime 1000;
expect (before, inbetween, !flag) = (false, false, true)
);
test "runOnlyPendingTimers" (fun () ->
let count = ref 0 in
Jest.useFakeTimers ();
let rec recursiveTimeout () = count := !count + 1; setTimeout recursiveTimeout 1500 in
recursiveTimeout ();
let before = !count in
Jest.runOnlyPendingTimers ();
let inbetween = !count in
Jest.runOnlyPendingTimers ();
expect (before, inbetween, !count) = (1, 2, 3)
);
test "clearAllTimers" (fun () ->
let flag = ref false in
Jest.useFakeTimers ();
setImmediate (fun () -> flag := true);
let before = !flag in
Jest.clearAllTimers ();
Jest.runAllTimers ();
expect (before, !flag) = (false, false)
);
testAsync "clearAllTimers" (fun finish ->
Jest.useFakeTimers ();
Jest.useRealTimers ();
nextTick (fun () -> finish pass);
);
);