command.helpOption.test.js
This file
Path
tests/command.helpOption.test.js
118 lines · 4.1 KiB · JavaScript
Last changed by
Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
History of this fileTake it
The walk stopped after 200 commits: lines older than 78e335f point at it.
Fix minor typo (#1579)John Geebefore adoption78e335f
lines 1–8why, one step back1const commander = require('../');23describe('helpOption', () => {4 let writeSpy;5 let writeErrorSpy;67 beforeAll(() => {8 // Optional. Suppress expected output to keep test output clean.Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 9–12why, one step back9 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => {});10 writeErrorSpy = jest11 .spyOn(process.stderr, 'write')12 .mockImplementation(() => {});Fix minor typo (#1579)John Geebefore adoption78e335f
lines 13–26why, one step back13 });1415 afterEach(() => {16 writeSpy.mockClear();17 writeErrorSpy.mockClear();18 });1920 afterAll(() => {21 writeSpy.mockRestore();22 writeErrorSpy.mockRestore();23 });2425 test('when helpOption has custom flags then custom short flag invokes help', () => {26 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 27why, one step back27 program.exitOverride().helpOption('-c,--custom-help', 'custom help output');Fix minor typo (#1579)John Geebefore adoption78e335f
lines 28–34why, one step back28 expect(() => {29 program.parse(['-c'], { from: 'user' });30 }).toThrow('(outputHelp)');31 });3233 test('when helpOption has custom flags then custom long flag invokes help', () => {34 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 35why, one step back35 program.exitOverride().helpOption('-c,--custom-help', 'custom help output');Fix minor typo (#1579)John Geebefore adoption78e335f
lines 36–42why, one step back36 expect(() => {37 program.parse(['--custom-help'], { from: 'user' });38 }).toThrow('(outputHelp)');39 });4041 test('when helpOption has just custom short flag then custom short flag invokes help', () => {42 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 43why, one step back43 program.exitOverride().helpOption('-c', 'custom help output');Fix minor typo (#1579)John Geebefore adoption78e335f
lines 44–50why, one step back44 expect(() => {45 program.parse(['-c'], { from: 'user' });46 }).toThrow('(outputHelp)');47 });4849 test('when helpOption has just custom long flag then custom long flag invokes help', () => {50 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 51why, one step back51 program.exitOverride().helpOption('--custom-help', 'custom help output');Fix minor typo (#1579)John Geebefore adoption78e335f
lines 52–58why, one step back52 expect(() => {53 program.parse(['--custom-help'], { from: 'user' });54 }).toThrow('(outputHelp)');55 });5657 test('when helpOption has custom description then helpInformation include custom description', () => {58 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 59why, one step back59 program.helpOption('-C,--custom-help', 'custom help output');Fix minor typo (#1579)John Geebefore adoption78e335f
lines 60–65why, one step back60 const helpInformation = program.helpInformation();61 expect(helpInformation).toMatch(/-C,--custom-help +custom help output/);62 });6364 test('when helpOption has just flags then helpInformation includes default description', () => {65 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 66why, one step back66 program.helpOption('-C,--custom-help');Fix minor typo (#1579)John Geebefore adoption78e335f
line 67why, one step back67 const helpInformation = program.helpInformation();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 68–70why, one step back68 expect(helpInformation).toMatch(69 /-C,--custom-help +display help for command/,70 );Fix minor typo (#1579)John Geebefore adoption78e335f
lines 71–74why, one step back71 });7273 test('when helpOption has just description then helpInformation includes default flags', () => {74 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 75why, one step back75 program.helpOption(undefined, 'custom help output');Fix minor typo (#1579)John Geebefore adoption78e335f
lines 76–81why, one step back76 const helpInformation = program.helpInformation();77 expect(helpInformation).toMatch(/-h, --help +custom help output/);78 });7980 test('when helpOption(false) then helpInformation does not include --help', () => {81 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 82why, one step back82 program.helpOption(false);Fix minor typo (#1579)John Geebefore adoption78e335f
lines 83–88why, one step back83 const helpInformation = program.helpInformation();84 expect(helpInformation).not.toMatch('--help');85 });8687 test('when helpOption(false) then --help is an unknown option', () => {88 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 89why, one step back89 program.exitOverride().helpOption(false);Fix minor typo (#1579)John Geebefore adoption78e335f
lines 90–100why, one step back90 let caughtErr;91 try {92 program.parse(['--help'], { from: 'user' });93 } catch (err) {94 caughtErr = err;95 }96 expect(caughtErr.code).toBe('commander.unknownOption');97 });9899 test('when helpOption(false) then -h is an unknown option', () => {100 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 101why, one step back101 program.exitOverride().helpOption(false);Fix minor typo (#1579)John Geebefore adoption78e335f
lines 102–112why, one step back102 let caughtErr;103 try {104 program.parse(['-h'], { from: 'user' });105 } catch (err) {106 caughtErr = err;107 }108 expect(caughtErr.code).toBe('commander.unknownOption');109 });110111 test('when helpOption(false) then unknown command error does not suggest --help', () => {112 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 113why, one step back113 program.exitOverride().helpOption(false).command('foo');Fix minor typo (#1579)John Geebefore adoption78e335f
lines 114–118why, one step back114 expect(() => {115 program.parse(['UNKNOWN'], { from: 'user' });116 }).toThrow("error: unknown command 'UNKNOWN'");117 });118});