From 6d24689b2905dd4615f317d80e8388979263bab5 Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Fri, 19 Jul 2024 20:54:18 -0700 Subject: [PATCH] fix: do not exit when only unref'd timer is present in test code (#3825) * do not exit when only unref'd timer is present in test code; closes #3817 Signed-off-by: Christopher Hiller * git checkout master -- docs/example/tests.html * fix: still bail if self.timeout() === 0 * fix: unref timers implicitly scheduled for MAX_TIMEOUT * Revert "fix: unref timers implicitly scheduled for MAX_TIMEOUT" This reverts commit d4a65aabb0523bc0f4d87eaefb7db2fd4df1fa53. * test: call .run() to not leave timer pending --------- Signed-off-by: Christopher Hiller Co-authored-by: Josh Goldberg --- lib/runnable.js | 10 ++++------ .../fixtures/options/timeout-unref.fixture.js | 3 +++ test/integration/options/timeout.spec.js | 11 +++++++++++ test/unit/runnable.spec.js | 1 + 4 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 test/integration/fixtures/options/timeout-unref.fixture.js diff --git a/lib/runnable.js b/lib/runnable.js index 5e0970b159..98327bf5d9 100644 --- a/lib/runnable.js +++ b/lib/runnable.js @@ -20,6 +20,8 @@ var setTimeout = global.setTimeout; var clearTimeout = global.clearTimeout; var toString = Object.prototype.toString; +var MAX_TIMEOUT = Math.pow(2, 31) - 1; + module.exports = Runnable; /** @@ -95,8 +97,7 @@ Runnable.prototype.timeout = function (ms) { } // Clamp to range - var INT_MAX = Math.pow(2, 31) - 1; - var range = [0, INT_MAX]; + var range = [0, MAX_TIMEOUT]; ms = utils.clamp(ms, range); // see #1652 for reasoning @@ -233,11 +234,8 @@ Runnable.prototype.clearTimeout = function () { */ Runnable.prototype.resetTimeout = function () { var self = this; - var ms = this.timeout(); + var ms = this.timeout() || MAX_TIMEOUT; - if (ms === 0) { - return; - } this.clearTimeout(); this.timer = setTimeout(function () { if (self.timeout() === 0) { diff --git a/test/integration/fixtures/options/timeout-unref.fixture.js b/test/integration/fixtures/options/timeout-unref.fixture.js new file mode 100644 index 0000000000..8f59aea591 --- /dev/null +++ b/test/integration/fixtures/options/timeout-unref.fixture.js @@ -0,0 +1,3 @@ +it('unrefs a timeout', function(done) { + setTimeout(done, 10).unref(); +}); diff --git a/test/integration/options/timeout.spec.js b/test/integration/options/timeout.spec.js index 83267bd02f..e3ac696e93 100644 --- a/test/integration/options/timeout.spec.js +++ b/test/integration/options/timeout.spec.js @@ -83,4 +83,15 @@ describe('--timeout', function () { } ); }); + + it("should complete tests having unref'd async behavior", function(done) { + runMochaJSON('options/timeout-unref', ['--timeout', '0'], function(err, res) { + if (err) { + done(err); + return; + } + expect(res, 'to have passed').and('to have passed test count', 1); + done(); + }); + }); }); diff --git a/test/unit/runnable.spec.js b/test/unit/runnable.spec.js index b4ae296478..598f532f80 100644 --- a/test/unit/runnable.spec.js +++ b/test/unit/runnable.spec.js @@ -702,6 +702,7 @@ describe('Runnable(title, fn)', function () { runnable.timeout(10); runnable.resetTimeout(); runnable.timeout(0); + runnable.run(); setTimeout(function () { expect(runnable.timedOut, 'to be', false); done();