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

Providing an api for disableTimeouts() in tests. #1268

Closed
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
12 changes: 12 additions & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ Context.prototype.runnable = function(runnable){
return this;
};

/**
* Used to disableTimeouts for this suite.
*
* @param {Boolean} bool
* @api private
*/

Context.prototype.disableTimeouts = function(bool){
if (arguments.length === 0) bool = true;
this.runnable().disableTimeouts = !!bool;
};

/**
* Set test timeout `ms`.
*
Expand Down
33 changes: 26 additions & 7 deletions test/acceptance/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,45 @@ describe('timeouts', function(){
}, 300);
})

describe('when disabled', function(){
describe('when disabled from Mocha.Runnable.prototype.disableTimeouts property', 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', timeout)
it('should be ignored for sync suites', syncTimeout)
})

describe('when disabled from suite.disableTimeouts()', function(){
beforeEach(function(){
this.disableTimeouts(true);
})

it('should be ignored for async suites', function(done){
setTimeout(done, timeout + 10);
this.disableTimeouts();
timeout.call(this, done);
})

it('should be ignored for sync suites', function(){
var start = Date.now();
while(Date.now() - start < timeout)continue;
this.disableTimeouts();
syncTimeout.call(this);
})
});
})

function timeout(fn){
this.timeout(5);
setTimeout(fn, 15);
}

function syncTimeout(){
var start = Date.now();
this.timeout(5);
while(Date.now() - start < timeout) continue;
}
})