commander.storeOptionsAsProperties.test.js
This file
Path
tests/commander.storeOptionsAsProperties.test.js
99 lines · 3.6 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
lines 1–3why, one step back1import * as commander from '../index.js';2import { test, describe } from 'node:test';3import assert from 'node:assert/strict';Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 4–99why, one step back45// Mostly testing direct on program, limited check that (sub)command working same.67describe('Command.storeOptionsAsProperties()', () => {8 // Default behaviours910 test('when default then options not stored on command', () => {11 const program = new commander.Command();12 program.option('--foo <value>', 'description');13 program.parse(['node', 'test', '--foo', 'bar']);14 assert.equal(program.foo, undefined);15 assert.equal(program.opts().foo, 'bar');16 });1718 test('when default then options+command passed to action', (t) => {19 const program = new commander.Command();20 const callback = t.mock.fn();21 program.argument('<value>').action(callback);22 program.parse(['node', 'test', 'value']);23 const callArgs = callback.mock.calls[0].arguments;24 assert.equal(callArgs[0], 'value');25 assert.equal(callArgs[1], program.opts());26 assert.equal(callArgs[2], program);27 });2829 // storeOptionsAsProperties3031 test('when storeOptionsAsProperties() then options stored on command', () => {32 const program = new commander.Command();33 program.storeOptionsAsProperties().option('--foo <value>', 'description');34 program.parse(['node', 'test', '--foo', 'bar']);35 assert.equal(program.foo, 'bar');36 assert.equal(program.opts().foo, 'bar');37 });3839 test('when storeOptionsAsProperties(true) then options stored on command', () => {40 const program = new commander.Command();41 program42 .storeOptionsAsProperties(true)43 .option('--foo <value>', 'description');44 program.parse(['node', 'test', '--foo', 'bar']);45 assert.equal(program.foo, 'bar');46 assert.equal(program.opts().foo, 'bar');47 });4849 test('when storeOptionsAsProperties(false) then options not stored on command', () => {50 const program = new commander.Command();51 program52 .storeOptionsAsProperties(false)53 .option('--foo <value>', 'description');54 program.parse(['node', 'test', '--foo', 'bar']);55 assert.equal(program.foo, undefined);56 assert.equal(program.opts().foo, 'bar');57 });5859 test('when storeOptionsAsProperties() then command+command passed to action', (t) => {60 const program = new commander.Command();61 const callback = t.mock.fn();62 program.storeOptionsAsProperties().argument('<value>').action(callback);63 program.parse(['node', 'test', 'value']);64 const callArgs = callback.mock.calls[0].arguments;65 assert.equal(callArgs[0], 'value');66 assert.equal(callArgs[1], program);67 assert.equal(callArgs[2], program);68 });6970 test('when storeOptionsAsProperties(false) then opts+command passed to action', (t) => {71 const program = new commander.Command();72 const callback = t.mock.fn();73 program74 .storeOptionsAsProperties(false)75 .argument('<value>')76 .action(callback);77 program.parse(['node', 'test', 'value']);78 const callArgs = callback.mock.calls[0].arguments;79 assert.equal(callArgs[0], 'value');80 assert.equal(callArgs[1], program.opts());81 assert.equal(callArgs[2], program);82 });8384 test('when storeOptionsAsProperties() after adding option then throw', () => {85 const program = new commander.Command();86 program.option('--port <number>', 'port number', '80');87 assert.throws(() => {88 program.storeOptionsAsProperties();89 });90 });9192 test('when storeOptionsAsProperties() after setting option value then throw', () => {93 const program = new commander.Command();94 program.setOptionValue('foo', 'bar');95 assert.throws(() => {96 program.storeOptionsAsProperties();97 });98 });99});