Skip to content

Commit

Permalink
test: code coverage (#2609)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito authored Apr 12, 2021
1 parent debe93b commit 7c2f5e7
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 4 deletions.
207 changes: 203 additions & 4 deletions test/api/CLI.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('CLI API', () => {
command.parseAsync(['--no-boolean'], { from: 'user' });
});

it('should make command with configs option', async (done) => {
it('should make command with configs boolean option', async (done) => {
cli.program.commands = [];

const command = await cli.makeCommand(
Expand All @@ -101,7 +101,7 @@ describe('CLI API', () => {
},
[
{
name: 'boolean',
name: 'configs-boolean',
configs: [
{
type: 'boolean',
Expand All @@ -111,13 +111,212 @@ describe('CLI API', () => {
},
],
(options) => {
expect(options).toEqual({ boolean: false });
expect(options).toEqual({ configsBoolean: false });

done();
},
);

command.parseAsync(['--no-boolean'], { from: 'user' });
command.parseAsync(['--no-configs-boolean'], { from: 'user' });
});

it('should make command with configs number option', async (done) => {
cli.program.commands = [];

const command = await cli.makeCommand(
{
name: 'command',
},
[
{
name: 'configs-number',
configs: [
{
type: 'number',
},
],
description: 'description',
},
],
(options) => {
expect(options).toEqual({ configsNumber: 42 });

done();
},
);

command.parseAsync(['--configs-number', '42'], { from: 'user' });
});

it('should make command with configs string option', async (done) => {
cli.program.commands = [];

const command = await cli.makeCommand(
{
name: 'command',
},
[
{
name: 'configs-string',
configs: [
{
type: 'string',
},
],
description: 'description',
},
],
(options) => {
expect(options).toEqual({ configsString: 'foo' });

done();
},
);

command.parseAsync(['--configs-string', 'foo'], { from: 'user' });
});

it('should make command with configs path option', async (done) => {
cli.program.commands = [];

const command = await cli.makeCommand(
{
name: 'command',
},
[
{
name: 'configs-path',
configs: [
{
type: 'path',
},
],
description: 'description',
},
],
(options) => {
expect(options).toEqual({ configsPath: '/root/foo' });

done();
},
);

command.parseAsync(['--configs-path', '/root/foo'], { from: 'user' });
});

it('should make command with configs RegExp option', async (done) => {
cli.program.commands = [];

const command = await cli.makeCommand(
{
name: 'command',
},
[
{
name: 'configs-regexp',
configs: [
{
type: 'RegExp',
},
],
description: 'description',
},
],
(options) => {
expect(options).toEqual({ configsRegexp: '\\w+' });

done();
},
);

command.parseAsync(['--configs-regexp', '\\w+'], { from: 'user' });
});

it('should make command with configs enum/string option', async (done) => {
cli.program.commands = [];

const command = await cli.makeCommand(
{
name: 'command',
},
[
{
name: 'enum-string',
configs: [
{
type: 'enum',
values: ['foo'],
},
],
description: 'description',
},
],
(options) => {
expect(options).toEqual({ enumString: 'foo' });

done();
},
);

command.parseAsync(['--enum-string', 'foo'], { from: 'user' });
});

it('should make command with configs enum/number option', async (done) => {
cli.program.commands = [];

const command = await cli.makeCommand(
{
name: 'command',
},
[
{
name: 'enum-number',
configs: [
{
type: 'enum',
values: [42],
},
],
description: 'description',
},
],
(options) => {
expect(options).toEqual({ enumNumber: 42 });

done();
},
);

command.parseAsync(['--enum-number', '42'], { from: 'user' });
});

it('should make command with configs enum/boolean option', async (done) => {
cli.program.commands = [];

const command = await cli.makeCommand(
{
name: 'command',
},
[
{
name: 'enum-boolean',
configs: [
{
type: 'boolean',
values: [false],
},
],
description: 'description',
},
],
(options) => {
expect(options).toEqual({ enumBoolean: false });

done();
},
);

command.parseAsync(['--no-enum-boolean'], { from: 'user' });
});

it('should make command with Boolean option and negative value #2', async (done) => {
Expand Down
9 changes: 9 additions & 0 deletions test/build/unknown/unknown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ describe('unknown behaviour', () => {
expect(stdout).toBeFalsy();
});

it('should log an error if an unknown flag is passed and includes =', async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ['--unknown=foo']);

expect(exitCode).toBe(2);
expect(stderr).toContain("Error: Unknown option '--unknown=foo'");
expect(stderr).toContain("Run 'webpack --help' to see available commands and options");
expect(stdout).toBeFalsy();
});

it('should log an error if an unknown flag is passed #3', async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ['-u', '--unknown']);

Expand Down
25 changes: 25 additions & 0 deletions test/serve/basic/same-ports-dev-serever.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = [
{
name: 'one',
mode: 'development',
devtool: false,
output: {
filename: 'first-output/[name].js',
},
devServer: {
port: 8081,
},
},
{
name: 'two',
mode: 'development',
devtool: false,
entry: './src/other.js',
output: {
filename: 'second-output/[name].js',
},
devServer: {
port: 8081,
},
},
];
15 changes: 15 additions & 0 deletions test/serve/basic/serve-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,19 @@ describe('basic serve usage', () => {
expect(stderr).toContain("Error: Unknown option '--unknown-flag'");
expect(stdout).toBeFalsy();
});

it('should work with the "stats" option in config', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['serve', '--config', 'stats.config.js'], {}, /Compiled successfully/);

expect(stderr).toBeFalsy();
expect(stdout).toContain('Compiled successfully');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});

it('should throw error when same ports in multicompiler', async () => {
const { stderr, stdout } = await runWatch(__dirname, ['serve', '--config', 'same-ports-dev-serever.config.js']);

expect(stdout).toBeFalsy();
expect(stderr).toContain('Unique ports must be specified for each devServer option in your webpack configuration');
});
});
7 changes: 7 additions & 0 deletions test/serve/basic/stats.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
mode: 'development',
devtool: false,
devServer: {
stats: 'minimal',
},
};
16 changes: 16 additions & 0 deletions test/serve/invalid-schema/invalid-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,20 @@ describe('invalid schema', () => {

expect(stdout).toBeFalsy();
});

it('should log webpack-dev-server error and exit process on invalid flag', async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ['serve', '--port', '-1']);

expect(exitCode).toEqual(2);
expect(stderr).toContain('RangeError');
expect(stdout).toBeFalsy();
});

it('should log webpack-dev-server error and exit process on invalid config', async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ['serve', '--config', './webpack-dev-server.config.mock.js']);

expect(exitCode).toEqual(2);
expect(stderr).toContain('webpack Dev Server Invalid Options');
expect(stdout).toBeFalsy();
});
});
6 changes: 6 additions & 0 deletions test/serve/invalid-schema/webpack-dev-server.config.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
mode: 'development',
devServer: {
bonjour: '',
},
};

0 comments on commit 7c2f5e7

Please sign in to comment.