Skip to content
Myr

options.custom-processing.test.js

This file

Path

tests/options.custom-processing.test.js

141 lines · 4.8 KiB · JavaScript

Last changed by

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

History of this file

Take it

Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4

lines 1–141why, one step back
1const commander = require('../');
23function myParseInt(value, dummyPrevious) {4 // parseInt takes a string and a radix5 return parseInt(value, 10);6}78function increaseVerbosity(dummyValue, previous) {9 return previous + 1;10}1112function collect(value, previous) {13 return previous.concat([value]);14}1516function commaSeparatedList(value, dummyPrevious) {17 return value.split(',');18}1920test('when option not specified then callback not called', () => {21 const mockCoercion = jest.fn();22 const program = new commander.Command();23 program24 .option('-i, --integer <n>', 'number', mockCoercion);25 program.parse(['node', 'test']);26 expect(mockCoercion).not.toHaveBeenCalled();27});2829test('when option not specified then value is undefined', () => {30 const program = new commander.Command();31 program32 .option('-i, --integer <n>', 'number', myParseInt);33 program.parse(['node', 'test']);34 expect(program.integer).toBeUndefined();35});3637test('when starting value is defined and option not specified then callback not called', () => {38 const mockCoercion = jest.fn();39 const program = new commander.Command();40 program41 .option('-i, --integer <n>', 'number', mockCoercion, 1);42 program.parse(['node', 'test']);43 expect(mockCoercion).not.toHaveBeenCalled();44});4546test('when starting value is defined and option not specified then value is starting value', () => {47 // NB: Can not specify a starting value for a boolean flag! Discovered when writing this test...48 const startingValue = 1;49 const program = new commander.Command();50 program51 .option('-i, --integer <n>', 'number', myParseInt, startingValue);52 program.parse(['node', 'test']);53 expect(program.integer).toBe(startingValue);54});5556test('when option specified then callback called with value', () => {57 const mockCoercion = jest.fn();58 const value = '1';59 const program = new commander.Command();60 program61 .option('-i, --integer <n>', 'number', mockCoercion);62 program.parse(['node', 'test', '-i', value]);63 expect(mockCoercion).toHaveBeenCalledWith(value, undefined);64});6566test('when option specified then value is as returned from callback ', () => {67 const callbackResult = 2;68 const program = new commander.Command();69 program70 .option('-i, --integer <n>', 'number', () => {71 return callbackResult;72 });73 program.parse(['node', 'test', '-i', '0']);74 expect(program.integer).toBe(callbackResult);75});7677test('when starting value is defined and option specified then callback called with value and starting value', () => {78 const mockCoercion = jest.fn();79 const startingValue = 1;80 const value = '2';81 const program = new commander.Command();82 program83 .option('-i, --integer <n>', 'number', mockCoercion, startingValue);84 program.parse(['node', 'test', '-i', value]);85 expect(mockCoercion).toHaveBeenCalledWith(value, startingValue);86});8788test('when option specified multiple times then callback called with value and previousValue', () => {89 const mockCoercion = jest.fn().mockImplementation(() => {90 return 'callback';91 });92 const program = new commander.Command();93 program94 .option('-i, --integer <n>', 'number', mockCoercion);95 program.parse(['node', 'test', '-i', '1', '-i', '2']);96 expect(mockCoercion).toHaveBeenCalledTimes(2);97 expect(mockCoercion).toHaveBeenNthCalledWith(1, '1', undefined);98 expect(mockCoercion).toHaveBeenNthCalledWith(2, '2', 'callback');99});100101// Now some functional tests like the examples in the README!102103test('when parseFloat "1e2" then value is 100', () => {104 const program = new commander.Command();105 program106 .option('-f, --float <number>', 'float argument', parseFloat);107 program.parse(['node', 'test', '-f', '1e2']);108 expect(program.float).toBe(100);109});110111test('when myParseInt "1" then value is 1', () => {112 const program = new commander.Command();113 program114 .option('-i, --integer <number>', 'integer argument', myParseInt);115 program.parse(['node', 'test', '-i', '1']);116 expect(program.integer).toBe(1);117});118119test('when increaseVerbosity -v -v -v then value is 3', () => {120 const program = new commander.Command();121 program122 .option('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0);123 program.parse(['node', 'test', '-v', '-v', '-v']);124 expect(program.verbose).toBe(3);125});126127test('when collect -c a -c b -c c then value is [a, b, c]', () => {128 const program = new commander.Command();129 program130 .option('-c, --collect <value>', 'repeatable value', collect, []);131 program.parse(['node', 'test', '-c', 'a', '-c', 'b', '-c', 'c']);132 expect(program.collect).toEqual(['a', 'b', 'c']);133});134135test('when commaSeparatedList x,y,z then value is [x, y, z]', () => {136 const program = new commander.Command();137 program138 .option('-l, --list <items>', 'comma separated list', commaSeparatedList);139 program.parse(['node', 'test', '--list', 'x,y,z']);140 expect(program.list).toEqual(['x', 'y', 'z']);141});