command.onCommand.test.js
This file
Path
tests/command.onCommand.test.js
37 lines · 1.5 KiB · JavaScript
Last changed by
Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738const commander = require('../');const { test, describe } = require('node:test');const assert = require('node:assert/strict');
// The action handler used to be implemented using command events and listeners.// Now, this is mostly just for backwards compatibility.
describe("Command.command('*')", () => { test('when action handler for subcommand then emit command:subcommand', (t) => { const mockListener = t.mock.fn(); const program = new commander.Command(); program.command('sub').action(() => {}); program.on('command:sub', mockListener); program.parse(['sub'], { from: 'user' }); assert.equal(mockListener.mock.callCount(), 1); });
test('when no action handler for subcommand then still emit command:subcommand', (t) => { const mockListener = t.mock.fn(); const program = new commander.Command(); program.command('sub'); program.on('command:sub', mockListener); program.parse(['sub'], { from: 'user' }); assert.equal(mockListener.mock.callCount(), 1); });
test('when subcommand has argument then emit command:subcommand with argument', (t) => { const mockListener = t.mock.fn(); const program = new commander.Command(); program.command('sub <file>').action(() => {}); program.on('command:sub', mockListener); program.parse(['sub', 'file'], { from: 'user' }); assert.equal(mockListener.mock.callCount(), 1); const callArgs = mockListener.mock.calls[0].arguments; assert.deepEqual(callArgs, [['file'], []]); });});