Skip to content

Commit

Permalink
use camelcase, filter dashed args on returns config
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Hargrove committed Dec 13, 2018
1 parent 3f45e11 commit 0d1a477
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
13 changes: 13 additions & 0 deletions packages/jest-cli/src/__tests__/cli/args.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
});
});
12 changes: 10 additions & 2 deletions packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -186,12 +187,19 @@ 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('-')) {
result[key] = argv[key];
}
return result;
}, {});
};

const getProjectListFromCLIArgs = (argv, project: ?Path) => {
Expand Down
1 change: 1 addition & 0 deletions packages/jest-validate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 7 additions & 10 deletions packages/jest-validate/src/validateCLIOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 0d1a477

Please sign in to comment.