From ad0a13daa0c90a51654c973464efddd7a5af4b3a Mon Sep 17 00:00:00 2001 From: Thomas Scholtes Date: Mon, 24 Jun 2019 14:57:59 +0200 Subject: [PATCH] Deconstruct and normalize CLI options in one place Instead of passing the whole object with all the CLI options down to `singleRun` and `watchRun` and then to `collecFiles` we create a dedicated object with all the parameters for collecting test files and pass this object. This approach has the advantage that we only make only the options that are strictly necessary available to the two `run` functions. This also consolidates the normalization of CLI options in one place (`runMocha`) instead of spreading it also to `collectFiles`. --- lib/cli/collect-files.js | 22 +++++++------------ lib/cli/run-helpers.js | 46 ++++++++++++++++++++++++++-------------- lib/cli/watch-run.js | 13 ++++++++---- 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/lib/cli/collect-files.js b/lib/cli/collect-files.js index f4fc10ba31..ad4699ec6e 100644 --- a/lib/cli/collect-files.js +++ b/lib/cli/collect-files.js @@ -8,23 +8,17 @@ const utils = require('../utils'); /** * Smash together an array of test files in the correct order - * @param {Object} [opts] - Options - * @param {string[]} [opts.extension] - File extensions to use - * @param {string[]} [opts.spec] - Files, dirs, globs to run - * @param {string[]} [opts.ignore] - Files, dirs, globs to ignore - * @param {boolean} [opts.recursive=false] - Find files recursively - * @param {boolean} [opts.sort=false] - Sort test files + * @param {Object} opts - Options + * @param {string[]} opts.extension - File extensions to use + * @param {string[]} opts.spec - Files, dirs, globs to run + * @param {string[]} opts.ignore - Files, dirs, globs to ignore + * @param {string[]} opts.file - List of additional files to include + * @param {boolean} opts.recursive - Find files recursively + * @param {boolean} opts.sort - Sort test files * @returns {string[]} List of files to test * @private */ -module.exports = ({ - ignore = [], - extension = [], - file = [], - recursive = false, - sort = false, - spec = [] -} = {}) => { +module.exports = ({ignore, extension, file, recursive, sort, spec} = {}) => { let files = []; const unmatched = []; spec.forEach(arg => { diff --git a/lib/cli/run-helpers.js b/lib/cli/run-helpers.js index 791b88a8c5..26b44c0070 100644 --- a/lib/cli/run-helpers.js +++ b/lib/cli/run-helpers.js @@ -96,14 +96,16 @@ exports.handleRequires = (requires = []) => { * @param {Mocha} mocha - Mocha instance * @param {Options} [opts] - Command line options * @param {boolean} [opts.exit] - Whether or not to force-exit after tests are complete + * @param {Object} fileCollectParams - Parameters that control test + * file collection. See `lib/cli/collect-files.js`. * @returns {Runner} * @private */ -exports.singleRun = (mocha, options) => { - const files = collectFiles(options); +exports.singleRun = (mocha, {exit}, fileCollectParams) => { + const files = collectFiles(fileCollectParams); debug('running tests with files', files); mocha.files = files; - return mocha.run(options.exit ? exitMocha : exitMochaLater); + return mocha.run(exit ? exitMocha : exitMochaLater); }; /** @@ -113,20 +115,32 @@ exports.singleRun = (mocha, options) => { * @private */ exports.runMocha = (mocha, options) => { - options = Object.assign( - { - watch: false, - extension: [], - grep: '', - ui: 'bdd', - exit: false - }, - options - ); - if (options.watch) { - watchRun(mocha, options); + const { + watch = false, + extension = [], + grep = '', + ui = 'bdd', + exit = false, + ignore = [], + file = [], + recursive = false, + sort = false, + spec = [] + } = options; + + const fileCollectParams = { + ignore, + extension, + file, + recursive, + sort, + spec + }; + + if (watch) { + watchRun(mocha, {exit}, fileCollectParams); } else { - exports.singleRun(mocha, options); + exports.singleRun(mocha, {ui, grep}, fileCollectParams); } }; diff --git a/lib/cli/watch-run.js b/lib/cli/watch-run.js index 6a77e09937..a2264c812c 100644 --- a/lib/cli/watch-run.js +++ b/lib/cli/watch-run.js @@ -15,12 +15,17 @@ const collectFiles = require('./collect-files'); /** * Run Mocha in "watch" mode * @param {Mocha} mocha - Mocha instance - * @param {Object} [opts] - Command line options + * @param {Object} opts - Options + * @param {string|RegExp} opts.grep - Grep for test titles + * @param {string} opts.ui - User interface + * @param {Object} fileCollectParams - Parameters that control test + * file collection. See `lib/cli/collect-files.js`. + * @param {string[]} fileCollectParams.extension - List of extensions to watch * @private */ -module.exports = (mocha, options) => { +module.exports = (mocha, options, fileCollectParams) => { let runner; - const files = collectFiles(options); + const files = collectFiles(fileCollectParams); console.log(); hideCursor(); @@ -32,7 +37,7 @@ module.exports = (mocha, options) => { process.exit(128 + 2); }); - const watchFiles = utils.files(process.cwd(), options.extension); + const watchFiles = utils.files(process.cwd(), fileCollectParams.extension); let runAgain = false; const loadAndRun = () => {