-
Notifications
You must be signed in to change notification settings - Fork 16
/
fake_timers.javascript.txt
82 lines (64 loc) · 5.58 KB
/
fake_timers.javascript.txt
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
FAKE_TIMERS
ALTERNATIVES ==> # - @sinonjs/fake-timers (prefer):
# - lots of functions mocked
# - more features
# - Node.js mocktimers (prefer if using Node.js core runner and need only few features)
# - core Node.js test runner
# - fewer features
# - jasmine-timers: Jasmine-specific
# - jest timers: Jest-specific
# - Deno:
# - Deno-specific, std library
# - fewer features
# - prefer with Deno, unless need to mock more than Date or *Timeout|Interval()
VERSION ==> #11.2.2
#Node and browser
#Mocks setTimeout, setInterval(), etc.
/=+===============================+=\
/ : : \
)==: CORE :==(
\ :_______________________________: /
\=+===============================+=/
FAKETIMERS.install([OPTS])->CLOCK #Mocks:
# - set|clearTimeout|Immediate|Interval() (including through 'node:timers'), request|cancelAnimationFrame(), Date() (or 'FUNC'...),
# performance.now(), process.nextTick(), queueMicrotask(), request|cancelIdleCallback(), process.hrtime[.bigint](),
# Intl.DateTimeFormat|RelativeTimeFormat
# - so they are synchronous and are advanced with CLOCK.tick(NUM)
#OPTS:
# - now DATE[_NUM] (def: 0)
# - toFake 'FUNC'_ARR (def: all except 'nextTick')
# - shouldAdvanceTime BOOL (def: false):
# - using OPTS.advanceTimeDelta NUM (def: 20)
# - calls (non-mocked) setInterval(() => CLOCK.tick(NUM), NUM)
# - i.e. CLOCK will somewhat follow real time (with NUM increments)
# - shouldClearNativeTimers BOOL (def: false): impact non-mocked methods too
# - loopLimit NUM (def: 1000): if CLOCK.run*() loops more, throw error
CLOCK.uninstall() #Removes mocking
CLOCK.reset() #Cancels current async tasks
#Also resets OPTS.now
FAKETIMERS.createClock
([OPTS.now][, OPTS.loopLimit]) #Same as FAKETIMERS.install() but does not mock
->CLOCK #Instead, use CLOCK.setTimeout|...()
FAKETIMERS.withGlobal(OBJ)
->FAKETIMERS2 #Use a different global object
/=+===============================+=\
/ : : \
)==: ADVANCE :==(
\ :_______________________________: /
\=+===============================+=/
CLOCK.tick(NUM|'[HH:][MM:]SS') #Must be positive. NUM is in ms.
CLOCK.jump(NUM|'[HH:][MM:]SS') #Same but only call setIterval() callbacks once
CLOCK.next() #Do CLOCK.tick() until the first timer's callback
CLOCK.runToFrame() #Do CLOCK.next() until the first requestAnimationFrame()'s callback
CLOCK.runAll() #Do CLOCK.next() until the last timer's callback
CLOCK.runToLast() #Same except if new timers were adding after the last timer, still stops at that last timer
CLOCK.runMicrotasks() #Run all microtasks
CLOCK.setSystemTime(DATE)->CLOCK #Change current DATE returned by performance.now() and Date()
#Does not impact other functions
CLOCK.countTimers()->NUM #NUM of setImmediate|Timeout|Interval() or requestAnimationFrame()
CLOCK.tickAsync(...)->PROMISE
CLOCK.nextAsync(...)->PROMISE
CLOCK.runAllAsync(...)->PROMISE
CLOCK.runToLastAsync(...)->PROMISE#Same but return a PROMISE resolved on next tick