Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show suggestion only when unrecognized cli param is longer than 1 character #10604

Merged
merged 1 commit into from Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
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
@@ -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
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
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