Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bad4999

Browse files
author
Steven Hargrove
committedDec 13, 2018
use camelcase, filter dashed args on returns config
1 parent 3f45e11 commit bad4999

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed
 

‎packages/jest-cli/src/__tests__/cli/args.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import type {Argv} from 'types/Argv';
1212
import {check} from '../../cli/args';
13+
import {buildArgv} from '../../cli';
1314

1415
describe('check', () => {
1516
it('returns true if the arguments are valid', () => {
@@ -59,3 +60,15 @@ describe('check', () => {
5960
);
6061
});
6162
});
63+
64+
describe('buildArgv', () => {
65+
it('should return only camelcased args ', () => {
66+
const mockProcessArgv = jest
67+
.spyOn(process.argv, 'slice')
68+
.mockImplementation(() => ['--clear-mocks']);
69+
const actual = buildArgv(null);
70+
expect(actual).not.toHaveProperty('clear-mocks');
71+
expect(actual).toHaveProperty('clearMocks', true);
72+
mockProcessArgv.mockRestore();
73+
});
74+
});

‎packages/jest-cli/src/cli/index.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import logDebugMessages from '../lib/log_debug_messages';
3535

3636
export async function run(maybeArgv?: Argv, project?: Path) {
3737
try {
38+
// $FlowFixMe:`allow reduced return
3839
const argv: Argv = buildArgv(maybeArgv, project);
3940

4041
if (argv.init) {
@@ -174,7 +175,7 @@ const readResultsAndExit = (
174175
}
175176
};
176177

177-
const buildArgv = (maybeArgv: ?Argv, project: ?Path) => {
178+
export const buildArgv = (maybeArgv: ?Argv, project: ?Path) => {
178179
const rawArgv: Argv | string[] = maybeArgv || process.argv.slice(2);
179180
const argv: Argv = yargs(rawArgv)
180181
.usage(args.usage)
@@ -186,12 +187,20 @@ const buildArgv = (maybeArgv: ?Argv, project: ?Path) => {
186187
validateCLIOptions(
187188
argv,
188189
Object.assign({}, args.options, {deprecationEntries}),
190+
// strip leading dashes
189191
Array.isArray(rawArgv)
190192
? rawArgv.map(rawArgv => rawArgv.replace(/^--?/, ''))
191193
: Object.keys(rawArgv),
192194
);
193195

194-
return argv;
196+
// strip dashed args
197+
return Object.keys(argv).reduce((result, key) => {
198+
if (!key.includes('-')) {
199+
// $FlowFixMe:`allow reduced return
200+
result[key] = argv[key];
201+
}
202+
return result;
203+
}, {});
195204
};
196205

197206
const getProjectListFromCLIArgs = (argv, project: ?Path) => {

‎packages/jest-validate/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"license": "MIT",
99
"main": "build/index.js",
1010
"dependencies": {
11+
"camelcase": "^5.0.0",
1112
"chalk": "^2.0.1",
1213
"jest-get-type": "^22.1.0",
1314
"leven": "^2.1.0",

‎packages/jest-validate/src/validateCLIOptions.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import type {Argv} from 'types/Argv';
1111

1212
import chalk from 'chalk';
13+
import camelcase from 'camelcase';
1314
import {createDidYouMeanMessage, format, ValidationError} from './utils';
1415
import {deprecationWarning} from './deprecated';
1516
import defaultConfig from './defaultConfig';
@@ -76,16 +77,12 @@ export default function validateCLIOptions(
7677
(acc, option) => acc.add(option).add(options[option].alias || option),
7778
new Set(yargsSpecialOptions),
7879
);
79-
const unrecognizedOptions = Object.keys(argv).filter(arg => {
80-
const camelCased = arg.replace(/-([^-])/g, (a, b) => b.toUpperCase());
81-
if (
82-
!allowedOptions.has(camelCased) &&
83-
(!rawArgv.length || rawArgv.includes(arg))
84-
) {
85-
return true;
86-
}
87-
return false;
88-
}, []);
80+
const unrecognizedOptions = Object.keys(argv).filter(
81+
arg =>
82+
!allowedOptions.has(camelcase(arg)) &&
83+
(!rawArgv.length || rawArgv.includes(arg)),
84+
[],
85+
);
8986

9087
if (unrecognizedOptions.length) {
9188
throw createCLIValidationError(unrecognizedOptions, allowedOptions);

‎yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -3437,6 +3437,11 @@ camelcase@^4.1.0:
34373437
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd"
34383438
integrity sha1-1UVjW+HjPFQmScaRc+Xeas+uNN0=
34393439

3440+
camelcase@^5.0.0:
3441+
version "5.0.0"
3442+
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.0.0.tgz#03295527d58bd3cd4aa75363f35b2e8d97be2f42"
3443+
integrity sha512-faqwZqnWxbxn+F1d399ygeamQNy3lPp/H9H6rNrqYh4FSVCtcY+3cub1MxA8o9mDd55mM8Aghuu/kuyYA6VTsA==
3444+
34403445
caniuse-api@^1.5.2:
34413446
version "1.6.1"
34423447
resolved "https://registry.yarnpkg.com/caniuse-api/-/caniuse-api-1.6.1.tgz#b534e7c734c4f81ec5fbe8aca2ad24354b962c6c"

0 commit comments

Comments
 (0)
Please sign in to comment.