command.allowUnknownOptions.test.js
This file
Path
tests/command.allowUnknownOptions.test.js
70 lines · 1.8 KiB · JavaScript
Last changed by
Add override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071const commander = require('../');
// Not testing output, just testing whether an error is detected.
describe('.version', () => { // Optional. Use internal knowledge to suppress output to keep test output clean. let consoleErrorSpy;
beforeAll(() => { consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { }); });
afterEach(() => { consoleErrorSpy.mockClear(); });
afterAll(() => { consoleErrorSpy.mockRestore(); });
test('when specify unknown program option then error', () => { const program = new commander.Command(); program .exitOverride() .option('-p, --pepper', 'add pepper');
expect(() => { program.parse(['node', 'test', '-m']); }).toThrow(); });
test('when specify unknown program option and allowUnknownOption then no error', () => { const program = new commander.Command(); program .exitOverride() .allowUnknownOption() .option('-p, --pepper', 'add pepper');
expect(() => { program.parse(['node', 'test', '-m']); }).not.toThrow(); });
test('when specify unknown command option then error', () => { const program = new commander.Command(); program .exitOverride() .command('sub') .option('-p, --pepper', 'add pepper') .action(() => { });
expect(() => { program.parse(['node', 'test', 'sub', '-m']); }).toThrow(); });
test('when specify unknown command option and allowUnknownOption then no error', () => { const program = new commander.Command(); program .exitOverride() .command('sub') .allowUnknownOption() .option('-p, --pepper', 'add pepper') .action(() => { });
expect(() => { program.parse(['node', 'test', 'sub', '-m']); }).not.toThrow(); });});