File tree Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -115,6 +115,32 @@ test.each([TimerMode.Legacy, TimerMode.Modern])(
115
115
}
116
116
) ;
117
117
118
+ test . each ( [ TimerMode . Legacy , TimerMode . Modern ] ) (
119
+ 'waits for assertion until timeout is met with %s fake timers' ,
120
+ async ( fakeTimerType ) => {
121
+ jest . useFakeTimers ( fakeTimerType ) ;
122
+
123
+ const mockErrorFn = jest . fn ( ( ) => {
124
+ throw Error ( 'test' ) ;
125
+ } ) ;
126
+
127
+ const mockHandleFn = jest . fn ( ( e ) => e ) ;
128
+
129
+ try {
130
+ await waitFor ( ( ) => mockErrorFn ( ) , {
131
+ timeout : 400 ,
132
+ interval : 200 ,
133
+ onTimeout : mockHandleFn ,
134
+ } ) ;
135
+ } catch ( error ) {
136
+ // suppress
137
+ }
138
+
139
+ expect ( mockErrorFn ) . toHaveBeenCalledTimes ( 3 ) ;
140
+ expect ( mockHandleFn ) . toHaveBeenCalledTimes ( 1 ) ;
141
+ }
142
+ ) ;
143
+
118
144
test . each ( [ TimerMode . Legacy , TimerMode . Legacy ] ) (
119
145
'awaiting something that succeeds before timeout works with %s fake timers' ,
120
146
async ( fakeTimerType ) => {
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ export type WaitForOptions = {
29
29
timeout ?: number ,
30
30
interval ?: number ,
31
31
stackTraceError ?: ErrorWithStack ,
32
+ onTimeout ?: ( error : Error ) => Error ,
32
33
} ;
33
34
34
35
function waitForInternal < T > (
@@ -37,6 +38,7 @@ function waitForInternal<T>(
37
38
timeout = DEFAULT_TIMEOUT ,
38
39
interval = DEFAULT_INTERVAL ,
39
40
stackTraceError,
41
+ onTimeout,
40
42
} : WaitForOptions
41
43
) : Promise < T > {
42
44
if ( typeof expectation !== 'function' ) {
@@ -179,6 +181,9 @@ function waitForInternal<T>(
179
181
copyStackTrace ( error , stackTraceError ) ;
180
182
}
181
183
}
184
+ if ( typeof onTimeout === 'function' ) {
185
+ onTimeout ( error ) ;
186
+ }
182
187
onDone ( error , null ) ;
183
188
}
184
189
} ) ;
Original file line number Diff line number Diff line change @@ -388,6 +388,7 @@ type TextMatchOptions = {
388
388
type WaitForOptions = {
389
389
timeout ?: number ;
390
390
interval ?: number ;
391
+ onTimeout ?: ( error : Error ) => Error ;
391
392
} ;
392
393
393
394
export type WaitForFunction = < T = any > (
You can’t perform that action at this time.
0 commit comments