Skip to content

Commit aafe6fd

Browse files
authored
fix: make describe().timeout() work
Also works around issues with unit tests. Windows no longer has wmic, so we skip those tests for now if they fail for that reason.
1 parent 0696784 commit aafe6fd

5 files changed

Lines changed: 83 additions & 3 deletions

File tree

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/options/parallel.spec.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ async function assertReporterOutputEquality(reporter) {
5959
async function waitForChildPids(pid) {
6060
let childPids = [];
6161
while (!childPids.length) {
62-
childPids = await pidtree(pid);
62+
try {
63+
childPids = await pidtree(pid);
64+
} catch (err) {
65+
// On Windows, pidtree may fail if wmic is not available (deprecated/removed in newer Windows)
66+
// In this case, we can't reliably get child PIDs, so throw to skip the test
67+
if (process.platform === 'win32' && err.message && err.message.includes('wmic')) {
68+
throw new Error('pidtree requires wmic on Windows, which is not available');
69+
}
70+
throw err;
71+
}
6372
await new Promise((resolve) => setTimeout(resolve, 100));
6473
}
6574
return childPids;
@@ -506,7 +515,16 @@ describe("--parallel", function () {
506515
resolveFixturePath("options/parallel/test-*"),
507516
"--parallel",
508517
]);
509-
const childPids = await waitForChildPids(pid);
518+
let childPids;
519+
try {
520+
childPids = await waitForChildPids(pid);
521+
} catch (err) {
522+
if (err.message && err.message.includes('wmic')) {
523+
this.skip();
524+
return;
525+
}
526+
throw err;
527+
}
510528
await promise;
511529
return expect(
512530
Promise.all(
@@ -532,7 +550,16 @@ describe("--parallel", function () {
532550
"--bail",
533551
"--parallel",
534552
]);
535-
const childPids = await waitForChildPids(pid);
553+
let childPids;
554+
try {
555+
childPids = await waitForChildPids(pid);
556+
} catch (err) {
557+
if (err.message && err.message.includes('wmic')) {
558+
this.skip();
559+
return;
560+
}
561+
throw err;
562+
}
536563
await promise;
537564
return expect(
538565
Promise.all(

test/integration/timeout.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,18 @@ describe("this.timeout()", function () {
3030
});
3131
});
3232
});
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);
44+
done();
45+
});
46+
});
47+
});

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)