Skip to content

Commit

Permalink
add option dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed May 29, 2021
1 parent 1c4e623 commit f05159d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/cli/run-option-metadata.js
Expand Up @@ -32,6 +32,7 @@ const TYPES = (exports.types = {
'color',
'delay',
'diff',
'dry-run',
'exit',
'forbid-only',
'forbid-pending',
Expand Down
4 changes: 4 additions & 0 deletions lib/cli/run.js
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/mocha.js
Expand Up @@ -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?
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 9 additions & 7 deletions lib/runner.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -554,16 +556,15 @@ 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) {
parentTitle = hook.parent.title;
} else {
parentTitle = hook.parent.root ? '{root}' : '';
}
hook.title = hook.originalTitle + ' in ' + dQuote(parentTitle);
hook.title = `${hook.originalTitle} in "${parentTitle}"`;
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f05159d

Please sign in to comment.