Skip to content

Commit

Permalink
fix(serve): supplying help or version as an arg should throw error (#…
Browse files Browse the repository at this point in the history
…1694)

* fix: serve recognizes color flag

* chore: migrate to colorette

* tests: add test case to ensure that help arg recognizes subcommands

* fix: throw error if help or version is supplied as an arg
  • Loading branch information
jamesgeorge007 authored Aug 7, 2020
1 parent 02ffa05 commit 6eb7883
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/serve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export default function serve(...args: string[]): void {
parsedWebpackArgs.unknownArgs
.filter((e) => e)
.forEach((unknown) => {
logger.warn('Unknown argument:', unknown);
logger.error('Unknown argument:', unknown);
});
return;
process.exit(2);
}

cli.getCompiler(webpackArgs, core).then((compiler): void => {
Expand Down
11 changes: 11 additions & 0 deletions test/help/help-commands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ const { run } = require('../utils/test-utils');
const helpHeader = 'The build tool for modern web applications';

describe('commands help', () => {
it('throws error if supplied as an argument for subcommands', () => {
const { stderr } = run(__dirname, ['serve', 'help'], false);
expect(stderr).toContain('Unknown argument: help');
});

it('shows help information with subcommands as an arg', () => {
const { stdout, stderr } = run(__dirname, ['help', 'serve'], false);
expect(stdout).toContain('webpack s | serve');
expect(stderr).toHaveLength(0);
});

it('throws error for invalid command with --help flag', () => {
const { stderr } = run(__dirname, ['--help', 'myCommand'], false);
expect(stderr).toContain(`You provided an invalid command 'myCommand'`);
Expand Down
12 changes: 12 additions & 0 deletions test/serve/basic/serve-basic.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
'use strict';

const { yellow, options } = require('colorette');
const path = require('path');
const getPort = require('get-port');
const { runServe } = require('../../utils/test-utils');

const testPath = path.resolve(__dirname);

const usageText = 'webpack s | serve';
const descriptionText = 'Run the webpack Dev Server';

describe('basic serve usage', () => {
let port;

Expand All @@ -23,6 +27,14 @@ describe('basic serve usage', () => {
console.warn('TODO: fix `serve` test on windows');
});
} else {
it('should respect the --no-color flag', async () => {
const { stdout, stderr } = await runServe(['--help', '--no-color'], __dirname);
options.enabled = true;
expect(stdout).not.toContain(yellow(usageText));
expect(stdout).toContain(descriptionText);
expect(stderr).toHaveLength(0);
});

it('should not invoke info subcommand', async () => {
const { stdout, stderr } = await runServe(['--client-log-level', 'info'], testPath);
expect(stdout).toContain('main.js');
Expand Down

0 comments on commit 6eb7883

Please sign in to comment.