CommandTestFactory - Passing options (flags) returns wrong values
See original GitHub issueIt might be a lack of documentation, but I can’t understand how CommandTestFactory
would want me to pass option flags to my command. Imagine the following command class (simplified for ease of reading):
@Command({
name: 'generate',
description: 'Generate a new project',
})
export class GenerateCommand {
public async run(input: string[], options: GenerateCommandOptions): Promise<void> {
const { name, description, ...requirements } = await this.promptForMissingOptions('generate', options);
await this.generate(new ProjectSchema(name, description, requirements));
}
@Option({ flags: '-o, --open-api' })
public openApi(openApi: string): boolean {
return openApi === 'true';
}
@Option({ flags: '-r, --rabbit' })
public rabbitMqClient(rabbitMqClient: string): boolean {
return rabbitMqClient === 'true';
}
@Option({ flags: '-g, --originator' })
public originator(originator: string): boolean {
return originator === 'true';
}
}
If I test this command with the following:
it(`creates a schema and generates the project`, async () => {
await CommandTestFactory.run(commandInstance, [
'generate',
'--name',
'Foo',
'--description',
'Desc',
'--open-api',
'false',
'--rabbit',
'true',
'--originator',
'false',
]);
});
It does not work, my command’s .run()
method will receive ['false', 'false', 'false']
as its input
parameter, like they are regular arguments, not options and the rabbit
option will still be false
even though I would assume it should be true
with the above.
I’m really not sure what’s going on here, any help would be greatly appreciated 😃
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
nest-commander/README.md at main - GitHub
The run command is where you can kick all of your logic off from, it will take in whatever parameters did not match...
Read more >How to unset flags Visited on command line in GoLang for Tests
I want this test to have no command line flags passed. The flags. Visit function should not be able to get true for...
Read more >Nest Commander | NestJS - A progressive Node.js framework
The run command is where you can kick all of your logic off from, it will take in whatever parameters did not match...
Read more >A Module for using NestJS to Build Up CLI Apps - Morioh
The run command is where you can kick all of your logic off from, it will take in whatever parameters did not match...
Read more >Nest-commander NPM
Options, CommandOptions, false, Extra options to pass on down to commander ... If you don't need any filtering done, simply return the value...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For anyone stumbling upon this question and struggling with boolean flags like I was, here’s a small example of what works (including negative flags). No need for
[]
or<>
:With this, your command will prompt the user if no flag is given, but will not prompt if
-r
,--rabbit-mq-client
or--no-rabbit-mq-client
is given, with the first two flags settingrabbitMqClient
totrue
and the last one tofalse
.Hope it can help someone 😃
I don’t see this explicitly stated in commander’s docs, but I can see that any option that is hyphenated, that does not have
no-
(i.e.--no-rabbit
) does have either[]
or<>
wrapping around the option name. I’ll take a look at the PR and see what you’ve got in addition. Thanks for opening one up!