Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass unknown to command:* to detect unknown options (#1049)
Co-authored-by: Case Larsen <clarsen@gmail.com>
- Loading branch information
1 parent
550deb3
commit 9593656
Showing
2 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const commander = require('../'); | ||
|
||
// Checking for detection of unknown options, including regression tests for some past issues. | ||
|
||
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 option with subcommand and action handler then error', () => { | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.command('info') | ||
.action(() => {}); | ||
|
||
let caughtErr; | ||
try { | ||
program.parse(['node', 'info', '--NONSENSE']); | ||
} catch (err) { | ||
caughtErr = err; | ||
} | ||
expect(caughtErr.code).toBe('commander.unknownOption'); | ||
}); | ||
|
||
test('when specify unknown option with subcommand argument and action handler then error', () => { | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.command('info <file>') | ||
.action(() => {}); | ||
|
||
let caughtErr; | ||
try { | ||
program.parse(['node', 'test', 'info', 'a', '--NONSENSE']); | ||
} catch (err) { | ||
caughtErr = err; | ||
} | ||
expect(caughtErr.code).toBe('commander.unknownOption'); | ||
}); | ||
|
||
test('when specify unknown option with program and action handler then error', () => { | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.arguments('[file]') | ||
.action(() => {}); | ||
|
||
let caughtErr; | ||
try { | ||
program.parse(['node', 'test', '--NONSENSE']); | ||
} catch (err) { | ||
caughtErr = err; | ||
} | ||
expect(caughtErr.code).toBe('commander.unknownOption'); | ||
}); | ||
|
||
test('when specify unknown option with program argument and action handler then error', () => { | ||
// Regression test from #965 | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.arguments('[file]') | ||
.action(() => {}); | ||
|
||
let caughtErr; | ||
try { | ||
program.parse(['node', 'test', 'info', 'a', '--NONSENSE']); | ||
} catch (err) { | ||
caughtErr = err; | ||
} | ||
expect(caughtErr.code).toBe('commander.unknownOption'); | ||
}); | ||
}); |