args.variadic.test.js
This file
Path
tests/args.variadic.test.js
86 lines · 2.6 KiB · JavaScript
Last changed by
Throw for author errors and improve (#1165)John Geebefore adoptiond9ac483
History of this fileTake it
Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 1–4why, one step back1const commander = require('../');23// Testing variadic arguments. Testing all the action arguments, but could test just variadicArg.4Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 5why, one step back5describe('variadic argument', () => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 6–74why, one step back6 // 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 no extra arguments specified for program then variadic arg is empty array', () => {22 const actionMock = jest.fn();23 const program = new commander.Command();24 program25 .arguments('<id> [variadicArg...]')26 .action(actionMock);2728 program.parse(['node', 'test', 'id']);2930 expect(actionMock).toHaveBeenCalledWith('id', [], program);31 });3233 test('when extra arguments specified for program then variadic arg is array of values', () => {34 const actionMock = jest.fn();35 const program = new commander.Command();36 program37 .arguments('<id> [variadicArg...]')38 .action(actionMock);39 const extraArguments = ['a', 'b', 'c'];4041 program.parse(['node', 'test', 'id', ...extraArguments]);4243 expect(actionMock).toHaveBeenCalledWith('id', extraArguments, program);44 });4546 test('when no extra arguments specified for command then variadic arg is empty array', () => {47 const actionMock = jest.fn();48 const program = new commander.Command();49 const cmd = program50 .command('sub [variadicArg...]')51 .action(actionMock);5253 program.parse(['node', 'test', 'sub']);5455 expect(actionMock).toHaveBeenCalledWith([], cmd);56 });5758 test('when extra arguments specified for command then variadic arg is array of values', () => {59 const actionMock = jest.fn();60 const program = new commander.Command();61 const cmd = program62 .command('sub [variadicArg...]')63 .action(actionMock);64 const extraArguments = ['a', 'b', 'c'];6566 program.parse(['node', 'test', 'sub', ...extraArguments]);6768 expect(actionMock).toHaveBeenCalledWith(extraArguments, cmd);69 });7071 test('when program variadic argument not last then error', () => {72 const program = new commander.Command();7374 expect(() => {Throw for author errors and improve (#1165)John Geebefore adoptiond9ac483
lines 75–76why, one step back75 program.arguments('<variadicArg...> [optionalArg]');76 }).toThrow("only the last argument can be variadic 'variadicArg'");Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 77–82why, one step back77 });7879 test('when command variadic argument not last then error', () => {80 const program = new commander.Command();8182 expect(() => {Throw for author errors and improve (#1165)John Geebefore adoptiond9ac483
lines 83–84why, one step back83 program.command('sub <variadicArg...> [optionalArg]');84 }).toThrow("only the last argument can be variadic 'variadicArg'");Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 85–86why, one step back85 });86});