-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not mutate program.args when calling action handler (#1048)
- Loading branch information
1 parent
5cac9d6
commit cc4a525
Showing
2 changed files
with
44 additions
and
8 deletions.
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,36 @@ | ||
const commander = require('../'); | ||
|
||
// Test some behaviours of .action not covered in more specific tests. | ||
|
||
test('when .action called then command passed to action', () => { | ||
const actionMock = jest.fn(); | ||
const program = new commander.Command(); | ||
const cmd = program | ||
.command('info') | ||
.action(actionMock); | ||
program.parse(['node', 'test', 'info']); | ||
expect(actionMock).toHaveBeenCalledWith(cmd); | ||
}); | ||
|
||
test('when .action called then program.args only contains args', () => { | ||
// At one time program.args was being modified to contain the same args as the call to .action | ||
// and so included the command as an extra and unexpected complex item in array. | ||
const program = new commander.Command(); | ||
program | ||
.command('info <file>') | ||
.action(() => {}); | ||
program.parse(['node', 'test', 'info', 'my-file']); | ||
expect(program.args).toEqual(['my-file']); | ||
}); | ||
|
||
test('when .action called with extra arguments then extras also passed to action', () => { | ||
// This is a new and undocumented behaviour for now. | ||
// Might make this an error by default in future. | ||
const actionMock = jest.fn(); | ||
const program = new commander.Command(); | ||
const cmd = program | ||
.command('info <file>') | ||
.action(actionMock); | ||
program.parse(['node', 'test', 'info', 'my-file', 'a']); | ||
expect(actionMock).toHaveBeenCalledWith('my-file', cmd, ['a']); | ||
}); |