Skip to content
Myr

command.helpOption.test.js

This file

Path

tests/command.helpOption.test.js

117 lines · 3.5 KiB · JavaScript

Last changed by

Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c

History of this file

Take it

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 1–2why, one step back
1const commander = require('../');
2

Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b

lines 3–4why, one step back
3describe('helpOption', () => {
4 let writeSpy;

Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c

line 5why, one step back
5 let consoleErrorSpy;

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

line 6why, one step back
6

Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b

line 7why, one step back
7 beforeAll(() => {

Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c

line 8why, one step back
8 // Optional. Suppress expected output to keep test output clean.

Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b

line 9why, one step back
9 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });

Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c

line 10why, one step back
10 consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });

Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b

lines 11–14why, one step back
11 });
1213 afterEach(() => {14 writeSpy.mockClear();

Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c

line 15why, one step back
15 consoleErrorSpy.mockClear();

Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b

lines 16–19why, one step back
16 });
1718 afterAll(() => {19 writeSpy.mockRestore();

Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c

line 20why, one step back
20 consoleErrorSpy.mockRestore();

Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b

lines 21–68why, one step back
21 });
2223 test('when helpOption has custom flags then custom short flag invokes help', () => {24 const program = new commander.Command();25 program26 .exitOverride()27 .helpOption('-c,--custom-help', 'custom help output');28 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();35 program36 .exitOverride()37 .helpOption('-c,--custom-help', 'custom help output');38 expect(() => {39 program.parse(['--custom-help'], { from: 'user' });40 }).toThrow('(outputHelp)');41 });4243 test('when helpOption has just custom short flag then custom short flag invokes help', () => {44 const program = new commander.Command();45 program46 .exitOverride()47 .helpOption('-c', 'custom help output');48 expect(() => {49 program.parse(['-c'], { from: 'user' });50 }).toThrow('(outputHelp)');51 });5253 test('when helpOption has just custom long flag then custom long flag invokes help', () => {54 const program = new commander.Command();55 program56 .exitOverride()57 .helpOption('--custom-help', 'custom help output');58 expect(() => {59 program.parse(['--custom-help'], { from: 'user' });60 }).toThrow('(outputHelp)');61 });6263 test('when helpOption has custom description then helpInformation include custom description', () => {64 const program = new commander.Command();65 program66 .helpOption('-C,--custom-help', 'custom help output');67 const helpInformation = program.helpInformation();68 expect(helpInformation).toMatch(/-C,--custom-help +custom help output/);

Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c

lines 69–115why, one step back
69 });
7071 test('when helpOption(false) then helpInformation does not include --help', () => {72 const program = new commander.Command();73 program74 .helpOption(false);75 const helpInformation = program.helpInformation();76 expect(helpInformation).not.toMatch('--help');77 });7879 test('when helpOption(false) then --help is an unknown option', () => {80 const program = new commander.Command();81 program82 .exitOverride()83 .helpOption(false);84 let caughtErr;85 try {86 program.parse(['--help'], { from: 'user' });87 } catch (err) {88 caughtErr = err;89 }90 expect(caughtErr.code).toBe('commander.unknownOption');91 });9293 test('when helpOption(false) then -h is an unknown option', () => {94 const program = new commander.Command();95 program96 .exitOverride()97 .helpOption(false);98 let caughtErr;99 try {100 program.parse(['-h'], { from: 'user' });101 } catch (err) {102 caughtErr = err;103 }104 expect(caughtErr.code).toBe('commander.unknownOption');105 });106107 test('when helpOption(false) then unknown command error does not suggest --help', () => {108 const program = new commander.Command();109 program110 .exitOverride()111 .helpOption(false)112 .command('foo');113 expect(() => {114 program.parse(['UNKNOWN'], { from: 'user' });115 }).toThrow("error: unknown command 'UNKNOWN'.");

Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b

line 116why, one step back
116 });

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

line 117why, one step back
117});