commander.configureCommand.test.js
This file
Path
tests/commander.configureCommand.test.js
86 lines · 2.8 KiB · JavaScript
Last changed by
More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687const commander = require('../');
// Mostly testing direct on program, limited check that (sub)command working same.
// Default behaviours
test('when default then options not stored on command', () => { const program = new commander.Command(); program .option('--foo <value>', 'description'); program.parse(['node', 'test', '--foo', 'bar']); expect(program.foo).toBeUndefined(); expect(program.opts().foo).toBe('bar');});
test('when default then options+command passed to action', () => { const program = new commander.Command(); const callback = jest.fn(); program .argument('<value>') .action(callback); program.parse(['node', 'test', 'value']); expect(callback).toHaveBeenCalledWith('value', program.opts(), program);});
// storeOptionsAsProperties
test('when storeOptionsAsProperties() then options stored on command', () => { const program = new commander.Command(); program .storeOptionsAsProperties() .option('--foo <value>', 'description'); program.parse(['node', 'test', '--foo', 'bar']); expect(program.foo).toBe('bar'); expect(program.opts().foo).toBe('bar');});
test('when storeOptionsAsProperties(true) then options stored on command', () => { const program = new commander.Command(); program .storeOptionsAsProperties(true) .option('--foo <value>', 'description'); program.parse(['node', 'test', '--foo', 'bar']); expect(program.foo).toBe('bar'); expect(program.opts().foo).toBe('bar');});
test('when storeOptionsAsProperties(false) then options not stored on command', () => { const program = new commander.Command(); program .storeOptionsAsProperties(false) .option('--foo <value>', 'description'); program.parse(['node', 'test', '--foo', 'bar']); expect(program.foo).toBeUndefined(); expect(program.opts().foo).toBe('bar');});
test('when storeOptionsAsProperties() then command+command passed to action', () => { const program = new commander.Command(); const callback = jest.fn(); program .storeOptionsAsProperties() .argument('<value>') .action(callback); program.parse(['node', 'test', 'value']); expect(callback).toHaveBeenCalledWith('value', program, program);});
test('when storeOptionsAsProperties(false) then opts+command passed to action', () => { const program = new commander.Command(); const callback = jest.fn(); program .storeOptionsAsProperties(false) .argument('<value>') .action(callback); program.parse(['node', 'test', 'value']); expect(callback).toHaveBeenCalledWith('value', program.opts(), program);});
test('when storeOptionsAsProperties() after adding option then throw', () => { const program = new commander.Command(); program.option('--port <number>', 'port number', '80'); expect(() => { program.storeOptionsAsProperties(false); }).toThrow();});