Skip to content

Commit

Permalink
add new option 'fail-zero'
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed Aug 12, 2021
1 parent 09ffc30 commit bea6fdd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
1 change: 1 addition & 0 deletions lib/cli/run-option-metadata.js
Expand Up @@ -35,6 +35,7 @@ const TYPES = (exports.types = {
'diff',
'dry-run',
'exit',
'fail-zero',
'forbid-only',
'forbid-pending',
'full-trace',
Expand Down
4 changes: 4 additions & 0 deletions lib/cli/run.js
Expand Up @@ -98,6 +98,10 @@ exports.builder = yargs =>
requiresArg: true,
coerce: list
},
'fail-zero': {
description: 'Fail if no test(s) encountered',
group: GROUPS.RULES
},
fgrep: {
conflicts: 'grep',
description: 'Only run tests containing this string',
Expand Down
47 changes: 32 additions & 15 deletions lib/mocha.js
Expand Up @@ -164,6 +164,7 @@ exports.run = function(...args) {
* @param {boolean} [options.delay] - Delay root suite execution?
* @param {boolean} [options.diff] - Show diff on failure?
* @param {boolean} [options.dryRun] - Report tests without running them?
* @param {boolean} [options.failZero] - Fail test run if zero tests?
* @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?
Expand Down Expand Up @@ -223,6 +224,7 @@ function Mocha(options = {}) {
'delay',
'diff',
'dryRun',
'failZero',
'forbidOnly',
'forbidPending',
'fullTrace',
Expand Down Expand Up @@ -778,20 +780,6 @@ Mocha.prototype.diff = function(diff) {
return this;
};

/**
* Enables or disables running tests in dry-run mode.
*
* @public
* @see [CLI option](../#-dry-run)
* @param {boolean} [dryRun=true] - Whether to activate dry-run mode.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.dryRun = function(dryRun) {
this.options.dryRun = dryRun !== false;
return this;
};

/**
* @summary
* Sets timeout threshold value.
Expand Down Expand Up @@ -918,6 +906,34 @@ Mocha.prototype.delay = function delay() {
return this;
};

/**
* Enables or disables running tests in dry-run mode.
*
* @public
* @see [CLI option](../#-dry-run)
* @param {boolean} [dryRun=true] - Whether to activate dry-run mode.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.dryRun = function(dryRun) {
this.options.dryRun = dryRun !== false;
return this;
};

/**
* Fails test run if no tests encountered with exit-code 1.
*
* @public
* @see [CLI option](../#-fail-zero)
* @param {boolean} [failZero=true] - Whether to fail test run.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.failZero = function(failZero) {
this.options.failZero = failZero !== false;
return this;
};

/**
* Causes tests marked `only` to fail the suite.
*
Expand Down Expand Up @@ -1023,9 +1039,10 @@ Mocha.prototype.run = function(fn) {
var options = this.options;
options.files = this.files;
const runner = new this._runnerClass(suite, {
cleanReferencesAfterRun: this._cleanReferencesAfterRun,
delay: options.delay,
dryRun: options.dryRun,
cleanReferencesAfterRun: this._cleanReferencesAfterRun
failZero: options.failZero
});
createStatsCollector(runner);
var reporter = new this._reporter(runner, options);
Expand Down
7 changes: 5 additions & 2 deletions lib/runner.js
Expand Up @@ -135,10 +135,11 @@ class Runner extends EventEmitter {
* @public
* @class
* @param {Suite} suite - Root suite
* @param {Object|boolean} [opts] - Options. If `boolean` (deprecated), whether or not to delay execution of root suite until ready.
* @param {Object|boolean} [opts] - Options. If `boolean` (deprecated), whether to delay execution of root suite until ready.
* @param {boolean} [opts.cleanReferencesAfterRun] - Whether to clean references to test fns and hooks when a suite is done.
* @param {boolean} [opts.delay] - Whether to delay execution of root suite until ready.
* @param {boolean} [opts.dryRun] - Whether to report tests without running them.
* @param {boolean} [opts.cleanReferencesAfterRun] - Whether to clean references to test fns and hooks when a suite is done.
* @param {boolean} [options.failZero] - Wether to fail test run if zero tests encountered.
*/
constructor(suite, opts) {
super();
Expand Down Expand Up @@ -1044,6 +1045,8 @@ Runner.prototype.run = function(fn, opts = {}) {
fn = fn || function() {};

const end = () => {
if (!this.total && this._opts.failZero) this.failures = 1;

debug('run(): root suite completed; emitting %s', constants.EVENT_RUN_END);
this.emit(constants.EVENT_RUN_END);
};
Expand Down

0 comments on commit bea6fdd

Please sign in to comment.