argument.choices.test.js
This file
Path
tests/argument.choices.test.js
57 lines · 2.1 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758import * as commander from '../index.js';import { createTestCommand } from './testHelpers.js';import { test, describe } from 'node:test';import assert from 'node:assert/strict';
describe('Argument.choices()', () => { test('when command argument in choices then argument set', () => { const program = createTestCommand(); let shade; program .addArgument(new commander.Argument('<shade>').choices(['red', 'blue'])) .action((shadeParam) => { shade = shadeParam; }); program.parse(['red'], { from: 'user' }); assert.equal(shade, 'red'); });
test('when command argument is not in choices then error', () => { // Lightweight check, more detailed testing of behaviour in command.exitOverride.test.js const program = createTestCommand(); program.addArgument( new commander.Argument('<shade>').choices(['red', 'blue']), ); assert.throws(() => { program.parse(['orange'], { from: 'user' }); }); });});
describe('Argument.choices() parameter is treated as readonly, per TypeScript declaration', () => { test('when choices called then parameter does not change', () => { // Unlikely this could break, but check the API we are declaring in TypeScript. const original = ['red', 'blue', 'green']; const param = original.slice(); new commander.Argument('<shade>').choices(param); assert.deepEqual(param, original); });
test('when choices called and argChoices later changed then parameter does not change', () => { const original = ['red', 'blue', 'green']; const param = original.slice(); const argument = new commander.Argument('<shade>').choices(param); argument.argChoices.push('purple'); assert.deepEqual(param, original); });
test('when choices called and parameter changed the choices does not change', () => { const program = createTestCommand(); const param = ['red', 'blue']; program.addArgument(new commander.Argument('<shade>').choices(param)); param.push('orange'); assert.throws(() => { program.parse(['orange'], { from: 'user' }); }); });});