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

guard against undefined timeout option in constructor; closes #3813 #3816

Merged
merged 1 commit into from Mar 7, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/mocha.js
Expand Up @@ -120,12 +120,15 @@ function Mocha(options) {
utils.deprecate(
'enableTimeouts is DEPRECATED and will be removed from a future version of Mocha. Instead, use "timeout: false" to disable timeouts.'
);
if (options.enableTimeouts === false) {
this.timeout(0);
}
}

// this guard exists because Suite#timeout does not consider `undefined` to be valid input
if (typeof options.timeout !== 'undefined') {
this.timeout(options.timeout === false ? 0 : options.timeout);
}
this.timeout(
options.enableTimeouts === false || options.timeout === false
? 0
: options.timeout
);

if ('retries' in options) {
this.retries(options.retries);
Expand Down
1 change: 1 addition & 0 deletions lib/suite.js
Expand Up @@ -114,6 +114,7 @@ Suite.prototype.clone = function() {
* Set or get timeout `ms` or short-hand such as "2s".
*
* @private
* @todo Do not attempt to set value if `ms` is undefined
* @param {number|string} ms
* @return {Suite|number} for chaining
*/
Expand Down
49 changes: 37 additions & 12 deletions test/unit/mocha.spec.js
Expand Up @@ -20,24 +20,49 @@ describe('Mocha', function() {
beforeEach(function() {
sandbox.stub(Mocha.prototype, 'useColors').returnsThis();
sandbox.stub(utils, 'deprecate');
sandbox.stub(Mocha.prototype, 'timeout').returnsThis();
});

it('should prefer "color" over "useColors"', function() {
// eslint-disable-next-line no-new
new Mocha({useColors: true, color: false});
expect(Mocha.prototype.useColors, 'to have a call satisfying', [false]);
describe('when "useColors" option is defined', function() {
it('should prefer "color" over "useColors"', function() {
// eslint-disable-next-line no-new
new Mocha({useColors: true, color: false});
expect(Mocha.prototype.useColors, 'to have a call satisfying', [
false
]).and('was called once');
});

it('should assign "useColors" to "color"', function() {
// eslint-disable-next-line no-new
new Mocha({useColors: true});
expect(Mocha.prototype.useColors, 'to have a call satisfying', [
true
]).and('was called once');
});

it('should call utils.deprecate()', function() {
// eslint-disable-next-line no-new
new Mocha({useColors: true});
expect(utils.deprecate, 'was called once');
});
});

it('should assign "useColors" to "color"', function() {
// eslint-disable-next-line no-new
new Mocha({useColors: true});
expect(Mocha.prototype.useColors, 'to have a call satisfying', [true]);
describe('when "timeout" option is `undefined`', function() {
it('should not attempt to set timeout', function() {
// eslint-disable-next-line no-new
new Mocha({timeout: undefined});
expect(Mocha.prototype.timeout, 'was not called');
});
});

it('should call utils.deprecate()', function() {
// eslint-disable-next-line no-new
new Mocha({useColors: true});
expect(utils.deprecate, 'was called');
describe('when "timeout" option is `false`', function() {
it('should set a timeout of 0', function() {
// eslint-disable-next-line no-new
new Mocha({timeout: false});
expect(Mocha.prototype.timeout, 'to have a call satisfying', [0]).and(
'was called once'
);
});
});
});

Expand Down