Skip to content

Commit

Permalink
fix: support test timeouts if the test takes long or never completes
Browse files Browse the repository at this point in the history
node exits process if unresolved promises are present, for example, listening for a never fired DOM event in jsdom will terminate the process with no errors given, also see nodejs/promises-debugging#16
  • Loading branch information
asurkov committed Dec 22, 2021
1 parent 6a633e9 commit d28d042
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .watestrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const cfg = {
*/
log_dir: process.env.WATEST_LOG_DIR,

/**
* If a test takes longer than the given number, the test will fail.
*/
timeout: process.env.WATEST_TIMEOUT,

/**
* Temporary storage dir. Recreated each test run. Shall be used to store
* files generated by tests if any.
Expand Down
18 changes: 17 additions & 1 deletion core/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,23 @@ class Series {
let start_time = new Date();
try {
this.core.setExpectedFailures(failures_info);
await func(); // execute the test

// If timeout is given then race it against the test.
if (settings.timeout) {
let kungFuDeathGripTimer = 0;
let kungFuDeathGrip = new Promise(r => {
kungFuDeathGripTimer = setTimeout(r, settings.timeout);
}).then(() =>
fail(
`Test ${name} takes longer than ${settings.timeout}ms. It's either slow or never ends.`
)
);

await Promise.race([func(), kungFuDeathGrip]);
clearTimeout(kungFuDeathGripTimer);
} else {
await func(); // execute the test
}
} catch (e) {
let failmsg = e;
if (e instanceof Error) {
Expand Down
4 changes: 4 additions & 0 deletions core/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class Settings {
return parseInt(rc.debunk_limit) || 5;
}

get timeout() {
return parseInt(rc.timeout) || 0;
}

setupTmpStorageDir() {
if (!rc.tmp_dir) {
console.log(`Settings: no temporary storage dir`);
Expand Down

0 comments on commit d28d042

Please sign in to comment.