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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119const commander = require('../');
// .command('*') is the old main/default command handler. It adds a listener// for 'command:*'. It has been somewhat replaced by the program action handler,// so most uses are probably old code. Current plan is keep the code backwards compatible// and put work in elsewhere for new code (e.g. evolving behaviour for program action handler).//// The event 'command:*' is also listened for directly for testing for unknown commands// due to an example in the README.//// Historical: the event 'command:*' used to also be shared by the action handler on the program.
describe(".command('*')", () => { // Use internal knowledge to suppress output to keep test output clean. let writeMock;
beforeAll(() => { writeMock = jest.spyOn(process.stdout, 'write').mockImplementation(() => { }); });
afterAll(() => { writeMock.mockRestore(); });
test('when no arguments then asterisk action not called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .exitOverride() // to catch help .command('*') .action(mockAction); try { program.parse(['node', 'test']); } catch (err) { ; } expect(mockAction).not.toHaveBeenCalled(); });
test('when unrecognised argument then asterisk action called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('*') .action(mockAction); program.parse(['node', 'test', 'unrecognised-command']); expect(mockAction).toHaveBeenCalled(); });
test('when recognised command then asterisk action not called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('install') .action(() => { }); program .command('*') .action(mockAction); program.parse(['node', 'test', 'install']); expect(mockAction).not.toHaveBeenCalled(); });
test('when unrecognised command/argument then asterisk action called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('install'); program .command('*') .action(mockAction); program.parse(['node', 'test', 'unrecognised-command']); expect(mockAction).toHaveBeenCalled(); });});
// Test .on explicitly rather than assuming covered by .commanddescribe(".on('command:*')", () => { test('when no arguments then listener not called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .on('command:*', mockAction); program.parse(['node', 'test']); expect(mockAction).not.toHaveBeenCalled(); });
test('when unrecognised argument then listener called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .on('command:*', mockAction); program.parse(['node', 'test', 'unrecognised-command']); expect(mockAction).toHaveBeenCalled(); });
test('when recognised command then listener not called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('install') .action(() => { }); program .on('command:*', mockAction); program.parse(['node', 'test', 'install']); expect(mockAction).not.toHaveBeenCalled(); });
test('when unrecognised command/argument then listener called', () => { const mockAction = jest.fn(); const program = new commander.Command(); program .command('install'); program .on('command:*', mockAction); program.parse(['node', 'test', 'unrecognised-command']); expect(mockAction).toHaveBeenCalled(); });});