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
Provenance
- Commit
5b98fc8- Tree
bcdca2d- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent
df5d679
4 files changed+332 added−332 removed
deleted:tests/command.addHelp.test.js-238 removed
1-const commander = require('../');2-3-// Using outputHelp to simplify testing.4-5-describe('program calls to addHelpText', () => {6- let writeSpy;7-8- beforeAll(() => {9- writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });10- });11-12- afterEach(() => {13- writeSpy.mockClear();14- });15-16- afterAll(() => {17- writeSpy.mockRestore();18- });19-20- 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- });27-28- 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- });35-36- 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- });42-43- 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- });50-51- 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- });58-59- 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- });66-67- 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-});81-82-describe('program and subcommand calls to addHelpText', () => {83- let writeSpy;84-85- beforeAll(() => {86- writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });87- });88-89- afterEach(() => {90- writeSpy.mockClear();91- });92-93- afterAll(() => {94- writeSpy.mockRestore();95- });96-97- 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- });105-106- 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- });114-115- 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- });123-124- 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-});133-134-describe('context checks with full parse', () => {135- let stdoutSpy;136- let stderrSpy;137-138- beforeAll(() => {139- stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });140- stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });141- });142-143- afterEach(() => {144- stdoutSpy.mockClear();145- stderrSpy.mockClear();146- });147-148- afterAll(() => {149- stdoutSpy.mockRestore();150- stderrSpy.mockRestore();151- });152-153- 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- });163-164- 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- });175-176- 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- });187-188- 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- });200-201- 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- });212-213- 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- });225-226- 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
1+const commander = require('../');2+3+// Using outputHelp to simplify testing.4+5+describe('program calls to addHelpText', () => {6+ let writeSpy;7+8+ beforeAll(() => {9+ writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });10+ });11+12+ afterEach(() => {13+ writeSpy.mockClear();14+ });15+16+ afterAll(() => {17+ writeSpy.mockRestore();18+ });19+20+ 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+ });27+28+ 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+ });35+36+ 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+ });42+43+ 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+ });50+51+ 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+ });58+59+ 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+ });66+67+ 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+});81+82+describe('program and subcommand calls to addHelpText', () => {83+ let writeSpy;84+85+ beforeAll(() => {86+ writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });87+ });88+89+ afterEach(() => {90+ writeSpy.mockClear();91+ });92+93+ afterAll(() => {94+ writeSpy.mockRestore();95+ });96+97+ 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+ });105+106+ 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+ });114+115+ 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+ });123+124+ 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+});133+134+describe('context checks with full parse', () => {135+ let stdoutSpy;136+ let stderrSpy;137+138+ beforeAll(() => {139+ stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });140+ stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });141+ });142+143+ afterEach(() => {144+ stdoutSpy.mockClear();145+ stderrSpy.mockClear();146+ });147+148+ afterAll(() => {149+ stdoutSpy.mockRestore();150+ stderrSpy.mockRestore();151+ });152+153+ 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+ });163+164+ 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+ });175+176+ 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+ });187+188+ 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+ });200+201+ 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+ });212+213+ 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+ });225+226+ 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
1+const commander = require('../');2+3+// Not testing output, just testing whether an error is detected.4+5+describe('allowUnknownOption', () => {6+ // Optional. Use internal knowledge to suppress output to keep test output clean.7+ let consoleErrorSpy;8+9+ beforeAll(() => {10+ consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });11+ });12+13+ afterEach(() => {14+ consoleErrorSpy.mockClear();15+ });16+17+ afterAll(() => {18+ consoleErrorSpy.mockRestore();19+ });20+21+ test('when specify unknown program option then error', () => {22+ const program = new commander.Command();23+ program24+ .exitOverride()25+ .option('-p, --pepper', 'add pepper');26+27+ expect(() => {28+ program.parse(['node', 'test', '-m']);29+ }).toThrow();30+ });31+32+ 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');38+39+ expect(() => {40+ program.parse(['node', 'test', '-m']);41+ }).toThrow();42+ });43+44+ 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');50+51+ expect(() => {52+ program.parse(['node', 'test', '-m']);53+ }).not.toThrow();54+ });55+56+ 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');62+63+ expect(() => {64+ program.parse(['node', 'test', '-m']);65+ }).not.toThrow();66+ });67+68+ 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(() => { });75+76+ expect(() => {77+ program.parse(['node', 'test', 'sub', '-m']);78+ }).toThrow();79+ });80+81+ 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(() => { });89+90+ expect(() => {91+ program.parse(['node', 'test', 'sub', '-m']);92+ }).not.toThrow();93+ });94+});deleted:tests/command.allowUnknownOptions.test.js-94 removed
1-const commander = require('../');2-3-// Not testing output, just testing whether an error is detected.4-5-describe('allowUnknownOption', () => {6- // Optional. Use internal knowledge to suppress output to keep test output clean.7- let consoleErrorSpy;8-9- beforeAll(() => {10- consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });11- });12-13- afterEach(() => {14- consoleErrorSpy.mockClear();15- });16-17- afterAll(() => {18- consoleErrorSpy.mockRestore();19- });20-21- test('when specify unknown program option then error', () => {22- const program = new commander.Command();23- program24- .exitOverride()25- .option('-p, --pepper', 'add pepper');26-27- expect(() => {28- program.parse(['node', 'test', '-m']);29- }).toThrow();30- });31-32- 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');38-39- expect(() => {40- program.parse(['node', 'test', '-m']);41- }).toThrow();42- });43-44- 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');50-51- expect(() => {52- program.parse(['node', 'test', '-m']);53- }).not.toThrow();54- });55-56- 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');62-63- expect(() => {64- program.parse(['node', 'test', '-m']);65- }).not.toThrow();66- });67-68- 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(() => { });75-76- expect(() => {77- program.parse(['node', 'test', 'sub', '-m']);78- }).toThrow();79- });80-81- 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(() => { });89-90- expect(() => {91- program.parse(['node', 'test', 'sub', '-m']);92- }).not.toThrow();93- });94-});