Skip to content

Commit

Permalink
fix: show default value in help output if available (#2814)
Browse files Browse the repository at this point in the history
* fix: show default value in help output if available

* test: add
  • Loading branch information
snitin315 authored Jul 15, 2021
1 parent 0d8d832 commit 7f50948
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ class WebpackCLI {
this.logger.raw(`${bold("Description:")} ${option.description}`);
}

if (!option.negate && options.defaultValue) {
if (!option.negate && option.defaultValue) {
this.logger.raw(
`${bold("Default value:")} ${JSON.stringify(option.defaultValue)}`,
);
Expand Down
30 changes: 30 additions & 0 deletions test/api/CLI.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1699,4 +1699,34 @@ describe("CLI API", () => {
expect(command.helpInformation()).toContain("--no-boolean Negated description");
});
});

describe("custom help output", () => {
let consoleSpy;
let exitSpy;

beforeEach(async () => {
consoleSpy = jest.spyOn(global.console, "log");
exitSpy = jest.spyOn(process, "exit").mockImplementation(() => {});

cli.program.option("--color [value]", "any color", "blue");
await new Promise((resolve, reject) => {
try {
cli.run(["help", "--color"], { from: "user" });
resolve();
} catch (error) {
reject(error);
}
});
});

afterEach(async () => {
consoleSpy.mockRestore();
exitSpy.mockRestore();
});

it("should display help information", () => {
expect(exitSpy).toHaveBeenCalledWith(0);
expect(consoleSpy.mock.calls).toMatchSnapshot();
});
});
});
31 changes: 31 additions & 0 deletions test/api/__snapshots__/CLI.test.js.snap.webpack4
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CLI API custom help output should display help information 1`] = `
Array [
Array [
"Usage: webpack --color [value]",
],
Array [
"Description: any color",
],
Array [
"Default value: \\"blue\\"",
],
Array [
"",
],
Array [
"To see list of all supported commands and options run 'webpack --help=verbose'.
",
],
Array [
"Webpack documentation: https://webpack.js.org/.",
],
Array [
"CLI documentation: https://webpack.js.org/api/cli/.",
],
Array [
"Made with ♥ by the webpack team.",
],
]
`;
31 changes: 31 additions & 0 deletions test/api/__snapshots__/CLI.test.js.snap.webpack5
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CLI API custom help output should display help information 1`] = `
Array [
Array [
"Usage: webpack --color [value]",
],
Array [
"Description: any color",
],
Array [
"Default value: \\"blue\\"",
],
Array [
"",
],
Array [
"To see list of all supported commands and options run 'webpack --help=verbose'.
",
],
Array [
"Webpack documentation: https://webpack.js.org/.",
],
Array [
"CLI documentation: https://webpack.js.org/api/cli/.",
],
Array [
"Made with ♥ by the webpack team.",
],
]
`;

0 comments on commit 7f50948

Please sign in to comment.