command.addHelpText.test.js
This file
Path
tests/command.addHelpText.test.js
245 lines · 7.7 KiB · JavaScript
Last changed by
Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2
History of this fileTake it
Rename test files to match method being testedJohn Geebefore adoption5b98fc8
lines 1–79why, one step back1const commander = require('../');23// Using outputHelp to simplify testing.45describe('program calls to addHelpText', () => {6 let writeSpy;78 beforeAll(() => {9 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });10 });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();38 program.addHelpText('before', () => { });39 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 });Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2
lines 80–86why, one step back8081 test('when "silly" position then throw', () => {82 const program = new commander.Command();83 expect(() => {84 program.addHelpText('silly', 'text');85 }).toThrow();86 });Rename test files to match method being testedJohn Geebefore adoption5b98fc8
lines 87–245why, one step back87});8889describe('program and subcommand calls to addHelpText', () => {90 let writeSpy;9192 beforeAll(() => {93 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });94 });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(() => {146 stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });147 stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });148 });149150 afterEach(() => {151 stdoutSpy.mockClear();152 stderrSpy.mockClear();153 });154155 afterAll(() => {156 stdoutSpy.mockRestore();157 stderrSpy.mockRestore();158 });159160 test('when help requested then text is on stdout', () => {161 const program = new commander.Command();162 program163 .exitOverride()164 .addHelpText('before', 'text');165 expect(() => {166 program.parse(['--help'], { from: 'user' });167 }).toThrow();168 expect(stdoutSpy).toHaveBeenCalledWith('text\n');169 });170171 test('when help for error then text is on stderr', () => {172 const program = new commander.Command();173 program174 .exitOverride()175 .addHelpText('before', 'text')176 .command('sub');177 expect(() => {178 program.parse([], { from: 'user' });179 }).toThrow();180 expect(stderrSpy).toHaveBeenCalledWith('text\n');181 });182183 test('when help requested then context.error is false', () => {184 let context = {};185 const program = new commander.Command();186 program187 .exitOverride()188 .addHelpText('before', (contextParam) => { context = contextParam; });189 expect(() => {190 program.parse(['--help'], { from: 'user' });191 }).toThrow();192 expect(context.error).toBe(false);193 });194195 test('when help for error then context.error is true', () => {196 let context = {};197 const program = new commander.Command();198 program199 .exitOverride()200 .addHelpText('before', (contextParam) => { context = contextParam; })201 .command('sub');202 expect(() => {203 program.parse([], { from: 'user' });204 }).toThrow();205 expect(context.error).toBe(true);206 });207208 test('when help on program then context.command is program', () => {209 let context = {};210 const program = new commander.Command();211 program212 .exitOverride()213 .addHelpText('before', (contextParam) => { context = contextParam; });214 expect(() => {215 program.parse(['--help'], { from: 'user' });216 }).toThrow();217 expect(context.command).toBe(program);218 });219220 test('when help on subcommand and "before" subcommand then context.command is subcommand', () => {221 let context = {};222 const program = new commander.Command();223 program224 .exitOverride();225 const sub = program.command('sub')226 .addHelpText('before', (contextParam) => { context = contextParam; });227 expect(() => {228 program.parse(['sub', '--help'], { from: 'user' });229 }).toThrow();230 expect(context.command).toBe(sub);231 });232233 test('when help on subcommand and "beforeAll" on program then context.command is subcommand', () => {234 let context = {};235 const program = new commander.Command();236 program237 .exitOverride()238 .addHelpText('beforeAll', (contextParam) => { context = contextParam; });239 const sub = program.command('sub');240 expect(() => {241 program.parse(['sub', '--help'], { from: 'user' });242 }).toThrow();243 expect(context.command).toBe(sub);244 });245});