Skip to content
Myr

command.addHelpText.test.js

This file

Path

tests/command.addHelpText.test.js

238 lines · 7.5 KiB · JavaScript

Last changed by

Rename test files to match method being testedJohn Geebefore adoption5b98fc8

History of this file

Take it

Rename test files to match method being testedJohn Geebefore adoption5b98fc8

lines 1–238why, one step back
1const 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 });80});8182describe('program and subcommand calls to addHelpText', () => {83 let writeSpy;8485 beforeAll(() => {86 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });87 });8889 afterEach(() => {90 writeSpy.mockClear();91 });9293 afterAll(() => {94 writeSpy.mockRestore();95 });9697 test('when "before" on program then not called on subcommand', () => {98 const program = new commander.Command();99 const sub = program.command('sub');100 const testMock = jest.fn();101 program.addHelpText('before', testMock);102 sub.outputHelp();103 expect(testMock).not.toHaveBeenCalled();104 });105106 test('when "beforeAll" on program then is called on subcommand', () => {107 const program = new commander.Command();108 const sub = program.command('sub');109 const testMock = jest.fn();110 program.addHelpText('beforeAll', testMock);111 sub.outputHelp();112 expect(testMock).toHaveBeenCalled();113 });114115 test('when "after" on program then not called on subcommand', () => {116 const program = new commander.Command();117 const sub = program.command('sub');118 const testMock = jest.fn();119 program.addHelpText('after', testMock);120 sub.outputHelp();121 expect(testMock).not.toHaveBeenCalled();122 });123124 test('when "afterAll" on program then is called on subcommand', () => {125 const program = new commander.Command();126 const sub = program.command('sub');127 const testMock = jest.fn();128 program.addHelpText('afterAll', testMock);129 sub.outputHelp();130 expect(testMock).toHaveBeenCalled();131 });132});133134describe('context checks with full parse', () => {135 let stdoutSpy;136 let stderrSpy;137138 beforeAll(() => {139 stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });140 stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });141 });142143 afterEach(() => {144 stdoutSpy.mockClear();145 stderrSpy.mockClear();146 });147148 afterAll(() => {149 stdoutSpy.mockRestore();150 stderrSpy.mockRestore();151 });152153 test('when help requested then text is on stdout', () => {154 const program = new commander.Command();155 program156 .exitOverride()157 .addHelpText('before', 'text');158 expect(() => {159 program.parse(['--help'], { from: 'user' });160 }).toThrow();161 expect(stdoutSpy).toHaveBeenCalledWith('text\n');162 });163164 test('when help for error then text is on stderr', () => {165 const program = new commander.Command();166 program167 .exitOverride()168 .addHelpText('before', 'text')169 .command('sub');170 expect(() => {171 program.parse([], { from: 'user' });172 }).toThrow();173 expect(stderrSpy).toHaveBeenCalledWith('text\n');174 });175176 test('when help requested then context.error is false', () => {177 let context = {};178 const program = new commander.Command();179 program180 .exitOverride()181 .addHelpText('before', (contextParam) => { context = contextParam; });182 expect(() => {183 program.parse(['--help'], { from: 'user' });184 }).toThrow();185 expect(context.error).toBe(false);186 });187188 test('when help for error then context.error is true', () => {189 let context = {};190 const program = new commander.Command();191 program192 .exitOverride()193 .addHelpText('before', (contextParam) => { context = contextParam; })194 .command('sub');195 expect(() => {196 program.parse([], { from: 'user' });197 }).toThrow();198 expect(context.error).toBe(true);199 });200201 test('when help on program then context.command is program', () => {202 let context = {};203 const program = new commander.Command();204 program205 .exitOverride()206 .addHelpText('before', (contextParam) => { context = contextParam; });207 expect(() => {208 program.parse(['--help'], { from: 'user' });209 }).toThrow();210 expect(context.command).toBe(program);211 });212213 test('when help on subcommand and "before" subcommand then context.command is subcommand', () => {214 let context = {};215 const program = new commander.Command();216 program217 .exitOverride();218 const sub = program.command('sub')219 .addHelpText('before', (contextParam) => { context = contextParam; });220 expect(() => {221 program.parse(['sub', '--help'], { from: 'user' });222 }).toThrow();223 expect(context.command).toBe(sub);224 });225226 test('when help on subcommand and "beforeAll" on program then context.command is subcommand', () => {227 let context = {};228 const program = new commander.Command();229 program230 .exitOverride()231 .addHelpText('beforeAll', (contextParam) => { context = contextParam; });232 const sub = program.command('sub');233 expect(() => {234 program.parse(['sub', '--help'], { from: 'user' });235 }).toThrow();236 expect(context.command).toBe(sub);237 });238});