From 90ce0fd9845a2d828725c44f4dda01ca8dc4d0fb Mon Sep 17 00:00:00 2001 From: Steven Hargrove Date: Thu, 13 Dec 2018 08:14:49 -0500 Subject: [PATCH] use camelcase, filter dashed args on returns config --- .../jest-cli/src/__tests__/cli/args.test.js | 13 +++++++++++++ packages/jest-cli/src/cli/index.js | 13 +++++++++++-- packages/jest-validate/package.json | 1 + .../jest-validate/src/validateCLIOptions.js | 17 +++++++---------- yarn.lock | 5 +++++ 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/packages/jest-cli/src/__tests__/cli/args.test.js b/packages/jest-cli/src/__tests__/cli/args.test.js index 88f3faf1c4c5..88d16dc43591 100644 --- a/packages/jest-cli/src/__tests__/cli/args.test.js +++ b/packages/jest-cli/src/__tests__/cli/args.test.js @@ -10,6 +10,7 @@ import type {Argv} from 'types/Argv'; import {check} from '../../cli/args'; +import {buildArgv} from '../../cli'; describe('check', () => { it('returns true if the arguments are valid', () => { @@ -59,3 +60,15 @@ describe('check', () => { ); }); }); + +describe('buildArgv', () => { + it('should return only camelcased args ', () => { + const mockProcessArgv = jest + .spyOn(process.argv, 'slice') + .mockImplementation(() => ['--clear-mocks']); + const actual = buildArgv(null); + expect(actual).not.toHaveProperty('clear-mocks'); + expect(actual).toHaveProperty('clearMocks', true); + mockProcessArgv.mockRestore(); + }); +}); diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index a45ec7226e84..65de1406434e 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -35,6 +35,7 @@ import logDebugMessages from '../lib/log_debug_messages'; export async function run(maybeArgv?: Argv, project?: Path) { try { + // $FlowFixMe:`allow reduced return const argv: Argv = buildArgv(maybeArgv, project); if (argv.init) { @@ -174,7 +175,7 @@ const readResultsAndExit = ( } }; -const buildArgv = (maybeArgv: ?Argv, project: ?Path) => { +export const buildArgv = (maybeArgv: ?Argv, project: ?Path) => { const rawArgv: Argv | string[] = maybeArgv || process.argv.slice(2); const argv: Argv = yargs(rawArgv) .usage(args.usage) @@ -186,12 +187,20 @@ const buildArgv = (maybeArgv: ?Argv, project: ?Path) => { validateCLIOptions( argv, Object.assign({}, args.options, {deprecationEntries}), + // strip leading dashes Array.isArray(rawArgv) ? rawArgv.map(rawArgv => rawArgv.replace(/^--?/, '')) : Object.keys(rawArgv), ); - return argv; + // strip dashed args + return Object.keys(argv).reduce((result, key) => { + if (!key.includes('-')) { + // $FlowFixMe:`allow reduced return + result[key] = argv[key]; + } + return result; + }, {}); }; const getProjectListFromCLIArgs = (argv, project: ?Path) => { diff --git a/packages/jest-validate/package.json b/packages/jest-validate/package.json index 33a9d90b4bb4..6ea5a6e5b68f 100644 --- a/packages/jest-validate/package.json +++ b/packages/jest-validate/package.json @@ -8,6 +8,7 @@ "license": "MIT", "main": "build/index.js", "dependencies": { + "camelcase": "^5.0.0", "chalk": "^2.0.1", "jest-get-type": "^22.1.0", "leven": "^2.1.0", diff --git a/packages/jest-validate/src/validateCLIOptions.js b/packages/jest-validate/src/validateCLIOptions.js index 67d0e8704242..006af5162fcf 100644 --- a/packages/jest-validate/src/validateCLIOptions.js +++ b/packages/jest-validate/src/validateCLIOptions.js @@ -10,6 +10,7 @@ import type {Argv} from 'types/Argv'; import chalk from 'chalk'; +import camelcase from 'camelcase'; import {createDidYouMeanMessage, format, ValidationError} from './utils'; import {deprecationWarning} from './deprecated'; import defaultConfig from './defaultConfig'; @@ -76,16 +77,12 @@ export default function validateCLIOptions( (acc, option) => acc.add(option).add(options[option].alias || option), new Set(yargsSpecialOptions), ); - 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; - }, []); + const unrecognizedOptions = Object.keys(argv).filter( + arg => + !allowedOptions.has(camelcase(arg)) && + (!rawArgv.length || rawArgv.includes(arg)), + [], + ); if (unrecognizedOptions.length) { throw createCLIValidationError(unrecognizedOptions, allowedOptions); diff --git a/yarn.lock b/yarn.lock index e41514cbff50..adfe56c0c2a3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3437,6 +3437,11 @@ camelcase@^4.1.0: resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd" integrity sha1-1UVjW+HjPFQmScaRc+Xeas+uNN0= +camelcase@^5.0.0: + version "5.0.0" + resolved "https://repo.artifacts.weather.com/api/npm/web-decoupling-npm-virtual/camelcase/-/camelcase-5.0.0.tgz#03295527d58bd3cd4aa75363f35b2e8d97be2f42" + integrity sha1-AylVJ9WL081Kp1Nj81sujZe+L0I= + caniuse-api@^1.5.2: version "1.6.1" resolved "https://registry.yarnpkg.com/caniuse-api/-/caniuse-api-1.6.1.tgz#b534e7c734c4f81ec5fbe8aca2ad24354b962c6c"