File tree Expand file tree Collapse file tree 3 files changed +18
-19
lines changed Expand file tree Collapse file tree 3 files changed +18
-19
lines changed Original file line number Diff line number Diff line change @@ -17,3 +17,14 @@ test('wait defaults to a noop callback', async () => {
17
17
await wait ( )
18
18
expect ( handler ) . toHaveBeenCalledTimes ( 1 )
19
19
} )
20
+
21
+ test ( 'can timeout after the given timeout time' , async ( ) => {
22
+ const error = new Error ( 'throws every time' )
23
+ const result = await wait (
24
+ ( ) => {
25
+ throw error
26
+ } ,
27
+ { timeout : 8 , interval : 5 } ,
28
+ ) . catch ( e => e )
29
+ expect ( result ) . toBe ( error )
30
+ } )
Original file line number Diff line number Diff line change 1
1
import { wait } from './wait'
2
2
3
+ // deprecated... TODO: remove this method. People should use a find* query or
4
+ // wait instead the reasoning is that this doesn't really do anything useful
5
+ // that you can't get from using find* or wait.
3
6
async function waitForElement ( callback , options ) {
4
7
if ( ! callback ) {
5
8
throw new Error ( 'waitForElement requires a callback as the first parameter' )
Original file line number Diff line number Diff line change @@ -23,20 +23,20 @@ function wait(
23
23
} = { } ,
24
24
) {
25
25
if ( interval < 1 ) interval = 1
26
- const maxTries = Math . ceil ( timeout / interval )
27
- let tries = 0
28
26
return new Promise ( ( resolve , reject ) => {
29
- let lastError , lastTimer
27
+ let lastError
30
28
const overallTimeoutTimer = setTimeout ( onTimeout , timeout )
29
+ const intervalId = setInterval ( checkCallback , interval )
31
30
32
31
const observer = newMutationObserver ( checkCallback )
33
32
runWithRealTimers ( ( ) =>
34
33
observer . observe ( container , mutationObserverOptions ) ,
35
34
)
35
+ checkCallback ( )
36
36
37
37
function onDone ( error , result ) {
38
38
clearTimeout ( overallTimeoutTimer )
39
- clearTimeout ( lastTimer )
39
+ clearInterval ( intervalId )
40
40
setImmediate ( ( ) => observer . disconnect ( ) )
41
41
if ( error ) {
42
42
reject ( error )
@@ -58,21 +58,6 @@ function wait(
58
58
function onTimeout ( ) {
59
59
onDone ( lastError || new Error ( 'Timed out in wait.' ) , null )
60
60
}
61
-
62
- function startTimer ( ) {
63
- lastTimer = setTimeout ( ( ) => {
64
- tries ++
65
- checkCallback ( )
66
- if ( tries > maxTries ) {
67
- onTimeout ( )
68
- return
69
- }
70
- startTimer ( )
71
- } , interval )
72
- }
73
-
74
- checkCallback ( )
75
- startTimer ( )
76
61
} )
77
62
}
78
63
You can’t perform that action at this time.
0 commit comments