Skip to content
Myr

Rename test files to match method being tested

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by John Gee · committed by John Gee · parent df5d679 · Browse files · 4 files+332 added−332 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+332 added−332 removed

  • tests:+332 added−332 removed

4 files+332 added−332 removed

deleted:tests/command.addHelp.test.js-238 removed
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});
added:tests/command.addHelpText.test.js+238 added
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});
added:tests/command.allowUnknownOption.test.js+94 added
1const commander = require('../');23// Not testing output, just testing whether an error is detected.45describe('allowUnknownOption', () => {6 // Optional. Use internal knowledge to suppress output to keep test output clean.7 let consoleErrorSpy;89 beforeAll(() => {10 consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });11 });1213 afterEach(() => {14 consoleErrorSpy.mockClear();15 });1617 afterAll(() => {18 consoleErrorSpy.mockRestore();19 });2021 test('when specify unknown program option then error', () => {22 const program = new commander.Command();23 program24 .exitOverride()25 .option('-p, --pepper', 'add pepper');2627 expect(() => {28 program.parse(['node', 'test', '-m']);29 }).toThrow();30 });3132 test('when specify unknown program option and allowUnknownOption(false) then error', () => {33 const program = new commander.Command();34 program35 .exitOverride()36 .allowUnknownOption(false)37 .option('-p, --pepper', 'add pepper');3839 expect(() => {40 program.parse(['node', 'test', '-m']);41 }).toThrow();42 });4344 test('when specify unknown program option and allowUnknownOption() then no error', () => {45 const program = new commander.Command();46 program47 .exitOverride()48 .allowUnknownOption()49 .option('-p, --pepper', 'add pepper');5051 expect(() => {52 program.parse(['node', 'test', '-m']);53 }).not.toThrow();54 });5556 test('when specify unknown program option and allowUnknownOption(true) then no error', () => {57 const program = new commander.Command();58 program59 .exitOverride()60 .allowUnknownOption(true)61 .option('-p, --pepper', 'add pepper');6263 expect(() => {64 program.parse(['node', 'test', '-m']);65 }).not.toThrow();66 });6768 test('when specify unknown command option then error', () => {69 const program = new commander.Command();70 program71 .exitOverride()72 .command('sub')73 .option('-p, --pepper', 'add pepper')74 .action(() => { });7576 expect(() => {77 program.parse(['node', 'test', 'sub', '-m']);78 }).toThrow();79 });8081 test('when specify unknown command option and allowUnknownOption then no error', () => {82 const program = new commander.Command();83 program84 .exitOverride()85 .command('sub')86 .allowUnknownOption()87 .option('-p, --pepper', 'add pepper')88 .action(() => { });8990 expect(() => {91 program.parse(['node', 'test', 'sub', '-m']);92 }).not.toThrow();93 });94});
deleted:tests/command.allowUnknownOptions.test.js-94 removed
1const commander = require('../');23// Not testing output, just testing whether an error is detected.45describe('allowUnknownOption', () => {6 // Optional. Use internal knowledge to suppress output to keep test output clean.7 let consoleErrorSpy;89 beforeAll(() => {10 consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });11 });1213 afterEach(() => {14 consoleErrorSpy.mockClear();15 });1617 afterAll(() => {18 consoleErrorSpy.mockRestore();19 });2021 test('when specify unknown program option then error', () => {22 const program = new commander.Command();23 program24 .exitOverride()25 .option('-p, --pepper', 'add pepper');2627 expect(() => {28 program.parse(['node', 'test', '-m']);29 }).toThrow();30 });3132 test('when specify unknown program option and allowUnknownOption(false) then error', () => {33 const program = new commander.Command();34 program35 .exitOverride()36 .allowUnknownOption(false)37 .option('-p, --pepper', 'add pepper');3839 expect(() => {40 program.parse(['node', 'test', '-m']);41 }).toThrow();42 });4344 test('when specify unknown program option and allowUnknownOption() then no error', () => {45 const program = new commander.Command();46 program47 .exitOverride()48 .allowUnknownOption()49 .option('-p, --pepper', 'add pepper');5051 expect(() => {52 program.parse(['node', 'test', '-m']);53 }).not.toThrow();54 });5556 test('when specify unknown program option and allowUnknownOption(true) then no error', () => {57 const program = new commander.Command();58 program59 .exitOverride()60 .allowUnknownOption(true)61 .option('-p, --pepper', 'add pepper');6263 expect(() => {64 program.parse(['node', 'test', '-m']);65 }).not.toThrow();66 });6768 test('when specify unknown command option then error', () => {69 const program = new commander.Command();70 program71 .exitOverride()72 .command('sub')73 .option('-p, --pepper', 'add pepper')74 .action(() => { });7576 expect(() => {77 program.parse(['node', 'test', 'sub', '-m']);78 }).toThrow();79 });8081 test('when specify unknown command option and allowUnknownOption then no error', () => {82 const program = new commander.Command();83 program84 .exitOverride()85 .command('sub')86 .allowUnknownOption()87 .option('-p, --pepper', 'add pepper')88 .action(() => { });8990 expect(() => {91 program.parse(['node', 'test', 'sub', '-m']);92 }).not.toThrow();93 });94});