Skip to content

Commit

Permalink
Show suggestion only when unrecognized cli param is longer than 1 cha…
Browse files Browse the repository at this point in the history
…racter (#10604)
  • Loading branch information
jcs98 authored Oct 7, 2020
1 parent 5da90b5 commit 285e462
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-validate]` Show suggestion only when unrecognized cli param is longer than 1 character ([#10604](https://github.com/facebook/jest/pull/10604))

### Chore & Maintenance

### Performance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`does not show suggestion when unrecognized cli param length <= 1 1`] = `
"<red><bold><bold>●</><bold> Unrecognized CLI Parameter</>:</>
<red></>
<red> Unrecognized option <bold>\\"l\\"</>.</>
<red></>
<red> <bold>CLI Options Documentation</>:</>
<red> https://jestjs.io/docs/en/cli.html</>
<red></>"
`;
exports[`fails for multiple unknown options 1`] = `
"<red><bold><bold>●</><bold> Unrecognized CLI Parameters</>:</>
<red></>
Expand All @@ -20,3 +30,13 @@ exports[`fails for unknown option 1`] = `
<red> https://jestjs.io/docs/en/cli.html</>
<red></>"
`;
exports[`shows suggestion when unrecognized cli param length > 1 1`] = `
"<red><bold><bold>●</><bold> Unrecognized CLI Parameter</>:</>
<red></>
<red> Unrecognized option <bold>\\"hell\\"</>. Did you mean <bold>\\"help\\"</>?</>
<red></>
<red> <bold>CLI Options Documentation</>:</>
<red> https://jestjs.io/docs/en/cli.html</>
<red></>"
`;
22 changes: 22 additions & 0 deletions packages/jest-validate/src/__tests__/validateCLIOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,25 @@ test('fails for multiple unknown options', () => {
validateCLIOptions(argv, options),
).toThrowErrorMatchingSnapshot();
});

test('does not show suggestion when unrecognized cli param length <= 1', () => {
const options = ['$0', '_', 'help', 'h'];
const argv = {
$0: true,
l: true,
};
expect(() =>
validateCLIOptions(argv, options),
).toThrowErrorMatchingSnapshot();
});

test('shows suggestion when unrecognized cli param length > 1', () => {
const options = ['$0', '_', 'help', 'h'];
const argv = {
$0: true,
hell: true,
};
expect(() =>
validateCLIOptions(argv, options),
).toThrowErrorMatchingSnapshot();
});
8 changes: 4 additions & 4 deletions packages/jest-validate/src/validateCLIOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ const createCLIValidationError = (

if (unrecognizedOptions.length === 1) {
const unrecognized = unrecognizedOptions[0];
const didYouMeanMessage = createDidYouMeanMessage(
unrecognized,
Array.from(allowedOptions),
);
const didYouMeanMessage =
unrecognized.length > 1
? createDidYouMeanMessage(unrecognized, Array.from(allowedOptions))
: '';
message =
` Unrecognized option ${chalk.bold(format(unrecognized))}.` +
(didYouMeanMessage ? ` ${didYouMeanMessage}` : '');
Expand Down

0 comments on commit 285e462

Please sign in to comment.