☕️ Classic, reliable, trusted test framework for Node.js and the browser
Add --no-timeouts options (fix #1262, fix #1268)
Travis Jeffery5dad9a34fe99
diff --git a/bin/_mocha b/bin/_mochaindex 5fcd4d9d..e08e1092 100755--- a/bin/_mocha+++ b/bin/_mocha@@ -81,7 +81,6 @@ program.option('--compilers <ext>:<module>,...', 'use the given module(s) to compile files', list, []).option('--debug-brk', "enable node's debugger breaking on the first line").option('--globals <names>', 'allow the given comma-delimited global [names]', list, [])- .option('--disable-timeouts', 'this is given implicitly when debugging').option('--harmony', 'enable all harmony features (except typeof)').option('--harmony-collections', 'enable harmony collections (sets, maps, and weak maps)').option('--harmony-generators', 'enable harmony generators')@@ -90,6 +89,7 @@ program.option('--interfaces', 'display available interfaces').option('--no-deprecation', 'silence deprecation warnings').option('--no-exit', 'require a clean shutdown of the event loop: mocha will not call process.exit')+ .option('--no-timeouts', 'disables timeouts, given implicitly with --debug').option('--prof', 'log statistical profiling information').option('--recursive', 'include sub directories').option('--reporters', 'display available reporters')@@ -230,9 +230,9 @@ if (program.inlineDiffs) mocha.useInlineDiffs(true);if (program.slow) mocha.suite.slow(program.slow);-// --disable-timeouts+// --no-timeouts-if (program.disableTimeouts) Mocha.Runnable.prototype.disableTimeouts = true;+if (!program.timeouts) mocha.enableTimeouts(false);// --timeoutdiff --git a/bin/mocha b/bin/mochaindex 535de42f..b5dbc72f 100755--- a/bin/mocha+++ b/bin/mocha@@ -14,13 +14,13 @@ process.argv.slice(2).forEach(function(arg){switch (flag) {case '-d':args.unshift('--debug');- args.push('--disable-timeouts');+ args.push('--no-timeouts');break;case 'debug':case '--debug':case '--debug-brk':args.unshift(arg);- args.push('--disable-timeouts');+ args.push('--no-timeouts');break;case '-gc':case '--expose-gc':diff --git a/lib/context.js b/lib/context.jsindex 6d6422a1..e7138ba9 100644--- a/lib/context.js+++ b/lib/context.js@@ -40,6 +40,20 @@ Context.prototype.timeout = function(ms){return this;};+/**+ * Set test timeout `enabled`.+ *+ * @param {Boolean} enabled+ * @return {Context} self+ * @api private+ */++Context.prototype.enableTimeouts = function (enabled) {+ this.runnable().enableTimeouts(enabled);+ return this;+};++/*** Set test slowness threshold `ms`.*diff --git a/lib/mocha.js b/lib/mocha.jsindex ee565279..7882fb64 100644--- a/lib/mocha.js+++ b/lib/mocha.js@@ -75,6 +75,7 @@ function Mocha(options) {this.reporter(options.reporter);if (null != options.timeout) this.timeout(options.timeout);this.useColors(options.useColors)+ if (options.enableTimeouts !== null) this.enableTimeouts(options.enableTimeouts);if (options.slow) this.slow(options.slow);this.suite.on('pre-require', function (context) {@@ -335,6 +336,21 @@ Mocha.prototype.slow = function(slow){return this;};+/**+ * Enable timeouts.+ *+ * @param {Boolean} enabled+ * @return {Mocha}+ * @api public+ */++Mocha.prototype.enableTimeouts = function(enabled) {+ this.suite.enableTimeouts(arguments.length && enabled !== undefined+ ? enabled+ : true);+ return this+};+/*** Makes all tests async (accepting a callback)*diff --git a/lib/runnable.js b/lib/runnable.jsindex 1733065f..db8d014c 100644--- a/lib/runnable.js+++ b/lib/runnable.js@@ -44,6 +44,7 @@ function Runnable(title, fn) {this.sync = ! this.async;this._timeout = 2000;this._slow = 75;+ this._enableTimeouts = true;this.timedOut = false;}@@ -53,12 +54,6 @@ function Runnable(title, fn) {Runnable.prototype.__proto__ = EventEmitter.prototype;-/**- * Set this value to true to disable timeouts.- */--Runnable.prototype.disableTimeouts = false;-/*** Set & get timeout `ms`.*@@ -92,6 +87,21 @@ Runnable.prototype.slow = function(ms){return this;};+/**+ * Set and & get timeout `enabled`.+ *+ * @param {Boolean} enabled+ * @return {Runnable|Boolean} enabled or self+ * @api private+ */++Runnable.prototype.enableTimeouts = function(enabled){+ if (arguments.length === 0) return this._enableTimeouts;+ debug('enableTimeouts %s', enabled);+ this._enableTimeouts = enabled;+ return this;+};+/*** Return the full title generated by recursively* concatenating the parent's full title.@@ -140,7 +150,7 @@ Runnable.prototype.resetTimeout = function(){var self = this;var ms = this.timeout() || 1e9;- if (this.disableTimeouts) return;+ if (!this._enableTimeouts) return;this.clearTimeout();this.timer = setTimeout(function(){self.callback(new Error('timeout of ' + ms + 'ms exceeded'));@@ -189,7 +199,7 @@ Runnable.prototype.run = function(fn){self.clearTimeout();self.duration = new Date - start;finished = true;- if (!err && self.duration > ms && !self.disableTimeouts) err = new Error('timeout of ' + ms + 'ms exceeded');+ if (!err && self.duration > ms && self._enableTimeouts) err = new Error('timeout of ' + ms + 'ms exceeded');fn(err);}diff --git a/lib/suite.js b/lib/suite.jsindex b06645b5..ee5983e5 100644--- a/lib/suite.js+++ b/lib/suite.js@@ -60,6 +60,7 @@ function Suite(title, parentContext) {this._afterAll = [];this.root = !title;this._timeout = 2000;+ this._enableTimeouts = true;this._slow = 75;this._bail = false;}@@ -82,6 +83,7 @@ Suite.prototype.clone = function(){debug('clone');suite.ctx = this.ctx;suite.timeout(this.timeout());+ suite.enableTimeouts(this.enableTimeouts());suite.slow(this.slow());suite.bail(this.bail());return suite;@@ -103,6 +105,21 @@ Suite.prototype.timeout = function(ms){return this;};+/**+ * Set timeout `enabled`.+ *+ * @param {Boolean} enabled+ * @return {Suite|Boolean} self or enabled+ * @api private+ */++Suite.prototype.enableTimeouts = function(enabled){+ if (arguments.length === 0) return this._enableTimeouts;+ debug('enableTimeouts %s', enabled);+ this._enableTimeouts = enabled;+ return this;+}+/*** Set slow `ms` or short-hand such as "2s".*@@ -153,6 +170,7 @@ Suite.prototype.beforeAll = function(title, fn){var hook = new Hook(title, fn);hook.parent = this;hook.timeout(this.timeout());+ hook.enableTimeouts(this.enableTimeouts());hook.slow(this.slow());hook.ctx = this.ctx;this._beforeAll.push(hook);@@ -179,6 +197,7 @@ Suite.prototype.afterAll = function(title, fn){var hook = new Hook(title, fn);hook.parent = this;hook.timeout(this.timeout());+ hook.enableTimeouts(this.enableTimeouts());hook.slow(this.slow());hook.ctx = this.ctx;this._afterAll.push(hook);@@ -205,6 +224,7 @@ Suite.prototype.beforeEach = function(title, fn){var hook = new Hook(title, fn);hook.parent = this;hook.timeout(this.timeout());+ hook.enableTimeouts(this.enableTimeouts());hook.slow(this.slow());hook.ctx = this.ctx;this._beforeEach.push(hook);@@ -231,6 +251,7 @@ Suite.prototype.afterEach = function(title, fn){var hook = new Hook(title, fn);hook.parent = this;hook.timeout(this.timeout());+ hook.enableTimeouts(this.enableTimeouts());hook.slow(this.slow());hook.ctx = this.ctx;this._afterEach.push(hook);@@ -249,6 +270,7 @@ Suite.prototype.afterEach = function(title, fn){Suite.prototype.addSuite = function(suite){suite.parent = this;suite.timeout(this.timeout());+ suite.enableTimeouts(this.enableTimeouts());suite.slow(this.slow());suite.bail(this.bail());this.suites.push(suite);@@ -267,6 +289,7 @@ Suite.prototype.addSuite = function(suite){Suite.prototype.addTest = function(test){test.parent = this;test.timeout(this.timeout());+ test.enableTimeouts(this.enableTimeouts());test.slow(this.slow());test.ctx = this.ctx;this.tests.push(test);diff --git a/test/acceptance/timeout.js b/test/acceptance/timeout.jsindex 6aff333b..0042f831 100644--- a/test/acceptance/timeout.js+++ b/test/acceptance/timeout.js@@ -19,26 +19,11 @@ describe('timeouts', function(){}, 300);})- describe('when disabled', function(){- var Mocha = require('../../lib/mocha');- var timeout;-- beforeEach(function(){- Mocha.Runnable.prototype.disableTimeouts = true;- timeout = this.timeout();- })-- afterEach(function(){- Mocha.Runnable.prototype.disableTimeouts = false;- })-- it('should be ignored for async suites', function(done){- setTimeout(done, timeout + 10);- })-- it('should be ignored for sync suites', function(){- var start = Date.now();- while(Date.now() - start < timeout)continue;- })+ describe('disabling', function(){+ it('should allow overriding per-test', function(done){+ this.enableTimeouts(false);+ this.timeout(1);+ setTimeout(done, 2);+ });});})diff --git a/test/runnable.js b/test/runnable.jsindex 3ea7dc6a..5fa9352f 100644--- a/test/runnable.js+++ b/test/runnable.js@@ -41,6 +41,14 @@ describe('Runnable(title, fn)', function(){})})+ describe('#enableTimeouts(enabled)', function(){+ it('should set enabled', function(){+ var run = new Runnable;+ run.enableTimeouts(false);+ run.enableTimeouts().should.equal(false);+ });+ });+describe('#slow(ms)', function(){it('should set the slow threshold', function(){var run = new Runnable;@@ -126,6 +134,17 @@ describe('Runnable(title, fn)', function(){})})+ describe('when timeouts are disabled', function() {+ it('should not error with timeout', function(done) {+ var test = new Runnable('foo', function(done){+ setTimeout(process.nextTick.bind(undefined, done), 2);+ });+ test.timeout(1);+ test.enableTimeouts(false);+ test.run(done);+ });+ });+describe('when async', function(){describe('without error', function(){it('should invoke the callback', function(done){