Skip to content

Commit

Permalink
closes mochajs#865: Disable timeouts when running under the debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdevel committed Jul 8, 2014
1 parent 7ac4f01 commit fa3fa4d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions bin/_mocha
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions bin/mocha
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
9 changes: 8 additions & 1 deletion lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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);
}

Expand Down
23 changes: 23 additions & 0 deletions test/acceptance/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
});
})

0 comments on commit fa3fa4d

Please sign in to comment.