Skip to content

Commit

Permalink
ensure invalid arguments are not ignored when using bin/mocha; closes #…
Browse files Browse the repository at this point in the history
…3687

Signed-off-by: Christopher Hiller <boneskull@boneskull.com>
  • Loading branch information
boneskull committed Jan 24, 2019
1 parent 0899fdd commit 52457cf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/cli/options.js
Expand Up @@ -17,6 +17,7 @@ const {loadConfig, findConfig} = require('./config');
const findup = require('findup-sync');
const {deprecate} = require('../utils');
const debug = require('debug')('mocha:cli:options');
const {createMissingArgumentError} = require('../errors');

/**
* The `yargs-parser` namespace
Expand Down Expand Up @@ -73,8 +74,8 @@ const nargOpts = types.array
* @private
* @ignore
*/
const parse = (args = [], ...configObjects) =>
yargsParser(
const parse = (args = [], ...configObjects) => {
const result = yargsParser.detailed(
args,
Object.assign(
{
Expand All @@ -87,6 +88,11 @@ const parse = (args = [], ...configObjects) =>
types
)
);
if (result.error) {
throw createMissingArgumentError(result.error.message);
}
return result.argv;
};

/**
* - Replaces comments with empty strings
Expand Down Expand Up @@ -254,6 +260,9 @@ module.exports.loadPkgRc = loadPkgRc;
*/
const loadOptions = (argv = []) => {
let args = parse(argv);
if (args.error) {
throw createMissingArgumentError(args.error.message);
}
// short-circuit: look for a flag that would abort loading of mocha.opts
if (
Array.from(ONE_AND_DONE_ARGS).reduce(
Expand Down
20 changes: 20 additions & 0 deletions test/integration/invalid-arguments.spec.js
@@ -0,0 +1,20 @@
'use strict';

var invokeMocha = require('./helpers').invokeMocha;

describe('invalid arguments', function() {
it('should exit with failure if arguments are invalid', function(done) {
invokeMocha(
['--ui'],
function(err, result) {
if (err) {
return done(err);
}
expect(result, 'to have failed');
expect(result.output, 'to match', /not enough arguments/i);
done();
},
{stdio: 'pipe'}
);
});
});

0 comments on commit 52457cf

Please sign in to comment.