/
wait.ts
173 lines (147 loc) · 4.76 KB
/
wait.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
import { getSafeTimers } from '@vitest/utils'
import { vi } from './vi'
// The waitFor function was inspired by https://github.com/testing-library/web-testing-library/pull/2
export type WaitForCallback<T> = () => T | Promise<T>
export interface WaitForOptions {
/**
* @description Time in ms between each check callback
* @default 50ms
*/
interval?: number
/**
* @description Time in ms after which the throw a timeout error
* @default 1000ms
*/
timeout?: number
}
function copyStackTrace(target: Error, source: Error) {
if (source.stack !== undefined)
target.stack = source.stack.replace(source.message, target.message)
return target
}
export function waitFor<T>(callback: WaitForCallback<T>, options: number | WaitForOptions = {}) {
const { setTimeout, setInterval, clearTimeout, clearInterval } = getSafeTimers()
const { interval = 50, timeout = 1000 } = typeof options === 'number' ? { timeout: options } : options
const STACK_TRACE_ERROR = new Error('STACK_TRACE_ERROR')
return new Promise<T>((resolve, reject) => {
let lastError: unknown
let promiseStatus: 'idle' | 'pending' | 'resolved' | 'rejected' = 'idle'
let timeoutId: ReturnType<typeof setTimeout>
let intervalId: ReturnType<typeof setInterval>
const onResolve = (result: T) => {
if (timeoutId)
clearTimeout(timeoutId)
if (intervalId)
clearInterval(intervalId)
resolve(result)
}
const handleTimeout = () => {
let error = lastError
if (!error)
error = copyStackTrace(new Error('Timed out in waitFor!'), STACK_TRACE_ERROR)
reject(error)
}
const checkCallback = () => {
if (vi.isFakeTimers())
vi.advanceTimersByTime(interval)
if (promiseStatus === 'pending')
return
try {
const result = callback()
if (
result !== null
&& typeof result === 'object'
&& typeof (result as any).then === 'function'
) {
const thenable = result as PromiseLike<T>
promiseStatus = 'pending'
thenable.then(
(resolvedValue) => {
promiseStatus = 'resolved'
onResolve(resolvedValue)
},
(rejectedValue) => {
promiseStatus = 'rejected'
lastError = rejectedValue
},
)
}
else {
onResolve(result as T)
return true
}
}
catch (error) {
lastError = error
}
}
if (checkCallback() === true)
return
timeoutId = setTimeout(handleTimeout, timeout)
intervalId = setInterval(checkCallback, interval)
})
}
export type WaitUntilCallback<T> = () => T | Promise<T>
export interface WaitUntilOptions extends Pick<WaitForOptions, 'interval' | 'timeout'> {}
export function waitUntil<T>(callback: WaitUntilCallback<T>, options: number | WaitUntilOptions = {}) {
const { setTimeout, setInterval, clearTimeout, clearInterval } = getSafeTimers()
const { interval = 50, timeout = 1000 } = typeof options === 'number' ? { timeout: options } : options
const STACK_TRACE_ERROR = new Error('STACK_TRACE_ERROR')
return new Promise<T>((resolve, reject) => {
let promiseStatus: 'idle' | 'pending' | 'resolved' | 'rejected' = 'idle'
let timeoutId: ReturnType<typeof setTimeout>
let intervalId: ReturnType<typeof setInterval>
const onReject = (error?: Error) => {
if (!error)
error = copyStackTrace(new Error('Timed out in waitUntil!'), STACK_TRACE_ERROR)
reject(error)
}
const onResolve = (result: T) => {
if (!result)
return
if (timeoutId)
clearTimeout(timeoutId)
if (intervalId)
clearInterval(intervalId)
resolve(result)
return true
}
const checkCallback = () => {
if (vi.isFakeTimers())
vi.advanceTimersByTime(interval)
if (promiseStatus === 'pending')
return
try {
const result = callback()
if (
result !== null
&& typeof result === 'object'
&& typeof (result as any).then === 'function'
) {
const thenable = result as PromiseLike<T>
promiseStatus = 'pending'
thenable.then(
(resolvedValue) => {
promiseStatus = 'resolved'
onResolve(resolvedValue)
},
(rejectedValue) => {
promiseStatus = 'rejected'
onReject(rejectedValue)
},
)
}
else {
return onResolve(result as T)
}
}
catch (error) {
onReject(error as Error)
}
}
if (checkCallback() === true)
return
timeoutId = setTimeout(onReject, timeout)
intervalId = setInterval(checkCallback, interval)
})
}