From 110ffcca77d64e6de5a880dbf9e79fafd77088cb Mon Sep 17 00:00:00 2001 From: juergba Date: Thu, 12 Aug 2021 10:45:07 +0200 Subject: [PATCH] add new option 'fail-zero' --- lib/cli/run-option-metadata.js | 1 + lib/cli/run.js | 4 ++++ lib/mocha.js | 1 + lib/runner.js | 2 ++ 4 files changed, 8 insertions(+) diff --git a/lib/cli/run-option-metadata.js b/lib/cli/run-option-metadata.js index 6bc6604018..984ad4b75e 100644 --- a/lib/cli/run-option-metadata.js +++ b/lib/cli/run-option-metadata.js @@ -35,6 +35,7 @@ const TYPES = (exports.types = { 'diff', 'dry-run', 'exit', + 'fail-zero', 'forbid-only', 'forbid-pending', 'full-trace', diff --git a/lib/cli/run.js b/lib/cli/run.js index 06941fab8c..160315cd75 100644 --- a/lib/cli/run.js +++ b/lib/cli/run.js @@ -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', diff --git a/lib/mocha.js b/lib/mocha.js index 62a414a9fa..c501d91682 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -1025,6 +1025,7 @@ Mocha.prototype.run = function(fn) { const runner = new this._runnerClass(suite, { delay: options.delay, dryRun: options.dryRun, + failZero: options.failZero, cleanReferencesAfterRun: this._cleanReferencesAfterRun }); createStatsCollector(runner); diff --git a/lib/runner.js b/lib/runner.js index 079ab3066d..388a8f394d 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -1044,6 +1044,8 @@ Runner.prototype.run = function(fn, opts = {}) { fn = fn || function() {}; const end = () => { + if (!this.total && options.failZero) this.failures = 1; + debug('run(): root suite completed; emitting %s', constants.EVENT_RUN_END); this.emit(constants.EVENT_RUN_END); };