Skip to content

Commit 5734bbe

Browse files
hainenbermark-wiemerJoshuaKGoldberg
authored
fix: allow chain call timeout to override nested items timeout (mochajs#5612)
Co-authored-by: Mark Wiemer <[email protected]> Co-authored-by: Josh Goldberg ✨ <[email protected]>
1 parent 1d2850d commit 5734bbe

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

lib/suite.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ Suite.prototype.timeout = function (ms) {
159159

160160
debug("timeout %d", ms);
161161
this._timeout = parseInt(ms, 10);
162+
163+
// Allow overriding inner/nested suites
164+
// See and test-cases with chain-called timeout argument.
165+
// https://github.com/mochajs/mocha/issues/5422
166+
for (const t of this.tests) {
167+
t.timeout(this._timeout);
168+
}
169+
for (const s of this.suites) {
170+
s.timeout(this._timeout);
171+
}
162172
return this;
163173
};
164174

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
describe("should fail due to suite-level timeout lower than elapsed time of inner test", function() {
4+
it("inner test", async function () {
5+
await new Promise((resolve) => {
6+
setTimeout(resolve, 100);
7+
});
8+
}).timeout(110);
9+
}).timeout(50);

test/integration/timeout.spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,22 @@ describe("this.timeout()", function () {
2525
done(err);
2626
return;
2727
}
28-
expect(res.stats.failures, "to be", 1);
28+
assert.strictEqual(res.stats.failures, 1);
29+
done();
30+
});
31+
});
32+
});
33+
34+
describe("describe.timeout()", function () {
35+
it("should fail due to suite-level timeout lower than elapsed time of inner test", function (done) {
36+
run("timeout-chained-call.fixture.js", args, function (err, res) {
37+
if (err) {
38+
done(err);
39+
return;
40+
}
41+
42+
assert.ok(res.failures[0].err.message.match(/Timeout of 50ms exceeded/));
43+
assert.strictEqual(res.code, 1);
2944
done();
3045
});
3146
});

test/unit/timeout.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ describe("timeouts", function () {
2020
}, 50);
2121
});
2222

23+
describe("chaining calls", function () {
24+
describe("suite-level", function () {
25+
describe("should override for inner test cases and deeply nested suites", function () {
26+
it("inner test", async function () {
27+
await new Promise((resolve) => {
28+
setTimeout(resolve, 1000);
29+
});
30+
});
31+
describe("nested suite", function () {
32+
it("nested test", async function () {
33+
await new Promise((resolve) => {
34+
setTimeout(resolve, 2200); // This waiting time is higher than the default timeout value, 2 seconds.
35+
});
36+
});
37+
}).timeout(70);
38+
}).timeout(2500); // This chained `timeout` config will override `timeout` for nested suites and cases
39+
});
40+
});
41+
2342
describe("disabling", function () {
2443
it("should work with timeout(0)", function (done) {
2544
this.timeout(0);

0 commit comments

Comments
 (0)