Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

closes #865: Disable timeouts when running under the debug #1262

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
})
});
})