diff --git a/lib/cli/options.js b/lib/cli/options.js index fc0c951a8c..7886b510a8 100644 --- a/lib/cli/options.js +++ b/lib/cli/options.js @@ -110,15 +110,19 @@ const parse = (args = [], defaultValues = {}, ...configObjects) => { // 4. we can then reapply the values after yargs-parser is done. const nodeArgs = (Array.isArray(args) ? args : args.split(' ')).reduce( (acc, arg) => { - const pair = arg.split('='); - let flag = pair[0]; - if (isNodeFlag(flag, false)) { - flag = flag.replace(/^--?/, ''); - return arg.includes('=') - ? acc.concat([[flag, pair[1]]]) - : acc.concat([[flag, true]]); + try { + const pair = arg.split('='); + let flag = pair[0]; + if (isNodeFlag(flag, false)) { + flag = flag.replace(/^--?/, ''); + return arg.includes('=') + ? acc.concat([[flag, pair[1]]]) + : acc.concat([[flag, true]]); + } + return acc; + } catch (err) { + throw new Error(`Invalid option ${arg} passed to mocha cli`); } - return acc; }, [] ); @@ -192,8 +196,7 @@ const loadPkgRc = (args = {}) => { filepath ); } else { - debug('failed to read default package.json at %s; ignoring', - filepath); + debug('failed to read default package.json at %s; ignoring', filepath); return result; } } diff --git a/test/node-unit/cli/options.spec.js b/test/node-unit/cli/options.spec.js index 7c846a37ed..22ed6830bb 100644 --- a/test/node-unit/cli/options.spec.js +++ b/test/node-unit/cli/options.spec.js @@ -204,9 +204,7 @@ describe('options', function () { const filepath = '/some/package.json'; readFileSync = sinon.stub(); // package.json - readFileSync - .onFirstCall() - .returns('{definitely-invalid'); + readFileSync.onFirstCall().returns('{definitely-invalid'); findConfig = sinon.stub().returns('/some/.mocharc.json'); loadConfig = sinon.stub().returns({}); findupSync = sinon.stub().returns(filepath); @@ -224,7 +222,7 @@ describe('options', function () { loadOptions(); }, 'to throw', - /SyntaxError/, + /SyntaxError/ ); }); }); @@ -676,5 +674,48 @@ describe('options', function () { ]); }); }); + + describe('"numerical arguments"', function () { + const numericalArg = 123; + + beforeEach(function () { + readFileSync = sinon.stub(); + findConfig = sinon.stub(); + loadConfig = sinon.stub(); + findupSync = sinon.stub(); + loadOptions = proxyLoadOptions({ + readFileSync, + findConfig, + loadConfig, + findupSync + }); + }); + + it('throws error when numerical option is passed to cli', function () { + expect(() => loadOptions(`${numericalArg}`), 'to throw', { + message: `Invalid option ${numericalArg} passed to mocha cli` + }); + }); + + it('throws error when numerical argument is passed to mocha flag that does not accept numerical value', function () { + expect(() => loadOptions(`--delay ${numericalArg}`), 'to throw', { + message: `Invalid option ${numericalArg} passed to mocha cli` + }); + }); + + it('does not throw error if numerical value is passed to a compatible mocha flag', function () { + expect(() => loadOptions(`--retries ${numericalArg}`), 'not to throw'); + }); + + it('does not throw error if numerical value is passed to a node options', function () { + expect( + () => + loadOptions( + `--secure-heap-min=${numericalArg} --conditions=${numericalArg}` + ), + 'not to throw' + ); + }); + }); }); });