command.addHelpOption.test.js
This file
Path
tests/command.addHelpOption.test.js
54 lines · 1.6 KiB · JavaScript
Last changed by
Refactor help option implementation to hold actual Option (#2006)John Geebefore adoption09244af
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455const { Command, Option } = require('../');
// More complete tests are in command.helpOption.test.js.
describe('addHelpOption', () => { let writeSpy; let writeErrorSpy;
beforeAll(() => { // Optional. Suppress expected output to keep test output clean. writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { }); writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { }); });
afterEach(() => { writeSpy.mockClear(); writeErrorSpy.mockClear(); });
afterAll(() => { writeSpy.mockRestore(); writeErrorSpy.mockRestore(); });
test('when addHelpOption has custom flags then custom short flag invokes help', () => { const program = new Command(); program .exitOverride() .addHelpOption(new Option('-c,--custom-help'));
expect(() => { program.parse(['-c'], { from: 'user' }); }).toThrow('(outputHelp)'); });
test('when addHelpOption has custom flags then custom long flag invokes help', () => { const program = new Command(); program .exitOverride() .addHelpOption(new Option('-c,--custom-help'));
expect(() => { program.parse(['--custom-help'], { from: 'user' }); }).toThrow('(outputHelp)'); });
test('when addHelpOption with hidden help option then help does not include help option', () => { const program = new Command(); program .addHelpOption(new Option('-c,--custom-help', 'help help help').hideHelp()); const helpInfo = program.helpInformation(); expect(helpInfo).not.toMatch(/help/); });});