Skip to content

Commit 52457cf

Browse files
committed
ensure invalid arguments are not ignored when using bin/mocha; closes #3687
Signed-off-by: Christopher Hiller <[email protected]>
1 parent 0899fdd commit 52457cf

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/cli/options.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {loadConfig, findConfig} = require('./config');
1717
const findup = require('findup-sync');
1818
const {deprecate} = require('../utils');
1919
const debug = require('debug')('mocha:cli:options');
20+
const {createMissingArgumentError} = require('../errors');
2021

2122
/**
2223
* The `yargs-parser` namespace
@@ -73,8 +74,8 @@ const nargOpts = types.array
7374
* @private
7475
* @ignore
7576
*/
76-
const parse = (args = [], ...configObjects) =>
77-
yargsParser(
77+
const parse = (args = [], ...configObjects) => {
78+
const result = yargsParser.detailed(
7879
args,
7980
Object.assign(
8081
{
@@ -87,6 +88,11 @@ const parse = (args = [], ...configObjects) =>
8788
types
8889
)
8990
);
91+
if (result.error) {
92+
throw createMissingArgumentError(result.error.message);
93+
}
94+
return result.argv;
95+
};
9096

9197
/**
9298
* - Replaces comments with empty strings
@@ -254,6 +260,9 @@ module.exports.loadPkgRc = loadPkgRc;
254260
*/
255261
const loadOptions = (argv = []) => {
256262
let args = parse(argv);
263+
if (args.error) {
264+
throw createMissingArgumentError(args.error.message);
265+
}
257266
// short-circuit: look for a flag that would abort loading of mocha.opts
258267
if (
259268
Array.from(ONE_AND_DONE_ARGS).reduce(
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
var invokeMocha = require('./helpers').invokeMocha;
4+
5+
describe('invalid arguments', function() {
6+
it('should exit with failure if arguments are invalid', function(done) {
7+
invokeMocha(
8+
['--ui'],
9+
function(err, result) {
10+
if (err) {
11+
return done(err);
12+
}
13+
expect(result, 'to have failed');
14+
expect(result.output, 'to match', /not enough arguments/i);
15+
done();
16+
},
17+
{stdio: 'pipe'}
18+
);
19+
});
20+
});

0 commit comments

Comments
 (0)