From f05159d56bfee4efae43dc448b20a1acd9ae2ae4 Mon Sep 17 00:00:00 2001 From: juergba Date: Sat, 29 May 2021 08:07:40 +0200 Subject: [PATCH] add option dry-run --- lib/cli/run-option-metadata.js | 1 + lib/cli/run.js | 4 ++++ lib/mocha.js | 2 ++ lib/runner.js | 16 +++++++++------- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/cli/run-option-metadata.js b/lib/cli/run-option-metadata.js index 33cd15ae08..fc870992c9 100644 --- a/lib/cli/run-option-metadata.js +++ b/lib/cli/run-option-metadata.js @@ -32,6 +32,7 @@ const TYPES = (exports.types = { 'color', 'delay', 'diff', + 'dry-run', 'exit', 'forbid-only', 'forbid-pending', diff --git a/lib/cli/run.js b/lib/cli/run.js index a8c8b619b3..1ef0af8c4f 100644 --- a/lib/cli/run.js +++ b/lib/cli/run.js @@ -83,6 +83,10 @@ exports.builder = yargs => description: 'Show diff on failure', group: GROUPS.OUTPUT }, + 'dry-run': { + description: 'List tests without execution', + group: GROUPS.RULES + }, exit: { description: 'Force Mocha to quit after tests complete', group: GROUPS.RULES diff --git a/lib/mocha.js b/lib/mocha.js index f1f2e25dd7..dfc994c272 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -164,6 +164,7 @@ exports.run = function(...args) { * @param {boolean} [options.color] - Color TTY output from reporter? * @param {boolean} [options.delay] - Delay root suite execution? * @param {boolean} [options.diff] - Show diff on failure? + * @param {boolean} [options.dryRun] - Display list of 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? @@ -1016,6 +1017,7 @@ Mocha.prototype.run = function(fn) { options.files = this.files; const runner = new this._runnerClass(suite, { delay: options.delay, + dryRun: options.dryRun, cleanReferencesAfterRun: this._cleanReferencesAfterRun }); createStatsCollector(runner); diff --git a/lib/runner.js b/lib/runner.js index 9ff34171d4..771727d034 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -19,7 +19,6 @@ var EVENT_ROOT_SUITE_RUN = Suite.constants.EVENT_ROOT_SUITE_RUN; var STATE_FAILED = Runnable.constants.STATE_FAILED; var STATE_PASSED = Runnable.constants.STATE_PASSED; var STATE_PENDING = Runnable.constants.STATE_PENDING; -var dQuote = utils.dQuote; var sQuote = utils.sQuote; var stackFilter = utils.stackTraceFilter(); var stringify = utils.stringify; @@ -140,6 +139,7 @@ class Runner extends EventEmitter { * @param {Suite} suite - Root suite * @param {Object|boolean} [opts] - Options. If `boolean`, whether or not to delay execution of root suite until ready (for backwards compatibility). * @param {boolean} [opts.delay] - Whether to delay execution of root suite until ready. + * @param {boolean} [opts.dryRun] - Whether to list tests without running them. * @param {boolean} [opts.cleanReferencesAfterRun] - Whether to clean references to test fns and hooks when a suite is done. */ constructor(suite, opts) { @@ -476,6 +476,8 @@ Runner.prototype.fail = function(test, err, force) { */ Runner.prototype.hook = function(name, fn) { + if (this._opts.dryRun) return fn(); + var suite = this.suite; var hooks = suite.getHooks(name); var self = this; @@ -554,8 +556,7 @@ Runner.prototype.hook = function(name, fn) { function setHookTitle(hook) { hook.originalTitle = hook.originalTitle || hook.title; if (hook.ctx && hook.ctx.currentTest) { - hook.title = - hook.originalTitle + ' for ' + dQuote(hook.ctx.currentTest.title); + hook.title = `${hook.originalTitle} for "${hook.ctx.currentTest.title}"`; } else { var parentTitle; if (hook.parent.title) { @@ -563,7 +564,7 @@ Runner.prototype.hook = function(name, fn) { } else { parentTitle = hook.parent.root ? '{root}' : ''; } - hook.title = hook.originalTitle + ' in ' + dQuote(parentTitle); + hook.title = `${hook.originalTitle} in "${parentTitle}"`; } } } @@ -609,7 +610,7 @@ Runner.prototype.hooks = function(name, suites, fn) { }; /** - * Run hooks from the top level down. + * Run 'afterEach' hooks from bottom up. * * @param {String} name * @param {Function} fn @@ -621,7 +622,7 @@ Runner.prototype.hookUp = function(name, fn) { }; /** - * Run hooks from the bottom up. + * Run 'beforeEach' hooks from top level down. * * @param {String} name * @param {Function} fn @@ -656,6 +657,8 @@ Runner.prototype.parents = function() { * @private */ Runner.prototype.runTest = function(fn) { + if (this._opts.dryRun) return fn(); + var self = this; var test = this.test; @@ -701,7 +704,6 @@ Runner.prototype.runTests = function(suite, fn) { self.suite = after ? errSuite.parent : errSuite; if (self.suite) { - // call hookUp afterEach self.hookUp(HOOK_TYPE_AFTER_EACH, function(err2, errSuite2) { self.suite = orig; // some hooks may fail even now