command.allowUnknownOptions.test.js
This file
Path
tests/command.allowUnknownOptions.test.js
70 lines · 1.8 KiB · JavaScript
Last changed by
Parse rework for nested commands (#1149)John Geebefore adoption1345f98
History of this fileTake it
Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 1–4why, one step back1const commander = require('../');23// Not testing output, just testing whether an error is detected.4Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 5why, one step back5describe('allowUnknownOption', () => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 6–23why, one step back6 // Optional. Use internal knowledge to suppress output to keep test output clean.7 let consoleErrorSpy;89 beforeAll(() => {10 consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });11 });1213 afterEach(() => {14 consoleErrorSpy.mockClear();15 });1617 afterAll(() => {18 consoleErrorSpy.mockRestore();19 });2021 test('when specify unknown program option then error', () => {22 const program = new commander.Command();23 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 24why, one step back24 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 25–34why, one step back25 .option('-p, --pepper', 'add pepper');2627 expect(() => {28 program.parse(['node', 'test', '-m']);29 }).toThrow();30 });3132 test('when specify unknown program option and allowUnknownOption then no error', () => {33 const program = new commander.Command();34 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 35why, one step back35 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 36–46why, one step back36 .allowUnknownOption()37 .option('-p, --pepper', 'add pepper');3839 expect(() => {40 program.parse(['node', 'test', '-m']);41 }).not.toThrow();42 });4344 test('when specify unknown command option then error', () => {45 const program = new commander.Command();46 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 47why, one step back47 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 48–59why, one step back48 .command('sub')49 .option('-p, --pepper', 'add pepper')50 .action(() => { });5152 expect(() => {53 program.parse(['node', 'test', 'sub', '-m']);54 }).toThrow();55 });5657 test('when specify unknown command option and allowUnknownOption then no error', () => {58 const program = new commander.Command();59 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 60why, one step back60 .exitOverride()Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 61–70why, one step back61 .command('sub')62 .allowUnknownOption()63 .option('-p, --pepper', 'add pepper')64 .action(() => { });6566 expect(() => {67 program.parse(['node', 'test', 'sub', '-m']);68 }).not.toThrow();69 });70});