Skip to content

Commit

Permalink
test: add
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Jul 14, 2021
1 parent 3cc424b commit d303b5a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -1066,7 +1066,7 @@ class WebpackCLI {
"Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands.",
);

const outputHelp = async (options, isVerbose, isHelpCommandSyntax, program) => {
this.outputHelp = async (options, isVerbose, isHelpCommandSyntax, program) => {
const { bold } = this.utils.colors;

const outputIncorrectUsageOfHelp = () => {
Expand Down Expand Up @@ -1461,7 +1461,7 @@ class WebpackCLI {
: [],
);

await outputHelp(optionsForHelp, isVerbose, isHelpCommandSyntax, program);
await this.outputHelp(optionsForHelp, isVerbose, isHelpCommandSyntax, program);
}

const isVersionOption = typeof options.version !== "undefined";
Expand Down
30 changes: 30 additions & 0 deletions test/api/CLI.test.js
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
@@ -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
@@ -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 d303b5a

Please sign in to comment.