Skip to content

Commit 285e462

Browse files
authored
Show suggestion only when unrecognized cli param is longer than 1 character (#10604)
1 parent 5da90b5 commit 285e462

4 files changed

Lines changed: 48 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Fixes
66

7+
- `[jest-validate]` Show suggestion only when unrecognized cli param is longer than 1 character ([#10604](https://github.com/facebook/jest/pull/10604))
8+
79
### Chore & Maintenance
810

911
### Performance

packages/jest-validate/src/__tests__/__snapshots__/validateCLIOptions.test.js.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`does not show suggestion when unrecognized cli param length <= 1 1`] = `
4+
"<red><bold><bold>●</><bold> Unrecognized CLI Parameter</>:</>
5+
<red></>
6+
<red> Unrecognized option <bold>\\"l\\"</>.</>
7+
<red></>
8+
<red> <bold>CLI Options Documentation</>:</>
9+
<red> https://jestjs.io/docs/en/cli.html</>
10+
<red></>"
11+
`;
12+
313
exports[`fails for multiple unknown options 1`] = `
414
"<red><bold><bold>●</><bold> Unrecognized CLI Parameters</>:</>
515
<red></>
@@ -20,3 +30,13 @@ exports[`fails for unknown option 1`] = `
2030
<red> https://jestjs.io/docs/en/cli.html</>
2131
<red></>"
2232
`;
33+
34+
exports[`shows suggestion when unrecognized cli param length > 1 1`] = `
35+
"<red><bold><bold>●</><bold> Unrecognized CLI Parameter</>:</>
36+
<red></>
37+
<red> Unrecognized option <bold>\\"hell\\"</>. Did you mean <bold>\\"help\\"</>?</>
38+
<red></>
39+
<red> <bold>CLI Options Documentation</>:</>
40+
<red> https://jestjs.io/docs/en/cli.html</>
41+
<red></>"
42+
`;

packages/jest-validate/src/__tests__/validateCLIOptions.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,25 @@ test('fails for multiple unknown options', () => {
4141
validateCLIOptions(argv, options),
4242
).toThrowErrorMatchingSnapshot();
4343
});
44+
45+
test('does not show suggestion when unrecognized cli param length <= 1', () => {
46+
const options = ['$0', '_', 'help', 'h'];
47+
const argv = {
48+
$0: true,
49+
l: true,
50+
};
51+
expect(() =>
52+
validateCLIOptions(argv, options),
53+
).toThrowErrorMatchingSnapshot();
54+
});
55+
56+
test('shows suggestion when unrecognized cli param length > 1', () => {
57+
const options = ['$0', '_', 'help', 'h'];
58+
const argv = {
59+
$0: true,
60+
hell: true,
61+
};
62+
expect(() =>
63+
validateCLIOptions(argv, options),
64+
).toThrowErrorMatchingSnapshot();
65+
});

packages/jest-validate/src/validateCLIOptions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ const createCLIValidationError = (
3131

3232
if (unrecognizedOptions.length === 1) {
3333
const unrecognized = unrecognizedOptions[0];
34-
const didYouMeanMessage = createDidYouMeanMessage(
35-
unrecognized,
36-
Array.from(allowedOptions),
37-
);
34+
const didYouMeanMessage =
35+
unrecognized.length > 1
36+
? createDidYouMeanMessage(unrecognized, Array.from(allowedOptions))
37+
: '';
3838
message =
3939
` Unrecognized option ${chalk.bold(format(unrecognized))}.` +
4040
(didYouMeanMessage ? ` ${didYouMeanMessage}` : '');

0 commit comments

Comments
 (0)