Skip to content
Myr

command.action.test.js

This file

Path

tests/command.action.test.js

90 lines · 3.0 KiB · JavaScript

Last changed by

Merge pull request #1063 from shadowspawn/feature/test-mainTomoaki Abebefore adoptiona45d676

History of this file

Take it

Do not mutate program.args when calling action handler (#1048)John Geebefore adoptioncc4a525

lines 1–36why, one step back
1const commander = require('../');
23// Test some behaviours of .action not covered in more specific tests.45test('when .action called then command passed to action', () => {6 const actionMock = jest.fn();7 const program = new commander.Command();8 const cmd = program9 .command('info')10 .action(actionMock);11 program.parse(['node', 'test', 'info']);12 expect(actionMock).toHaveBeenCalledWith(cmd);13});1415test('when .action called then program.args only contains args', () => {16 // At one time program.args was being modified to contain the same args as the call to .action17 // and so included the command as an extra and unexpected complex item in array.18 const program = new commander.Command();19 program20 .command('info <file>')21 .action(() => {});22 program.parse(['node', 'test', 'info', 'my-file']);23 expect(program.args).toEqual(['my-file']);24});2526test('when .action called with extra arguments then extras also passed to action', () => {27 // This is a new and undocumented behaviour for now.28 // Might make this an error by default in future.29 const actionMock = jest.fn();30 const program = new commander.Command();31 const cmd = program32 .command('info <file>')33 .action(actionMock);34 program.parse(['node', 'test', 'info', 'my-file', 'a']);35 expect(actionMock).toHaveBeenCalledWith('my-file', cmd, ['a']);36});

Merge pull request #1063 from shadowspawn/feature/test-mainTomoaki Abebefore adoptiona45d676

lines 37–90why, one step back
37
38test('when .action on program with argument then action called', () => {39 const actionMock = jest.fn();40 const program = new commander.Command();41 program42 .arguments('<file>')43 .action(actionMock);44 program.parse(['node', 'test', 'my-file']);45 expect(actionMock).toHaveBeenCalledWith('my-file', program);46});4748// Changes made in #729 to call program action handler49test('when .action on program and no arguments then action called', () => {50 const actionMock = jest.fn();51 const program = new commander.Command();52 program53 .action(actionMock);54 program.parse(['node', 'test']);55 expect(actionMock).toHaveBeenCalledWith(program);56});5758test('when .action on program with optional argument supplied then action called', () => {59 const actionMock = jest.fn();60 const program = new commander.Command();61 program62 .arguments('[file]')63 .action(actionMock);64 program.parse(['node', 'test', 'my-file']);65 expect(actionMock).toHaveBeenCalledWith('my-file', program);66});6768test('when .action on program without optional argument supplied then action called', () => {69 const actionMock = jest.fn();70 const program = new commander.Command();71 program72 .arguments('[file]')73 .action(actionMock);74 program.parse(['node', 'test']);75 expect(actionMock).toHaveBeenCalledWith(undefined, program);76});7778test('when .action on program with subcommand and program argument then program action called', () => {79 const actionMock = jest.fn();80 const program = new commander.Command();81 program82 .arguments('[file]')83 .action(actionMock);84 program85 .command('subcommand');8687 program.parse(['node', 'test', 'a']);8889 expect(actionMock).toHaveBeenCalledWith('a', program);90});