command.allowUnknownOption.test.js
This file
Path
tests/command.allowUnknownOption.test.js
95 lines · 2.5 KiB · JavaScript
Last changed by
More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
History of this fileTake it
Rename test files to match method being testedJohn Geebefore adoption5b98fc8
lines 1–6why, one step back1const commander = require('../');23// Not testing output, just testing whether an error is detected.45describe('allowUnknownOption', () => {6 // Optional. Use internal knowledge to suppress output to keep test output clean.configure output (#1387)John Geebefore adoptioned7f13e
line 7why, one step back7 let writeErrorSpy;Rename test files to match method being testedJohn Geebefore adoption5b98fc8
lines 8–9why, one step back89 beforeAll(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 10why, one step back10 writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });Rename test files to match method being testedJohn Geebefore adoption5b98fc8
lines 11–13why, one step back11 });1213 afterEach(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 14why, one step back14 writeErrorSpy.mockClear();Rename test files to match method being testedJohn Geebefore adoption5b98fc8
lines 15–17why, one step back15 });1617 afterAll(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 18why, one step back18 writeErrorSpy.mockRestore();Rename test files to match method being testedJohn Geebefore adoption5b98fc8
lines 19–85why, one step back19 });2021 test('when specify unknown program option then error', () => {22 const program = new commander.Command();23 program24 .exitOverride()25 .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(false) then error', () => {33 const program = new commander.Command();34 program35 .exitOverride()36 .allowUnknownOption(false)37 .option('-p, --pepper', 'add pepper');3839 expect(() => {40 program.parse(['node', 'test', '-m']);41 }).toThrow();42 });4344 test('when specify unknown program option and allowUnknownOption() then no error', () => {45 const program = new commander.Command();46 program47 .exitOverride()48 .allowUnknownOption()49 .option('-p, --pepper', 'add pepper');5051 expect(() => {52 program.parse(['node', 'test', '-m']);53 }).not.toThrow();54 });5556 test('when specify unknown program option and allowUnknownOption(true) then no error', () => {57 const program = new commander.Command();58 program59 .exitOverride()60 .allowUnknownOption(true)61 .option('-p, --pepper', 'add pepper');6263 expect(() => {64 program.parse(['node', 'test', '-m']);65 }).not.toThrow();66 });6768 test('when specify unknown command option then error', () => {69 const program = new commander.Command();70 program71 .exitOverride()72 .command('sub')73 .option('-p, --pepper', 'add pepper')74 .action(() => { });7576 expect(() => {77 program.parse(['node', 'test', 'sub', '-m']);78 }).toThrow();79 });8081 test('when specify unknown command option and allowUnknownOption then no error', () => {82 const program = new commander.Command();83 program84 .exitOverride()85 .command('sub')More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9
line 86why, one step back86 .argument('[args...]') // unknown option will be passed as an argumentRename test files to match method being testedJohn Geebefore adoption5b98fc8
lines 87–95why, one step back87 .allowUnknownOption()88 .option('-p, --pepper', 'add pepper')89 .action(() => { });9091 expect(() => {92 program.parse(['node', 'test', 'sub', '-m']);93 }).not.toThrow();94 });95});