diff --git a/src/wait.ts b/src/wait.ts index b2ba116..233ab3c 100644 --- a/src/wait.ts +++ b/src/wait.ts @@ -34,11 +34,14 @@ export function waitFor(getter: () => T, options: { return new Promise(rs => setTimeout(rs, options.interval)).then(wait); } + // We generate the error here to capture the stack trace, but we will only throw it if it times out + const timeoutError = new Error(options.present ? 'Element not found' : 'Element not removed'); + return Promise.race([ new Promise( (_, rj) => setTimeout(() => { timedOut = true; - rj(new Error(options.present ? 'Element not found' : 'Element not removed')); + rj(timeoutError); }, options.timeout) ), wait()