Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

closes #865: Disable timeouts when running under the debug

jsdevelfa3fa4d023e4
diff --git a/bin/_mocha b/bin/_mocha
index 86c67e29..5fcd4d9d 100755
--- a/bin/_mocha
+++ b/bin/_mocha
@@ -81,6 +81,7 @@ 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')
@@ -229,6 +230,10 @@ if (program.inlineDiffs) mocha.useInlineDiffs(true);
if (program.slow) mocha.suite.slow(program.slow);
+// --disable-timeouts
+
+if (program.disableTimeouts) Mocha.Runnable.prototype.disableTimeouts = true;
+
// --timeout
if (program.timeout) mocha.suite.timeout(program.timeout);
diff --git a/bin/mocha b/bin/mocha
index effa25d2..535de42f 100755
--- a/bin/mocha
+++ b/bin/mocha
@@ -14,11 +14,13 @@ process.argv.slice(2).forEach(function(arg){
switch (flag) {
case '-d':
args.unshift('--debug');
+ args.push('--disable-timeouts');
break;
case 'debug':
case '--debug':
case '--debug-brk':
args.unshift(arg);
+ args.push('--disable-timeouts');
break;
case '-gc':
case '--expose-gc':
diff --git a/lib/runnable.js b/lib/runnable.js
index c27eeed9..9a988869 100644
--- a/lib/runnable.js
+++ b/lib/runnable.js
@@ -53,6 +53,12 @@ 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`.
*
@@ -134,6 +140,7 @@ Runnable.prototype.resetTimeout = function(){
var self = this;
var ms = this.timeout() || 1e9;
+ if (this.disableTimeouts) return;
this.clearTimeout();
this.timer = setTimeout(function(){
self.callback(new Error('timeout of ' + ms + 'ms exceeded'));
@@ -182,7 +189,7 @@ Runnable.prototype.run = function(fn){
self.clearTimeout();
self.duration = new Date - start;
finished = true;
- if (!err && self.duration > ms) err = new Error('timeout of ' + ms + 'ms exceeded');
+ if (!err && self.duration > ms && !self.disableTimeouts) err = new Error('timeout of ' + ms + 'ms exceeded');
fn(err);
}
diff --git a/test/acceptance/timeout.js b/test/acceptance/timeout.js
index 109808bf..6aff333b 100644
--- a/test/acceptance/timeout.js
+++ b/test/acceptance/timeout.js
@@ -18,4 +18,27 @@ describe('timeouts', function(){
done();
}, 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;
+ })
+ });
})