Skip to content
Myr

options.required.test.js

This file

Path

tests/options.required.test.js

82 lines · 2.8 KiB · JavaScript

Last changed by

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

History of this file

Take it

The walk stopped after 200 commits: lines older than 9c7cfc0 point at it.

Prepare for 6.1.0John Geebefore adoption9c7cfc0

lines 1–9why, one step back
1const commander = require('../');
23// option with required value, no default4describe('option with required value, no default', () => {5 test('when option not specified then value is undefined', () => {6 const program = new commander.Command();7 program8 .option('--cheese <type>', 'cheese type');9 program.parse(['node', 'test']);

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 10why, one step back
10 expect(program.opts().cheese).toBeUndefined();

Prepare for 6.1.0John Geebefore adoption9c7cfc0

lines 11–18why, one step back
11 });
1213 test('when option specified then value is as specified', () => {14 const program = new commander.Command();15 program16 .option('--cheese <type>', 'cheese type');17 const cheeseType = 'blue';18 program.parse(['node', 'test', '--cheese', cheeseType]);

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 19why, one step back
19 expect(program.opts().cheese).toBe(cheeseType);

Prepare for 6.1.0John Geebefore adoption9c7cfc0

lines 20–49why, one step back
20 });
2122 test('when option value not specified then error', () => {23 // Arrange. Mock error routine to allow interception.24 const mockOptionMissingArgument = jest.fn(() => {25 throw new Error('optionMissingArgument');26 });27 const program = new commander.Command();28 program.optionMissingArgument = mockOptionMissingArgument;29 program30 .option('--cheese <type>', 'cheese type');3132 // Act. The throw is due to the above mock, and not default behaviour.33 expect(() => {34 program.parse(['node', 'test', '--cheese']);35 }).toThrow();3637 // Assert38 expect(mockOptionMissingArgument).toHaveBeenCalled();39 });40});4142// option with required value, with default43describe('option with required value, with default', () => {44 test('when option not specified then value is default', () => {45 const defaultValue = 'default';46 const program = new commander.Command();47 program48 .option('--cheese <type>', 'cheese type', defaultValue);49 program.parse(['node', 'test']);

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 50why, one step back
50 expect(program.opts().cheese).toBe(defaultValue);

Prepare for 6.1.0John Geebefore adoption9c7cfc0

lines 51–59why, one step back
51 });
5253 test('when option specified then value is as specified', () => {54 const defaultValue = 'default';55 const program = new commander.Command();56 program57 .option('--cheese <type>', 'cheese type', defaultValue);58 const cheeseType = 'blue';59 program.parse(['node', 'test', '--cheese', cheeseType]);

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 60why, one step back
60 expect(program.opts().cheese).toBe(cheeseType);

Prepare for 6.1.0John Geebefore adoption9c7cfc0

lines 61–82why, one step back
61 });
6263 test('when option value not specified then error', () => {64 // Arrange. Mock error routine to allow interception.65 const mockOptionMissingArgument = jest.fn(() => {66 throw new Error('optionMissingArgument');67 });68 const defaultValue = 'default';69 const program = new commander.Command();70 program.optionMissingArgument = mockOptionMissingArgument;71 program72 .option('--cheese <type>', 'cheese type', defaultValue);7374 // Act. The throw is due to the above mock, and not default behaviour.75 expect(() => {76 program.parse(['node', 'test', '--cheese']);77 }).toThrow();7879 // Assert80 expect(mockOptionMissingArgument).toHaveBeenCalled();81 });82});