command.showSuggestionAfterError.test.js
This file
Path
tests/command.showSuggestionAfterError.test.js
50 lines · 1.5 KiB · JavaScript
Last changed by
Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
History of this fileTake it
Suggestion for unknown command and unknown option (#1590)John Geebefore adoption91ccfd5
lines 1–17why, one step back1const { Command } = require('../');23function getSuggestion(program, arg) {4 let message = '';5 program6 .exitOverride()7 .configureOutput({8 writeErr: (str) => { message = str; }9 });1011 try {12 program.parse([arg], { from: 'user' });13 } catch (err) {14 }1516 const match = message.match(/Did you mean (one of )?(.*)\?/);17 return match ? match[2] : null;Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 18why, one step back18}Suggestion for unknown command and unknown option (#1590)John Geebefore adoption91ccfd5
lines 19–50why, one step back1920test('when unknown command and showSuggestionAfterError() then show suggestion', () => {21 const program = new Command();22 program.showSuggestionAfterError();23 program.command('example');24 const suggestion = getSuggestion(program, 'exampel');25 expect(suggestion).toBe('example');26});2728test('when unknown command and showSuggestionAfterError(false) then do not show suggestion', () => {29 const program = new Command();30 program.showSuggestionAfterError(false);31 program.command('example');32 const suggestion = getSuggestion(program, 'exampel');33 expect(suggestion).toBe(null);34});3536test('when unknown option and showSuggestionAfterError() then show suggestion', () => {37 const program = new Command();38 program.showSuggestionAfterError();39 program.option('--example');40 const suggestion = getSuggestion(program, '--exampel');41 expect(suggestion).toBe('--example');42});4344test('when unknown option and showSuggestionAfterError(false) then do not show suggestion', () => {45 const program = new Command();46 program.showSuggestionAfterError(false);47 program.option('--example');48 const suggestion = getSuggestion(program, '--exampel');49 expect(suggestion).toBe(null);50});