-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcommand.showSuggestionAfterError.test.js
50 lines (43 loc) · 1.53 KB
/
command.showSuggestionAfterError.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const { Command } = require('../');
function getSuggestion(program, arg) {
let message = '';
program
.exitOverride()
.configureOutput({
writeErr: (str) => { message = str; }
});
try {
program.parse([arg], { from: 'user' });
} catch (err) {
}
const match = message.match(/Did you mean (one of )?(.*)\?/);
return match ? match[2] : null;
}
test('when unknown command and showSuggestionAfterError() then show suggestion', () => {
const program = new Command();
program.showSuggestionAfterError();
program.command('example');
const suggestion = getSuggestion(program, 'exampel');
expect(suggestion).toBe('example');
});
test('when unknown command and showSuggestionAfterError(false) then do not show suggestion', () => {
const program = new Command();
program.showSuggestionAfterError(false);
program.command('example');
const suggestion = getSuggestion(program, 'exampel');
expect(suggestion).toBe(null);
});
test('when unknown option and showSuggestionAfterError() then show suggestion', () => {
const program = new Command();
program.showSuggestionAfterError();
program.option('--example');
const suggestion = getSuggestion(program, '--exampel');
expect(suggestion).toBe('--example');
});
test('when unknown option and showSuggestionAfterError(false) then do not show suggestion', () => {
const program = new Command();
program.showSuggestionAfterError(false);
program.option('--example');
const suggestion = getSuggestion(program, '--exampel');
expect(suggestion).toBe(null);
});