command.addHelpText.test.js
This file
Path
tests/command.addHelpText.test.js
246 lines · 7.7 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 cc2db5b point at it.
Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 1–8why, one step back1const commander = require('../');23// Using outputHelp to simplify testing.45describe('program calls to addHelpText', () => {6 let writeSpy;78 beforeAll(() => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 9why, one step back9 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => {});Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 10–37why, one step back10 });1112 afterEach(() => {13 writeSpy.mockClear();14 });1516 afterAll(() => {17 writeSpy.mockRestore();18 });1920 test('when "before" string then string before built-in help', () => {21 const program = new commander.Command();22 program.addHelpText('before', 'text');23 program.outputHelp();24 expect(writeSpy).toHaveBeenNthCalledWith(1, 'text\n');25 expect(writeSpy).toHaveBeenNthCalledWith(2, program.helpInformation());26 });2728 test('when "before" function then function result before built-in help', () => {29 const program = new commander.Command();30 program.addHelpText('before', () => 'text');31 program.outputHelp();32 expect(writeSpy).toHaveBeenNthCalledWith(1, 'text\n');33 expect(writeSpy).toHaveBeenNthCalledWith(2, program.helpInformation());34 });3536 test('when "before" function returns nothing then no effect', () => {37 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 38why, one step back38 program.addHelpText('before', () => {});Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 39–92why, one step back39 program.outputHelp();40 expect(writeSpy).toHaveBeenNthCalledWith(1, program.helpInformation());41 });4243 test('when "beforeAll" string then string before built-in help', () => {44 const program = new commander.Command();45 program.addHelpText('beforeAll', 'text');46 program.outputHelp();47 expect(writeSpy).toHaveBeenNthCalledWith(1, 'text\n');48 expect(writeSpy).toHaveBeenNthCalledWith(2, program.helpInformation());49 });5051 test('when "after" string then string after built-in help', () => {52 const program = new commander.Command();53 program.addHelpText('after', 'text');54 program.outputHelp();55 expect(writeSpy).toHaveBeenNthCalledWith(1, program.helpInformation());56 expect(writeSpy).toHaveBeenNthCalledWith(2, 'text\n');57 });5859 test('when "afterAll" string then string after built-in help', () => {60 const program = new commander.Command();61 program.addHelpText('afterAll', 'text');62 program.outputHelp();63 expect(writeSpy).toHaveBeenNthCalledWith(1, program.helpInformation());64 expect(writeSpy).toHaveBeenNthCalledWith(2, 'text\n');65 });6667 test('when all the simple positions then strings in order', () => {68 const program = new commander.Command();69 program.addHelpText('before', 'before');70 program.addHelpText('after', 'after');71 program.addHelpText('beforeAll', 'beforeAll');72 program.addHelpText('afterAll', 'afterAll');73 program.outputHelp();74 expect(writeSpy).toHaveBeenNthCalledWith(1, 'beforeAll\n');75 expect(writeSpy).toHaveBeenNthCalledWith(2, 'before\n');76 expect(writeSpy).toHaveBeenNthCalledWith(3, program.helpInformation());77 expect(writeSpy).toHaveBeenNthCalledWith(4, 'after\n');78 expect(writeSpy).toHaveBeenNthCalledWith(5, 'afterAll\n');79 });8081 test('when "silly" position then throw', () => {82 const program = new commander.Command();83 expect(() => {84 program.addHelpText('silly', 'text');85 }).toThrow();86 });87});8889describe('program and subcommand calls to addHelpText', () => {90 let writeSpy;9192 beforeAll(() => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 93why, one step back93 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => {});Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 94–145why, one step back94 });9596 afterEach(() => {97 writeSpy.mockClear();98 });99100 afterAll(() => {101 writeSpy.mockRestore();102 });103104 test('when "before" on program then not called on subcommand', () => {105 const program = new commander.Command();106 const sub = program.command('sub');107 const testMock = jest.fn();108 program.addHelpText('before', testMock);109 sub.outputHelp();110 expect(testMock).not.toHaveBeenCalled();111 });112113 test('when "beforeAll" on program then is called on subcommand', () => {114 const program = new commander.Command();115 const sub = program.command('sub');116 const testMock = jest.fn();117 program.addHelpText('beforeAll', testMock);118 sub.outputHelp();119 expect(testMock).toHaveBeenCalled();120 });121122 test('when "after" on program then not called on subcommand', () => {123 const program = new commander.Command();124 const sub = program.command('sub');125 const testMock = jest.fn();126 program.addHelpText('after', testMock);127 sub.outputHelp();128 expect(testMock).not.toHaveBeenCalled();129 });130131 test('when "afterAll" on program then is called on subcommand', () => {132 const program = new commander.Command();133 const sub = program.command('sub');134 const testMock = jest.fn();135 program.addHelpText('afterAll', testMock);136 sub.outputHelp();137 expect(testMock).toHaveBeenCalled();138 });139});140141describe('context checks with full parse', () => {142 let stdoutSpy;143 let stderrSpy;144145 beforeAll(() => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 146–151why, one step back146 stdoutSpy = jest147 .spyOn(process.stdout, 'write')148 .mockImplementation(() => {});149 stderrSpy = jest150 .spyOn(process.stderr, 'write')151 .mockImplementation(() => {});Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 152–165why, one step back152 });153154 afterEach(() => {155 stdoutSpy.mockClear();156 stderrSpy.mockClear();157 });158159 afterAll(() => {160 stdoutSpy.mockRestore();161 stderrSpy.mockRestore();162 });163164 test('when help requested then text is on stdout', () => {165 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 166why, one step back166 program.exitOverride().addHelpText('before', 'text');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 167–174why, one step back167 expect(() => {168 program.parse(['--help'], { from: 'user' });169 }).toThrow();170 expect(stdoutSpy).toHaveBeenCalledWith('text\n');171 });172173 test('when help for error then text is on stderr', () => {174 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 175why, one step back175 program.exitOverride().addHelpText('before', 'text').command('sub');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 176–182why, one step back176 expect(() => {177 program.parse([], { from: 'user' });178 }).toThrow();179 expect(stderrSpy).toHaveBeenCalledWith('text\n');180 });181182 test('when help requested then context.error is false', () => {Change initial variable values in test for better error messagesWee Bitbefore adoption401b0d6
line 183why, one step back183 let context;Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 184why, one step back184 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 185–187why, one step back185 program.exitOverride().addHelpText('before', (contextParam) => {186 context = contextParam;187 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 188–194why, one step back188 expect(() => {189 program.parse(['--help'], { from: 'user' });190 }).toThrow();191 expect(context.error).toBe(false);192 });193194 test('when help for error then context.error is true', () => {Change initial variable values in test for better error messagesWee Bitbefore adoption401b0d6
line 195why, one step back195 let context;Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 196–198why, one step back196 const program = new commander.Command();197 program198 .exitOverride()Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 199–201why, one step back199 .addHelpText('before', (contextParam) => {200 context = contextParam;201 })Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 202–209why, one step back202 .command('sub');203 expect(() => {204 program.parse([], { from: 'user' });205 }).toThrow();206 expect(context.error).toBe(true);207 });208209 test('when help on program then context.command is program', () => {Change initial variable values in test for better error messagesWee Bitbefore adoption401b0d6
line 210why, one step back210 let context;Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 211why, one step back211 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 212–214why, one step back212 program.exitOverride().addHelpText('before', (contextParam) => {213 context = contextParam;214 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 215–221why, one step back215 expect(() => {216 program.parse(['--help'], { from: 'user' });217 }).toThrow();218 expect(context.command).toBe(program);219 });220221 test('when help on subcommand and "before" subcommand then context.command is subcommand', () => {Change initial variable values in test for better error messagesWee Bitbefore adoption401b0d6
line 222why, one step back222 let context;Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 223why, one step back223 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 224–227why, one step back224 program.exitOverride();225 const sub = program.command('sub').addHelpText('before', (contextParam) => {226 context = contextParam;227 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 228–234why, one step back228 expect(() => {229 program.parse(['sub', '--help'], { from: 'user' });230 }).toThrow();231 expect(context.command).toBe(sub);232 });233234 test('when help on subcommand and "beforeAll" on program then context.command is subcommand', () => {Change initial variable values in test for better error messagesWee Bitbefore adoption401b0d6
line 235why, one step back235 let context;Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 236why, one step back236 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 237–239why, one step back237 program.exitOverride().addHelpText('beforeAll', (contextParam) => {238 context = contextParam;239 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 240–246why, one step back240 const sub = program.command('sub');241 expect(() => {242 program.parse(['sub', '--help'], { from: 'user' });243 }).toThrow();244 expect(context.command).toBe(sub);245 });246});