-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is the best way to unit test a commander cli? #1565
Comments
A couple of tips: Have a function that creates a new If your tests include error cases detected by Commander, pass in some configuration when creating the command to make it easier to catch errors ( |
@shadowspawn do you have some examples to share, will be really helpful? |
Example of a function to create the "program", with some extra options for using in unit tests: I am not claiming this is the best way, just what I used! TypeScript example: // ------------------------------------------------------------------------------
// Command line processing. Returning new object to allow multiple calls for testing.
export function makeProgram(options?: { exitOverride?: boolean, suppressOutput?: boolean }): Command {
const program = new commander.Command();
// Configuration
if (options?.exitOverride) {
program.exitOverride();
}
if (options?.suppressOutput) {
program.configureOutput({
writeOut: () => {},
writeErr: () => {}
});
}
... |
Thank you. |
following example from tj/commander.js#1565 (comment)
I would like to cover my cli (commander.js) with unit test.
What is the best way to do that?
The text was updated successfully, but these errors were encountered: