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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687const commander = require('../');
// Testing variadic arguments. Testing all the action arguments, but could test just variadicArg.
describe('variadic argument', () => { // Optional. Use internal knowledge to suppress output to keep test output clean. let consoleErrorSpy;
beforeAll(() => { consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { }); });
afterEach(() => { consoleErrorSpy.mockClear(); });
afterAll(() => { consoleErrorSpy.mockRestore(); });
test('when no extra arguments specified for program then variadic arg is empty array', () => { const actionMock = jest.fn(); const program = new commander.Command(); program .arguments('<id> [variadicArg...]') .action(actionMock);
program.parse(['node', 'test', 'id']);
expect(actionMock).toHaveBeenCalledWith('id', [], program); });
test('when extra arguments specified for program then variadic arg is array of values', () => { const actionMock = jest.fn(); const program = new commander.Command(); program .arguments('<id> [variadicArg...]') .action(actionMock); const extraArguments = ['a', 'b', 'c'];
program.parse(['node', 'test', 'id', ...extraArguments]);
expect(actionMock).toHaveBeenCalledWith('id', extraArguments, program); });
test('when no extra arguments specified for command then variadic arg is empty array', () => { const actionMock = jest.fn(); const program = new commander.Command(); const cmd = program .command('sub [variadicArg...]') .action(actionMock);
program.parse(['node', 'test', 'sub']);
expect(actionMock).toHaveBeenCalledWith([], cmd); });
test('when extra arguments specified for command then variadic arg is array of values', () => { const actionMock = jest.fn(); const program = new commander.Command(); const cmd = program .command('sub [variadicArg...]') .action(actionMock); const extraArguments = ['a', 'b', 'c'];
program.parse(['node', 'test', 'sub', ...extraArguments]);
expect(actionMock).toHaveBeenCalledWith(extraArguments, cmd); });
test('when program variadic argument not last then error', () => { const program = new commander.Command();
expect(() => { program.arguments('<variadicArg...> [optionalArg]'); }).toThrow("only the last argument can be variadic 'variadicArg'"); });
test('when command variadic argument not last then error', () => { const program = new commander.Command();
expect(() => { program.command('sub <variadicArg...> [optionalArg]'); }).toThrow("only the last argument can be variadic 'variadicArg'"); });});