-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
timers.ts
258 lines (195 loc) · 6.41 KB
/
timers.ts
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// TODO setImmediate, nextTick, requestAnimationFrame, cancelAnimationFrame
// TODO async timers
import type { SpyInstance } from './jest-mock'
import { spyOn } from './jest-mock'
const originalSetTimeout = global.setTimeout
const originalSetInterval = global.setInterval
const originalClearTimeout = global.clearTimeout
const originalClearInterval = global.clearInterval
type Arguments = [cb: (args: void) => void, ms?: number | undefined]
const MAX_LOOPS = 10_000
interface FakeCall {
cb: () => any
ms: number
id: number
nestedMs: number
scopeId: number
}
enum QueueTaskType {
Interval = 'interval',
Timeout = 'timeout',
Immediate = 'immediate',
}
interface QueueTask {
type: QueueTaskType
call: FakeCall
nested: boolean
}
const assertEvery = (assertions: any[], message: string) => {
if (assertions.some(a => !a))
throw new Error(message)
}
const assertMaxLoop = (times: number) => {
if (times >= MAX_LOOPS)
throw new Error('setTimeout/setInterval called 10 000 times. It\'s possible it stuck in an infinite loop.')
}
// TODO should do what NodeJS.Timeout does on refresh
const getNodeTimeout = (id: number): NodeJS.Timeout => {
const timer = {
ref: () => timer,
unref: () => timer,
hasRef: () => true,
refresh: () => timer,
[Symbol.toPrimitive]: () => id,
}
return timer
}
export class FakeTimers {
private _setTimeout!: SpyInstance<Arguments, NodeJS.Timeout>
private _setInterval!: SpyInstance<Arguments, NodeJS.Timeout>
private _clearTimeout!: SpyInstance<[NodeJS.Timeout], void>
private _clearInterval!: SpyInstance<[NodeJS.Timeout], void>
private _advancedTime = 0
private _nestedTime: Record<string, number> = {}
private _scopeId = 0
private _isNested = false
private _isOnlyPending = false
private _spyid = 0
private _isMocked = false
private _tasksQueue: QueueTask[] = []
private _queueCount = 0
public useFakeTimers() {
this._isMocked = true
this.reset()
const spyFactory = (spyType: QueueTaskType, resultBuilder: (id: number, cb: (args: void) => void) => any) => {
return (cb: (args: void) => void, ms = 0) => {
const id = ++this._spyid
// all timers up until this point
const nestedTo = Object.entries(this._nestedTime).filter(([key]) => Number(key) <= this._scopeId)
const nestedMs = nestedTo.reduce((total, [, ms]) => total + ms, ms)
const call: FakeCall = { id, cb, ms, nestedMs, scopeId: this._scopeId }
const task = { type: spyType, call, nested: this._isNested }
this.pushTask(task)
return resultBuilder(id, cb)
}
}
this._setTimeout = spyOn(global, 'setTimeout').mockImplementation(spyFactory(QueueTaskType.Timeout, getNodeTimeout))
this._setInterval = spyOn(global, 'setInterval').mockImplementation(spyFactory(QueueTaskType.Interval, getNodeTimeout))
const clearTimerFactory = (spyType: QueueTaskType) => (id: number | undefined | NodeJS.Timeout) => {
if (id === undefined) return
const index = this._tasksQueue.findIndex(({ call, type }) => type === spyType && call.id === Number(id))
if (index !== -1)
this._tasksQueue.splice(index, 1)
}
this._clearTimeout = spyOn(global, 'clearTimeout').mockImplementation(clearTimerFactory(QueueTaskType.Timeout))
this._clearInterval = spyOn(global, 'clearInterval').mockImplementation(clearTimerFactory(QueueTaskType.Interval))
}
public useRealTimers() {
this._isMocked = false
this.reset()
global.setTimeout = originalSetTimeout
global.setInterval = originalSetInterval
global.clearTimeout = originalClearTimeout
global.clearInterval = originalClearInterval
}
public runOnlyPendingTimers(): void | Promise<void> {
this.assertMocked()
this._isOnlyPending = true
this.runQueue()
}
public runAllTimers(): void | Promise<void> {
this.assertMocked()
this.runQueue()
}
public advanceTimersByTime(ms: number): void | Promise<void> {
this.assertMocked()
this._advancedTime += ms
this.runQueue()
}
public advanceTimersToNextTimer(): void | Promise<void> {
this.assertMocked()
this.callQueueItem(0)
}
public getTimerCount(): number {
this.assertMocked()
return this._tasksQueue.length
}
private reset() {
this._advancedTime = 0
this._nestedTime = {}
this._isNested = false
this._isOnlyPending = false
this._spyid = 0
this._queueCount = 0
this._tasksQueue = []
this._clearInterval?.mockRestore()
this._clearTimeout?.mockRestore()
this._setInterval?.mockRestore()
this._setTimeout?.mockRestore()
}
private callQueueItem(index: number) {
const task = this._tasksQueue[index]
if (!task) return
const { call, type } = task
this._scopeId = call.id
this._isNested = true
this._nestedTime[call.id] ??= 0
this._nestedTime[call.id] += call.ms
if (type === 'timeout') {
this.removeTask(index)
}
else if (type === 'interval') {
call.nestedMs += call.ms
const nestedMs = call.nestedMs
const closestTask = this._tasksQueue.findIndex(({ type, call }) => type === 'interval' && call.nestedMs < nestedMs)
if (closestTask !== -1 && closestTask !== index)
this.ensureQueueOrder()
}
call.cb()
this._queueCount++
}
private runQueue() {
let index = 0
while (this._tasksQueue[index]) {
assertMaxLoop(this._queueCount)
const { call, nested } = this._tasksQueue[index]
if (this._advancedTime && call.nestedMs > this._advancedTime)
break
if (this._isOnlyPending && nested) {
index++
continue
}
this.callQueueItem(index)
}
}
private removeTask(index: number) {
if (index === 0)
this._tasksQueue.shift()
else
this._tasksQueue.splice(index, 1)
}
private pushTask(task: QueueTask) {
this._tasksQueue.push(task)
this.ensureQueueOrder()
}
private ensureQueueOrder() {
this._tasksQueue.sort((t1, t2) => {
const diff = t1.call.nestedMs - t2.call.nestedMs
if (diff === 0) {
if (t1.type === QueueTaskType.Immediate && t2.type !== QueueTaskType.Immediate)
return 1
return 0
}
return diff
})
}
private assertMocked() {
assertEvery([
this._isMocked,
this._setTimeout,
this._setInterval,
this._clearTimeout,
this._clearInterval,
], 'timers are not mocked. try calling "vi.useFakeTimers()" first')
}
}