Skip to content

Commit

Permalink
Merge pull request #2 from tomtomau/wait-for-error
Browse files Browse the repository at this point in the history
rfc(error): improve timeout error stack trace for waitFor timeouts
  • Loading branch information
tomtomau authored Aug 26, 2020
2 parents 113c329 + ff2fb3d commit ecc6d62
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 7 deletions.
4 changes: 3 additions & 1 deletion dist/amd/wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ define(["require", "exports"], function (require, exports) {
}
return new Promise(function (rs) { return 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
var timeoutError = new Error(options.present ? 'Element not found' : 'Element not removed');
return Promise.race([
new Promise(function (_, rj) { return setTimeout(function () {
timedOut = true;
rj(new Error(options.present ? 'Element not found' : 'Element not removed'));
rj(timeoutError);
}, options.timeout); }),
wait()
]);
Expand Down
4 changes: 3 additions & 1 deletion dist/commonjs/wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ function waitFor(getter, options) {
}
return new Promise(function (rs) { return 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
var timeoutError = new Error(options.present ? 'Element not found' : 'Element not removed');
return Promise.race([
new Promise(function (_, rj) { return setTimeout(function () {
timedOut = true;
rj(new Error(options.present ? 'Element not found' : 'Element not removed'));
rj(timeoutError);
}, options.timeout); }),
wait()
]);
Expand Down
4 changes: 3 additions & 1 deletion dist/es2015/wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ export function waitFor(getter, options = { present: true, interval: 50, timeout
}
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()
]);
Expand Down
4 changes: 3 additions & 1 deletion dist/es2017/wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ export function waitFor(getter, options = { present: true, interval: 50, timeout
}
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()
]);
Expand Down
4 changes: 3 additions & 1 deletion dist/native-modules/wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ export function waitFor(getter, options) {
}
return new Promise(function (rs) { return 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
var timeoutError = new Error(options.present ? 'Element not found' : 'Element not removed');
return Promise.race([
new Promise(function (_, rj) { return setTimeout(function () {
timedOut = true;
rj(new Error(options.present ? 'Element not found' : 'Element not removed'));
rj(timeoutError);
}, options.timeout); }),
wait()
]);
Expand Down
4 changes: 3 additions & 1 deletion dist/system/wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ System.register([], function (exports_1, context_1) {
}
return new Promise(function (rs) { return 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
var timeoutError = new Error(options.present ? 'Element not found' : 'Element not removed');
return Promise.race([
new Promise(function (_, rj) { return setTimeout(function () {
timedOut = true;
rj(new Error(options.present ? 'Element not found' : 'Element not removed'));
rj(timeoutError);
}, options.timeout); }),
wait()
]);
Expand Down
5 changes: 4 additions & 1 deletion src/wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ export function waitFor<T>(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()
Expand Down

0 comments on commit ecc6d62

Please sign in to comment.