Skip to content
Myr

args.variadic.test.js

This file

Path

tests/args.variadic.test.js

94 lines · 2.8 KiB · JavaScript

Last changed by

Merge pull request #1050 from shadowspawn/feature/update-dependenciesTomoaki Abebefore adoption36ae7a9

History of this file

Take it

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 1–73why, one step back
1const commander = require('../');
23// Testing variadic arguments. Testing all the action arguments, but could test just variadicArg.45describe('.version', () => {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 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();73 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 74why, one step back
74 .exitOverride()

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 75–79why, one step back
75 .arguments('<variadicArg...> [optionalArg]')
76 .action(jest.fn);7778 expect(() => {79 program.parse(['node', 'test', 'a']);

Merge pull request #1050 from shadowspawn/feature/update-dependenciesTomoaki Abebefore adoption36ae7a9

line 80why, one step back
80 }).toThrow("error: variadic arguments must be last 'variadicArg'");

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 81–85why, one step back
81 });
8283 test('when command variadic argument not last then error', () => {84 const program = new commander.Command();85 program

Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e

line 86why, one step back
86 .exitOverride()

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 87–91why, one step back
87 .command('sub <variadicArg...> [optionalArg]')
88 .action(jest.fn);8990 expect(() => {91 program.parse(['node', 'test', 'sub', 'a']);

Merge pull request #1050 from shadowspawn/feature/update-dependenciesTomoaki Abebefore adoption36ae7a9

line 92why, one step back
92 }).toThrow("error: variadic arguments must be last 'variadicArg'");

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 93–94why, one step back
93 });
94});