Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

ensure invalid arguments are not ignored when using bin/mocha; closes #3687

Christopher Hiller8b7e3da4aec3
diff --git a/lib/cli/options.js b/lib/cli/options.js
index 00ce35b7..85412041 100644
--- a/lib/cli/options.js
+++ b/lib/cli/options.js
@@ -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
@@ -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(
{
@@ -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
diff --git a/test/integration/invalid-arguments.spec.js b/test/integration/invalid-arguments.spec.js
new file mode 100644
index 00000000..6c147bdb
--- /dev/null
+++ b/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'}
+ );
+ });
+});