Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ Suite.prototype.timeout = function (ms) {

debug("timeout %d", ms);
this._timeout = parseInt(ms, 10);

// Allow overriding inner/nested suites
// See and test-cases with chain-called timeout argument.
// https://github.com/mochajs/mocha/issues/5422
for (const t of this.tests) {
t.timeout(this._timeout);
}
for (const s of this.suites) {
s.timeout(this._timeout);
}
return this;
};

Expand Down
9 changes: 9 additions & 0 deletions test/integration/fixtures/timeout-chained-call.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

describe("should fail due to suite-level timeout is lower than elapsed time of inner test", function() {
Comment thread
hainenber marked this conversation as resolved.
Resolved
it("inner test", async function () {
await new Promise((resolve) => {
setTimeout(resolve, 100);
});
}).timeout(110);
}).timeout(50);
17 changes: 16 additions & 1 deletion test/integration/timeout.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,22 @@ describe("this.timeout()", function () {
done(err);
return;
}
expect(res.stats.failures, "to be", 1);
assert.strictEqual(res.stats.failures, 1);
done();
});
});
});

describe("describe.timeout()", function () {
it("should fail due to suite-level timeout is lower than elapsed time of inner test", function (done) {
Comment thread
hainenber marked this conversation as resolved.
Resolved
run("timeout-chained-call.fixture.js", args, function (err, res) {
if (err) {
done(err);
return;
}

assert.ok(res.failures[0].err.message.match(/Timeout of 50ms exceeded/));
assert.strictEqual(res.code, 1);
done();
});
});
Expand Down
19 changes: 19 additions & 0 deletions test/unit/timeout.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ describe("timeouts", function () {
}, 50);
});

describe("chaining calls", function () {
describe("suite-level", function () {
describe("should override for inner test cases and deeply nested suites", function () {
it("inner test", async function () {
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
});
describe("nested suite", function () {
it("nested test", async function () {
await new Promise((resolve) => {
setTimeout(resolve, 2200); // This waiting time is higher than the default timeout value, 2 seconds.
});
});
}).timeout(70);
Comment thread
JoshuaKGoldberg marked this conversation as resolved.
Resolved
}).timeout(2500); // This chained `timeout` config will override `timeout` for nested suites and cases
});
});

Comment thread
mark-wiemer marked this conversation as resolved.
Resolved
describe("disabling", function () {
it("should work with timeout(0)", function (done) {
this.timeout(0);
Expand Down