args.variadic.test.js
This file
Path
tests/args.variadic.test.js
80 lines · 2.6 KiB · JavaScript
Last changed by
Test coverage (#1433)John Geebefore adoption891e23d
History of this fileTake it
The walk stopped after 200 commits: lines older than f47bdf5 point at it.
Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 1–14why, one step back1const commander = require('../');23// Testing variadic arguments. Testing all the action arguments, but could test just variadicArg.45describe('variadic argument', () => {6 test('when no extra arguments specified for program then variadic arg is empty array', () => {7 const actionMock = jest.fn();8 const program = new commander.Command();9 program10 .arguments('<id> [variadicArg...]')11 .action(actionMock);1213 program.parse(['node', 'test', 'id']);14Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 15why, one step back15 expect(actionMock).toHaveBeenCalledWith('id', [], program.opts(), program);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 16–27why, one step back16 });1718 test('when extra arguments specified for program then variadic arg is array of values', () => {19 const actionMock = jest.fn();20 const program = new commander.Command();21 program22 .arguments('<id> [variadicArg...]')23 .action(actionMock);24 const extraArguments = ['a', 'b', 'c'];2526 program.parse(['node', 'test', 'id', ...extraArguments]);27Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 28why, one step back28 expect(actionMock).toHaveBeenCalledWith('id', extraArguments, program.opts(), program);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 29–39why, one step back29 });3031 test('when no extra arguments specified for command then variadic arg is empty array', () => {32 const actionMock = jest.fn();33 const program = new commander.Command();34 const cmd = program35 .command('sub [variadicArg...]')36 .action(actionMock);3738 program.parse(['node', 'test', 'sub']);39Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 40why, one step back40 expect(actionMock).toHaveBeenCalledWith([], cmd.opts(), cmd);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 41–52why, one step back41 });4243 test('when extra arguments specified for command then variadic arg is array of values', () => {44 const actionMock = jest.fn();45 const program = new commander.Command();46 const cmd = program47 .command('sub [variadicArg...]')48 .action(actionMock);49 const extraArguments = ['a', 'b', 'c'];5051 program.parse(['node', 'test', 'sub', ...extraArguments]);52Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 53why, one step back53 expect(actionMock).toHaveBeenCalledWith(extraArguments, cmd.opts(), cmd);Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
lines 54–70why, one step back54 });5556 test('when program variadic argument not last then error', () => {57 const program = new commander.Command();5859 expect(() => {60 program.arguments('<variadicArg...> [optionalArg]');61 }).toThrow("only the last argument can be variadic 'variadicArg'");62 });6364 test('when command variadic argument not last then error', () => {65 const program = new commander.Command();6667 expect(() => {68 program.command('sub <variadicArg...> [optionalArg]');69 }).toThrow("only the last argument can be variadic 'variadicArg'");70 });Test coverage (#1433)John Geebefore adoption891e23d
lines 71–79why, one step back7172 test('when variadic argument then usage shows variadic', () => {73 const program = new commander.Command();74 program75 .name('foo')76 .arguments('[args...]');7778 expect(program.usage()).toBe('[options] [args...]');79 });Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5
line 80why, one step back80});