Skip to content

Commit

Permalink
Mocha constructor: some fixes and cleanup (#4004)
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed Sep 22, 2019
1 parent 6ec07d6 commit eed38d7
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 130 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Expand Up @@ -1569,7 +1569,7 @@ mocha.setup({
// Use "tdd" interface, ignore leaks, and force all tests to be asynchronous
mocha.setup({
ui: 'tdd',
ignoreLeaks: true,
checkLeaks: false, // default
asyncOnly: true
});
```
Expand Down
79 changes: 27 additions & 52 deletions lib/mocha.js
Expand Up @@ -69,18 +69,18 @@ exports.Test = require('./test');
* @param {boolean} [options.allowUncaught] - Propagate uncaught errors?
* @param {boolean} [options.asyncOnly] - Force `done` callback or promise?
* @param {boolean} [options.bail] - Bail after first test failure?
* @param {boolean} [options.checkLeaks] - If true, check leaks.
* @param {boolean} [options.checkLeaks] - Check for global variable leaks?
* @param {boolean} [options.color] - Color TTY output from reporter?
* @param {boolean} [options.delay] - Delay root suite execution?
* @param {boolean} [options.enableTimeouts] - Enable timeouts?
* @param {boolean} [options.diff] - Show diff on failure?
* @param {string} [options.fgrep] - Test filter given string.
* @param {boolean} [options.forbidOnly] - Tests marked `only` fail the suite?
* @param {boolean} [options.forbidPending] - Pending tests fail the suite?
* @param {boolean} [options.fullStackTrace] - Full stacktrace upon failure?
* @param {boolean} [options.fullTrace] - Full stacktrace upon failure?
* @param {string[]} [options.global] - Variables expected in global scope.
* @param {RegExp|string} [options.grep] - Test filter given regular expression.
* @param {boolean} [options.growl] - Enable desktop notifications?
* @param {boolean} [options.hideDiff] - Suppress diffs from failures?
* @param {boolean} [options.ignoreLeaks] - Ignore global leaks?
* @param {boolean} [options.inlineDiffs] - Display inline diffs?
* @param {boolean} [options.invert] - Invert test filter matches?
* @param {boolean} [options.noHighlighting] - Disable syntax highlighting?
* @param {string} [options.reporter] - Reporter name.
Expand All @@ -89,8 +89,6 @@ exports.Test = require('./test');
* @param {number} [options.slow] - Slow threshold value.
* @param {number|string} [options.timeout] - Timeout threshold value.
* @param {string} [options.ui] - Interface name.
* @param {boolean} [options.color] - Color TTY output from reporter?
* @param {boolean} [options.useInlineDiffs] - Use inline diffs?
*/
function Mocha(options) {
options = utils.assign({}, mocharc, options || {});
Expand All @@ -99,35 +97,14 @@ function Mocha(options) {
// root suite
this.suite = new exports.Suite('', new exports.Context(), true);

if ('useColors' in options) {
utils.deprecate(
'useColors is DEPRECATED and will be removed from a future version of Mocha. Instead, use the "color" option'
);
options.color = 'color' in options ? options.color : options.useColors;
}

// Globals are passed in as options.global, with options.globals for backward compatibility.
options.globals = options.global || options.globals || [];
delete options.global;

this.grep(options.grep)
.fgrep(options.fgrep)
.ui(options.ui)
.bail(options.bail)
.reporter(options.reporter, options.reporterOptions)
.useColors(options.color)
.reporter(options.reporter, options.reporterOption)
.slow(options.slow)
.useInlineDiffs(options.inlineDiffs)
.globals(options.globals);

if ('enableTimeouts' in 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);
}
}
.globals(options.global);

// this guard exists because Suite#timeout does not consider `undefined` to be valid input
if (typeof options.timeout !== 'undefined') {
Expand All @@ -138,10 +115,6 @@ function Mocha(options) {
this.retries(options.retries);
}

if ('diff' in options) {
this.hideDiff(!options.diff);
}

[
'allowUncaught',
'asyncOnly',
Expand Down Expand Up @@ -257,6 +230,8 @@ Mocha.prototype.reporter = function(reporter, reporterOptions) {
}
this._reporter = _reporter;
}
this.options.reporterOption = reporterOptions;
// alias option name is used in public reporters xunit/tap/progress
this.options.reporterOptions = reporterOptions;
return this;
};
Expand Down Expand Up @@ -457,7 +432,7 @@ Mocha.prototype.invert = function() {
*
* @public
* @see {@link Mocha#checkLeaks}
* @param {boolean} ignoreLeaks - Whether to ignore global leaks.
* @param {boolean} [ignoreLeaks=false] - Whether to ignore global leaks.
* @return {Mocha} this
* @chainable
* @example
Expand All @@ -466,7 +441,7 @@ Mocha.prototype.invert = function() {
* mocha.ignoreLeaks(true);
*/
Mocha.prototype.ignoreLeaks = function(ignoreLeaks) {
this.options.ignoreLeaks = Boolean(ignoreLeaks);
this.options.checkLeaks = !ignoreLeaks;
return this;
};

Expand All @@ -480,7 +455,7 @@ Mocha.prototype.ignoreLeaks = function(ignoreLeaks) {
* @chainable
*/
Mocha.prototype.checkLeaks = function() {
this.options.ignoreLeaks = false;
this.options.checkLeaks = true;
return this;
};

Expand All @@ -492,7 +467,7 @@ Mocha.prototype.checkLeaks = function() {
* @chainable
*/
Mocha.prototype.fullTrace = function() {
this.options.fullStackTrace = true;
this.options.fullTrace = true;
return this;
};

Expand Down Expand Up @@ -555,7 +530,7 @@ Mocha.prototype._growl = growl.notify;
* mocha.globals(['jQuery', 'MyLib']);
*/
Mocha.prototype.globals = function(globals) {
this.options.globals = this.options.globals
this.options.global = (this.options.global || [])
.concat(globals)
.filter(Boolean)
.filter(function(elt, idx, arr) {
Expand All @@ -574,7 +549,7 @@ Mocha.prototype.globals = function(globals) {
*/
Mocha.prototype.useColors = function(colors) {
if (colors !== undefined) {
this.options.useColors = colors;
this.options.color = colors;
}
return this;
};
Expand All @@ -584,25 +559,25 @@ Mocha.prototype.useColors = function(colors) {
* in test failure output.
*
* @public
* @param {boolean} inlineDiffs - Whether to use inline diffs.
* @param {boolean} [inlineDiffs=false] - Whether to use inline diffs.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.useInlineDiffs = function(inlineDiffs) {
this.options.useInlineDiffs = inlineDiffs !== undefined && inlineDiffs;
this.options.inlineDiffs = inlineDiffs !== undefined && inlineDiffs;
return this;
};

/**
* Determines if reporter should include diffs in test failure output.
*
* @public
* @param {boolean} hideDiff - Whether to hide diffs.
* @param {boolean} [hideDiff=false] - Whether to hide diffs.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.hideDiff = function(hideDiff) {
this.options.hideDiff = hideDiff !== undefined && hideDiff;
this.options.diff = !(hideDiff === true);
return this;
};

Expand Down Expand Up @@ -814,26 +789,26 @@ Mocha.prototype.run = function(fn) {
var runner = new exports.Runner(suite, options.delay);
createStatsCollector(runner);
var reporter = new this._reporter(runner, options);
runner.ignoreLeaks = options.ignoreLeaks !== false;
runner.fullStackTrace = options.fullStackTrace;
runner.checkLeaks = options.checkLeaks === true;
runner.fullStackTrace = options.fullTrace;
runner.asyncOnly = options.asyncOnly;
runner.allowUncaught = options.allowUncaught;
runner.forbidOnly = options.forbidOnly;
runner.forbidPending = options.forbidPending;
if (options.grep) {
runner.grep(options.grep, options.invert);
}
if (options.globals) {
runner.globals(options.globals);
if (options.global) {
runner.globals(options.global);
}
if (options.growl) {
this._growl(runner);
}
if (options.useColors !== undefined) {
exports.reporters.Base.useColors = options.useColors;
if (options.color !== undefined) {
exports.reporters.Base.useColors = options.color;
}
exports.reporters.Base.inlineDiffs = options.useInlineDiffs;
exports.reporters.Base.hideDiff = options.hideDiff;
exports.reporters.Base.inlineDiffs = options.inlineDiffs;
exports.reporters.Base.hideDiff = !options.diff;

function done(failures) {
fn = fn || utils.noop;
Expand Down
2 changes: 1 addition & 1 deletion lib/runner.js
Expand Up @@ -244,7 +244,7 @@ Runner.prototype.globals = function(arr) {
* @private
*/
Runner.prototype.checkGlobals = function(test) {
if (this.ignoreLeaks) {
if (!this.checkLeaks) {
return;
}
var ok = this._globals;
Expand Down

0 comments on commit eed38d7

Please sign in to comment.