Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

fix: allow chain call timeout to override nested items timeout (#5612)

Đỗ Trọng Hải5525da60cf8d
diff --git a/lib/suite.js b/lib/suite.js
index 20e1a93e..a29d71e8 100644
--- a/lib/suite.js
+++ b/lib/suite.js
@@ -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;
};
diff --git a/test/integration/fixtures/timeout-chained-call.fixture.js b/test/integration/fixtures/timeout-chained-call.fixture.js
new file mode 100644
index 00000000..197e2708
--- /dev/null
+++ b/test/integration/fixtures/timeout-chained-call.fixture.js
@@ -0,0 +1,9 @@
+'use strict';
+
+describe("should fail due to suite-level timeout lower than elapsed time of inner test", function() {
+ it("inner test", async function () {
+ await new Promise((resolve) => {
+ setTimeout(resolve, 100);
+ });
+ }).timeout(110);
+}).timeout(50);
diff --git a/test/integration/timeout.spec.js b/test/integration/timeout.spec.js
index af39800e..c0389647 100644
--- a/test/integration/timeout.spec.js
+++ b/test/integration/timeout.spec.js
@@ -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 lower than elapsed time of inner test", function (done) {
+ 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();
});
});
diff --git a/test/unit/timeout.spec.js b/test/unit/timeout.spec.js
index dbf6a379..0a99babd 100644
--- a/test/unit/timeout.spec.js
+++ b/test/unit/timeout.spec.js
@@ -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);
+ }).timeout(2500); // This chained `timeout` config will override `timeout` for nested suites and cases
+ });
+ });
+
describe("disabling", function () {
it("should work with timeout(0)", function (done) {
this.timeout(0);