options.required.test.js
This file
Path
tests/options.required.test.js
70 lines · 2.4 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import * as commander from '../index.js';import { createTestCommand } from './testHelpers.js';import { test, describe } from 'node:test';import assert from 'node:assert/strict';
// option with required value, no defaultdescribe('option with required option-argument, no default', () => { test('when option not specified then value is undefined', () => { const program = new commander.Command(); program.option('--cheese <type>', 'cheese type'); program.parse(['node', 'test']); assert.equal(program.opts().cheese, undefined); });
test('when option specified then value is as specified', () => { const program = new commander.Command(); program.option('--cheese <type>', 'cheese type'); const cheeseType = 'blue'; program.parse(['node', 'test', '--cheese', cheeseType]); assert.equal(program.opts().cheese, cheeseType); });
test('when option value not specified then error', () => { const program = createTestCommand(); program.option('--cheese <type>', 'cheese type');
// Act. The throw is due to the above mock, and not default behaviour. assert.throws( () => { program.parse(['node', 'test', '--cheese']); }, { code: 'commander.optionMissingArgument', }, ); });});
// option with required value, with defaultdescribe('option with required option-argument, with default', () => { test('when option not specified then value is default', () => { const defaultValue = 'default'; const program = new commander.Command(); program.option('--cheese <type>', 'cheese type', defaultValue); program.parse(['node', 'test']); assert.equal(program.opts().cheese, defaultValue); });
test('when option specified then value is as specified', () => { const defaultValue = 'default'; const program = new commander.Command(); program.option('--cheese <type>', 'cheese type', defaultValue); const cheeseType = 'blue'; program.parse(['node', 'test', '--cheese', cheeseType]); assert.equal(program.opts().cheese, cheeseType); });
test('when option value not specified then error', (t) => { const defaultValue = 'default'; const program = createTestCommand(); program.option('--cheese <type>', 'cheese type', defaultValue);
assert.throws( () => { program.parse(['node', 'test', '--cheese']); }, { code: 'commander.optionMissingArgument' }, ); });});