Skip to content

Commit d28d042

Browse files
committed
fix: support test timeouts if the test takes long or never completes
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
1 parent 6a633e9 commit d28d042

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

.watestrc.js

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const cfg = {
1818
*/
1919
log_dir: process.env.WATEST_LOG_DIR,
2020

21+
/**
22+
* If a test takes longer than the given number, the test will fail.
23+
*/
24+
timeout: process.env.WATEST_TIMEOUT,
25+
2126
/**
2227
* Temporary storage dir. Recreated each test run. Shall be used to store
2328
* files generated by tests if any.

core/series.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,23 @@ class Series {
637637
let start_time = new Date();
638638
try {
639639
this.core.setExpectedFailures(failures_info);
640-
await func(); // execute the test
640+
641+
// If timeout is given then race it against the test.
642+
if (settings.timeout) {
643+
let kungFuDeathGripTimer = 0;
644+
let kungFuDeathGrip = new Promise(r => {
645+
kungFuDeathGripTimer = setTimeout(r, settings.timeout);
646+
}).then(() =>
647+
fail(
648+
`Test ${name} takes longer than ${settings.timeout}ms. It's either slow or never ends.`
649+
)
650+
);
651+
652+
await Promise.race([func(), kungFuDeathGrip]);
653+
clearTimeout(kungFuDeathGripTimer);
654+
} else {
655+
await func(); // execute the test
656+
}
641657
} catch (e) {
642658
let failmsg = e;
643659
if (e instanceof Error) {

core/settings.js

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ class Settings {
5050
return parseInt(rc.debunk_limit) || 5;
5151
}
5252

53+
get timeout() {
54+
return parseInt(rc.timeout) || 0;
55+
}
56+
5357
setupTmpStorageDir() {
5458
if (!rc.tmp_dir) {
5559
console.log(`Settings: no temporary storage dir`);

0 commit comments

Comments
 (0)