Setting timeouts with Mocha allows you to control the maximum time a test case or suite can take to execute. However, incorrect usage or excessive
timeout values can lead to undesired consequences and impact the effectiveness of your test suite. For example, setting a timeout by calling
this.timeout()
with a value greater than the maximum delay (2,147,483,647 ms)
will cause the timeout to be disabled.
describe("testing this.timeout", function() {
it("unexpectedly disables the timeout", function(done) {
this.timeout(2147483648); // Noncompliant: the timeout is disabled
});
});
When using this.timeout()
, make sure to set a reasonable value that allows your tests to complete within a reasonable timeframe.
describe("testing this.timeout", function() {
it("sets the timeout to 1'000 milliseconds", function(done) {
this.timeout(1000);
});
});
If the goal is really to disable the timeout, set it to zero instead.
describe("testing this.timeout", function() {
it("disables the timeout as expected", function(done) {
this.timeout(0);
});
});