command.asterisk.test.js
This file
Path
tests/command.asterisk.test.js
118 lines · 3.7 KiB · JavaScript
Last changed by
Replace git-style wording from README (#1171)John Geebefore adoptionfbdd132
History of this fileTake it
Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 1–2why, one step back1const commander = require('../');2Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
lines 3–8why, one step back3// .command('*') is the old main/default command handler. It adds a listener4// for 'command:*'. It has been somewhat replaced by the program action handler,5// so most uses are probably old code. Current plan is keep the code backwards compatible6// and put work in elsewhere for new code (e.g. evolving behaviour for program action handler).7//8// The event 'command:*' is also listened for directly for testing for unknown commandsReplace git-style wording from README (#1171)John Geebefore adoptionfbdd132
line 9why, one step back9// due to an example in the README.Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
lines 10–11why, one step back10//11// Historical: the event 'command:*' used to also be shared by the action handler on the program.Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
line 12why, one step back12Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
line 13why, one step back13describe(".command('*')", () => {Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 14–24why, one step back14 // Use internal knowledge to suppress output to keep test output clean.15 let writeMock;1617 beforeAll(() => {18 writeMock = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });19 });2021 afterAll(() => {22 writeMock.mockRestore();23 });24Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
lines 25–28why, one step back25 test('when no arguments then asterisk action not called', () => {26 const mockAction = jest.fn();27 const program = new commander.Command();28 programParse rework for nested commands (#1149)John Geebefore adoption1345f98
line 29why, one step back29 .exitOverride() // to catch helpRun default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
lines 30–31why, one step back30 .command('*')31 .action(mockAction);Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 32–36why, one step back32 try {33 program.parse(['node', 'test']);34 } catch (err) {35 ;36 }Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
lines 37–73why, one step back37 expect(mockAction).not.toHaveBeenCalled();38 });3940 test('when unrecognised argument then asterisk action called', () => {41 const mockAction = jest.fn();42 const program = new commander.Command();43 program44 .command('*')45 .action(mockAction);46 program.parse(['node', 'test', 'unrecognised-command']);47 expect(mockAction).toHaveBeenCalled();48 });4950 test('when recognised command then asterisk action not called', () => {51 const mockAction = jest.fn();52 const program = new commander.Command();53 program54 .command('install')55 .action(() => { });56 program57 .command('*')58 .action(mockAction);59 program.parse(['node', 'test', 'install']);60 expect(mockAction).not.toHaveBeenCalled();61 });6263 test('when unrecognised command/argument then asterisk action called', () => {64 const mockAction = jest.fn();65 const program = new commander.Command();66 program67 .command('install');68 program69 .command('*')70 .action(mockAction);71 program.parse(['node', 'test', 'unrecognised-command']);72 expect(mockAction).toHaveBeenCalled();73 });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 74–75why, one step back74});75Run default command if it's not just * (#1062)Justin Bennettbefore adoption4fb9ed4
lines 76–117why, one step back76// Test .on explicitly rather than assuming covered by .command77describe(".on('command:*')", () => {78 test('when no arguments then listener not called', () => {79 const mockAction = jest.fn();80 const program = new commander.Command();81 program82 .on('command:*', mockAction);83 program.parse(['node', 'test']);84 expect(mockAction).not.toHaveBeenCalled();85 });8687 test('when unrecognised argument then listener called', () => {88 const mockAction = jest.fn();89 const program = new commander.Command();90 program91 .on('command:*', mockAction);92 program.parse(['node', 'test', 'unrecognised-command']);93 expect(mockAction).toHaveBeenCalled();94 });9596 test('when recognised command then listener not called', () => {97 const mockAction = jest.fn();98 const program = new commander.Command();99 program100 .command('install')101 .action(() => { });102 program103 .on('command:*', mockAction);104 program.parse(['node', 'test', 'install']);105 expect(mockAction).not.toHaveBeenCalled();106 });107108 test('when unrecognised command/argument then listener called', () => {109 const mockAction = jest.fn();110 const program = new commander.Command();111 program112 .command('install');113 program114 .on('command:*', mockAction);115 program.parse(['node', 'test', 'unrecognised-command']);116 expect(mockAction).toHaveBeenCalled();117 });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
line 118why, one step back118});