-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathasyncUtils.ts
227 lines (195 loc) · 7.04 KB
/
asyncUtils.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
import {
Act,
WaitOptions,
WaitForOptions,
WaitForValueToChangeOptions,
WaitForNextUpdateOptions,
AsyncUtils
} from '../types'
import { createTimeoutController } from '../helpers/createTimeoutController'
import { TimeoutError } from '../helpers/error'
const DEFAULT_INTERVAL = 50
const DEFAULT_TIMEOUT = 1000
// This is so the stack trace the developer sees is one that's
// closer to their code (because async stack traces are hard to follow).
function copyStackTrace(target: Error, source: Error) {
target.stack = source.stack?.replace(source.message, target.message)
}
function jestFakeTimersAreEnabled() {
/* istanbul ignore else */
if (typeof jest !== 'undefined' && jest !== null) {
return (
// legacy timers
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
(setTimeout as any)._isMockFunction === true ||
// modern timers
Object.prototype.hasOwnProperty.call(setTimeout, 'clock')
)
}
// istanbul ignore next
return false
}
function asyncUtils(act: Act, addResolver: (callback: () => void) => void): AsyncUtils {
const wait = async (callback: () => boolean | void, { interval, timeout }: WaitOptions) => {
const checkResult = () => {
const callbackResult = callback()
return callbackResult ?? callbackResult === undefined
}
const timeoutSignal = createTimeoutController(timeout)
const waitForResult = async () => {
while (true) {
const intervalSignal = createTimeoutController(interval)
timeoutSignal.onTimeout(() => intervalSignal.cancel())
await intervalSignal.wrap(new Promise<void>(addResolver))
if (checkResult() || timeoutSignal.timedOut) {
return
}
}
}
if (!checkResult()) {
await act(() => timeoutSignal.wrap(waitForResult()))
}
return !timeoutSignal.timedOut
}
/**
* `waitFor` implementation from `@testing-library/dom` for Jest fake timers
* @param callback
* @param param1
* @returns
*/
const waitForInJestFakeTimers = (
callback: () => boolean | void,
{
interval,
stackTraceError,
timeout
}: { interval: number; timeout: number; stackTraceError: Error }
) => {
return new Promise(async (resolve, reject) => {
let lastError: unknown
let finished = false
const overallTimeoutTimer = setTimeout(handleTimeout, timeout)
checkCallback()
// this is a dangerous rule to disable because it could lead to an
// infinite loop. However, eslint isn't smart enough to know that we're
// setting finished inside `onDone` which will be called when we're done
// waiting or when we've timed out.
// eslint-disable-next-line no-unmodified-loop-condition
while (!finished) {
if (!jestFakeTimersAreEnabled()) {
const error = new Error(
`Changed from using fake timers to real timers while using waitFor. This is not allowed and will result in very strange behavior. Please ensure you're awaiting all async things your test is doing before changing to real timers. For more info, please go to https://github.com/testing-library/dom-testing-library/issues/830`
)
copyStackTrace(error, stackTraceError)
reject(error)
return
}
// we *could* (maybe should?) use `advanceTimersToNextTimer` but it's
// possible that could make this loop go on forever if someone is using
// third party code that's setting up recursive timers so rapidly that
// the user's timer's don't get a chance to resolve. So we'll advance
// by an interval instead. (We have a test for this case).
jest.advanceTimersByTime(interval)
// It's really important that checkCallback is run *before* we flush
// in-flight promises. To be honest, I'm not sure why, and I can't quite
// think of a way to reproduce the problem in a test, but I spent
// an entire day banging my head against a wall on this.
checkCallback()
if (finished) {
break
}
// In this rare case, we *need* to wait for in-flight promises
// to resolve before continuing. We don't need to take advantage
// of parallelization so we're fine.
// https://stackoverflow.com/a/59243586/971592
await act(async () => {
await new Promise((r) => {
setTimeout(r, 0)
jest.advanceTimersByTime(0)
})
})
}
function onDone(error: unknown, result: boolean | void | null) {
finished = true
clearTimeout(overallTimeoutTimer)
if (error) {
reject(error)
} else {
resolve(result)
}
}
function checkCallback() {
try {
const result = callback()
onDone(null, result)
// If `callback` throws, wait for the next mutation, interval, or timeout.
} catch (error: unknown) {
// Save the most recent callback error to reject the promise with it in the event of a timeout
lastError = error
}
}
function handleTimeout() {
let error
if (lastError) {
error = lastError
} else {
error = new Error('Timed out in waitFor.')
copyStackTrace(error, stackTraceError)
}
onDone(error, null)
}
})
}
const waitFor = async (
callback: () => boolean | void,
{ interval = DEFAULT_INTERVAL, timeout = DEFAULT_TIMEOUT }: WaitForOptions = {}
) => {
const safeCallback = () => {
try {
return callback()
} catch (error: unknown) {
return false
}
}
if (typeof interval === 'number' && typeof timeout === 'number' && jestFakeTimersAreEnabled()) {
// create the error here so its stack trace is as close to the
// calling code as possible
const stackTraceError = new Error('STACK_TRACE_MESSAGE')
return act(async () => {
await waitForInJestFakeTimers(callback, { interval, stackTraceError, timeout })
})
}
const result = await wait(safeCallback, { interval, timeout })
if (!result && timeout) {
throw new TimeoutError(waitFor, timeout)
}
}
const waitForValueToChange = async (
selector: () => unknown,
{ interval = DEFAULT_INTERVAL, timeout = DEFAULT_TIMEOUT }: WaitForValueToChangeOptions = {}
) => {
const initialValue = selector()
const result = await wait(() => selector() !== initialValue, { interval, timeout })
if (!result && timeout) {
throw new TimeoutError(waitForValueToChange, timeout)
}
}
const waitForNextUpdate = async ({
timeout = DEFAULT_TIMEOUT
}: WaitForNextUpdateOptions = {}) => {
let updated = false
addResolver(() => {
updated = true
})
const result = await wait(() => updated, { interval: false, timeout })
if (!result && timeout) {
throw new TimeoutError(waitForNextUpdate, timeout)
}
}
return {
waitFor,
waitForValueToChange,
waitForNextUpdate
}
}
export { asyncUtils }