@@ -131,13 +131,25 @@ test('does not work after it resolves', async () => {
131
131
// @testing -library/react usage to ensure `IS_REACT_ACT_ENVIRONMENT` is set when acting.
132
132
advanceTimersByTime : async timeoutMS => {
133
133
const originalContext = context
134
- context = 'no- act'
134
+ context = 'act'
135
135
try {
136
136
jest . advanceTimersByTime ( timeoutMS )
137
137
} finally {
138
138
context = originalContext
139
139
}
140
140
} ,
141
+ flushPromises : async ( ) => {
142
+ const originalContext = context
143
+ context = 'no-act'
144
+ try {
145
+ await await new Promise ( r => {
146
+ setTimeout ( r , 0 )
147
+ jest . advanceTimersByTime ( 0 )
148
+ } )
149
+ } finally {
150
+ context = originalContext
151
+ }
152
+ } ,
141
153
}
142
154
143
155
let data = null
@@ -161,3 +173,75 @@ test('does not work after it resolves', async () => {
161
173
162
174
expect ( context ) . toEqual ( 'initial' )
163
175
} )
176
+
177
+ /** @type {import('../').FakeClock } */
178
+ const jestFakeClock = {
179
+ advanceTimersByTime : timeoutMS => {
180
+ jest . advanceTimersByTime ( timeoutMS )
181
+ } ,
182
+ flushPromises : ( ) => {
183
+ return new Promise ( r => {
184
+ setTimeout ( r , 0 )
185
+ jest . advanceTimersByTime ( 0 )
186
+ } )
187
+ } ,
188
+ }
189
+ describe . each ( [
190
+ [ 'real timers' , { useTimers : ( ) => jest . useRealTimers ( ) , clock : undefined } ] ,
191
+ [
192
+ 'fake legacy timers' ,
193
+ { useTimers : ( ) => jest . useFakeTimers ( 'legacy' ) , clock : jestFakeClock } ,
194
+ ] ,
195
+ [
196
+ 'fake modern timers' ,
197
+ { useTimers : ( ) => jest . useFakeTimers ( 'modern' ) , clock : jestFakeClock } ,
198
+ ] ,
199
+ ] ) (
200
+ 'waitFor DOM reference implementation using %s' ,
201
+ ( label , { useTimers, clock} ) => {
202
+ beforeEach ( ( ) => {
203
+ useTimers ( )
204
+ } )
205
+
206
+ afterEach ( ( ) => {
207
+ jest . useRealTimers ( )
208
+ } )
209
+
210
+ test ( 'void callback' , async ( ) => {
211
+ await expect ( waitFor ( ( ) => { } , { clock} ) ) . resolves . toBeUndefined ( )
212
+ } )
213
+
214
+ test ( 'callback passes after timeout' , async ( ) => {
215
+ let state = 'pending'
216
+ setTimeout ( ( ) => {
217
+ state = 'done'
218
+ } , 10 )
219
+
220
+ await expect (
221
+ waitFor (
222
+ ( ) => {
223
+ if ( state !== 'done' ) {
224
+ throw new Error ( 'Not done' )
225
+ }
226
+ } ,
227
+ { clock, interval : 5 } ,
228
+ ) ,
229
+ ) . resolves . toBeUndefined ( )
230
+ } )
231
+
232
+ test ( 'timeout' , async ( ) => {
233
+ const state = 'pending'
234
+
235
+ await expect (
236
+ waitFor (
237
+ ( ) => {
238
+ if ( state !== 'done' ) {
239
+ throw new Error ( 'Not done' )
240
+ }
241
+ } ,
242
+ { clock, timeout : 10 } ,
243
+ ) ,
244
+ ) . rejects . toThrowErrorMatchingSnapshot ( )
245
+ } )
246
+ } ,
247
+ )
0 commit comments