Skip to content

Commit

Permalink
Deconstruct and normalize CLI options in one place
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Thomas Scholtes committed Jun 25, 2019
1 parent 0c63f7b commit b8d2d42
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
22 changes: 8 additions & 14 deletions lib/cli/collect-files.js
Expand Up @@ -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 => {
Expand Down
46 changes: 30 additions & 16 deletions lib/cli/run-helpers.js
Expand Up @@ -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);
};

/**
Expand All @@ -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, {ui, grep}, fileCollectParams);
} else {
exports.singleRun(mocha, options);
exports.singleRun(mocha, {exit}, fileCollectParams);
}
};

Expand Down
13 changes: 9 additions & 4 deletions lib/cli/watch-run.js
Expand Up @@ -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();
Expand All @@ -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 = () => {
Expand Down

0 comments on commit b8d2d42

Please sign in to comment.