Skip to content

Commit

Permalink
added dashed arg support to fix jestjs#7424
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Hargrove committed Dec 11, 2018
1 parent 509272d commit 7c3e1ef
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
52 changes: 52 additions & 0 deletions e2e/__tests__/supports-dashed-args.js
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2018-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';

import path from 'path';
import runJest from '../runJest';

const consoleDir = path.resolve(__dirname, '../console');
const eachDir = path.resolve(__dirname, '../each');

expect.addSnapshotSerializer({
print: value => value,
test: received => typeof received === 'string',
});

test('works with passing tests', () => {
const result = runJest(eachDir, [
'success.test.js',
'--runInBand',
'--collect-coverage',
'--coverageReporters',
'text-summary',
'--detect-open-handles',
]);
expect(result.status).toBe(0);
});

test('throws error for unknown dashed & camelcase args', () => {
const result = runJest(consoleDir, [
'--doesNotExist',
'--also-does-not-exist',
]);
expect(result.status).toBe(1);
expect(result.stderr).toMatchInlineSnapshot(`
● Unrecognized CLI Parameters:
Following options were not recognized:
["doesNotExist", "also-does-not-exist"]
CLI Options Documentation:
https://jestjs.io/docs/en/cli.html
`);
});
6 changes: 5 additions & 1 deletion packages/jest-cli/src/cli/index.js
Expand Up @@ -175,7 +175,8 @@ const readResultsAndExit = (
};

const buildArgv = (maybeArgv: ?Argv, project: ?Path) => {
const argv: Argv = yargs(maybeArgv || process.argv.slice(2))
const rawArgv: Argv | string[] = maybeArgv || process.argv.slice(2);
const argv: Argv = yargs(rawArgv)
.usage(args.usage)
.alias('help', 'h')
.options(args.options)
Expand All @@ -185,6 +186,9 @@ const buildArgv = (maybeArgv: ?Argv, project: ?Path) => {
validateCLIOptions(
argv,
Object.assign({}, args.options, {deprecationEntries}),
Array.isArray(rawArgv)
? rawArgv.map(rawArgv => rawArgv.replace(/^--?/, ''))
: Object.keys(rawArgv),
);

return argv;
Expand Down
19 changes: 15 additions & 4 deletions packages/jest-validate/src/validateCLIOptions.js
Expand Up @@ -65,16 +65,27 @@ const logDeprecatedOptions = (
});
};

export default function validateCLIOptions(argv: Argv, options: Object) {
export default function validateCLIOptions(
argv: Argv,
options: Object,
rawArgv: string[] = [],
) {
const yargsSpecialOptions = ['$0', '_', 'help', 'h'];
const deprecationEntries = options.deprecationEntries || {};
const allowedOptions = Object.keys(options).reduce(
(acc, option) => acc.add(option).add(options[option].alias || option),
new Set(yargsSpecialOptions),
);
const unrecognizedOptions = Object.keys(argv).filter(
arg => !allowedOptions.has(arg),
);
const unrecognizedOptions = Object.keys(argv).filter(arg => {
const camelCased = arg.replace(/-([^-])/g, (a, b) => b.toUpperCase());
if (
!allowedOptions.has(camelCased) &&
(!rawArgv.length || rawArgv.includes(arg))
) {
return true;
}
return false;
}, []);

if (unrecognizedOptions.length) {
throw createCLIValidationError(unrecognizedOptions, allowedOptions);
Expand Down

0 comments on commit 7c3e1ef

Please sign in to comment.