From 399f81323d68f35740858c96ccf7741e25b42c26 Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Mon, 11 Mar 2019 11:28:26 -0700 Subject: [PATCH 1/6] do not exit when only unref'd timer is present in test code; closes #3817 Signed-off-by: Christopher Hiller --- lib/runnable.js | 20 +++++++++---------- .../fixtures/options/timeout-unref.fixture.js | 3 +++ test/integration/options/timeout.spec.js | 19 ++++++++++++++++++ 3 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 test/integration/fixtures/options/timeout-unref.fixture.js create mode 100644 test/integration/options/timeout.spec.js diff --git a/lib/runnable.js b/lib/runnable.js index 026ab06b40..0eb1418ad5 100644 --- a/lib/runnable.js +++ b/lib/runnable.js @@ -16,6 +16,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; /** @@ -77,8 +79,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 @@ -253,18 +254,17 @@ Runnable.prototype.inspect = function() { */ Runnable.prototype.resetTimeout = function() { var self = this; - var ms = this.timeout() || 1e9; + // if timeout is 0, _enableTimeouts should also be false. + // we set this timeout to MAX_TIMEOUT so the event loop stays active. + var ms = this.timeout() || MAX_TIMEOUT; - if (!this._enableTimeouts) { - return; - } this.clearTimeout(); this.timer = setTimeout(function() { - if (!self._enableTimeouts) { - return; + // only throw error if timeouts are enabled. + if (self._enableTimeouts) { + self.callback(self._timeoutError(ms)); + self.timedOut = true; } - self.callback(self._timeoutError(ms)); - self.timedOut = true; }, ms); }; 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 new file mode 100644 index 0000000000..357e33509c --- /dev/null +++ b/test/integration/options/timeout.spec.js @@ -0,0 +1,19 @@ +'use strict'; + +var helpers = require('../helpers'); +var runMochaJSON = helpers.runMochaJSON; + +describe('--timeout', function() { + var args = []; + + it("should complete tests having unref'd async behavior", function(done) { + args = ['--timeout', '0']; + runMochaJSON('options/timeout-unref', args, function(err, res) { + if (err) { + return done(err); + } + expect(res, 'to have passed').and('to have passed test count', 1); + done(); + }); + }); +}); From 9f499c42b4ad4eb7b1daab3a0618c57d2b0a416b Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 28 Feb 2024 11:49:28 -0500 Subject: [PATCH 2/6] git checkout master -- docs/example/tests.html --- docs/example/tests.html | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/docs/example/tests.html b/docs/example/tests.html index a2b2fdc7ab..77e99ddf2a 100644 --- a/docs/example/tests.html +++ b/docs/example/tests.html @@ -1,21 +1,17 @@ - + Mocha - - + +
- - + +