From 7c2f5e7e32c9ad4200a8071b7f8d05ed118624d7 Mon Sep 17 00:00:00 2001 From: Alexey Lavinsky Date: Mon, 12 Apr 2021 07:10:09 -0700 Subject: [PATCH] test: code coverage (#2609) --- test/api/CLI.test.js | 207 +++++++++++++++++- test/build/unknown/unknown.test.js | 9 + .../basic/same-ports-dev-serever.config.js | 25 +++ test/serve/basic/serve-basic.test.js | 15 ++ test/serve/basic/stats.config.js | 7 + .../invalid-schema/invalid-schema.test.js | 16 ++ .../webpack-dev-server.config.mock.js | 6 + 7 files changed, 281 insertions(+), 4 deletions(-) create mode 100644 test/serve/basic/same-ports-dev-serever.config.js create mode 100644 test/serve/basic/stats.config.js create mode 100644 test/serve/invalid-schema/webpack-dev-server.config.mock.js diff --git a/test/api/CLI.test.js b/test/api/CLI.test.js index 336d97138c3..7c480374b84 100644 --- a/test/api/CLI.test.js +++ b/test/api/CLI.test.js @@ -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( @@ -101,7 +101,7 @@ describe('CLI API', () => { }, [ { - name: 'boolean', + name: 'configs-boolean', configs: [ { type: 'boolean', @@ -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) => { diff --git a/test/build/unknown/unknown.test.js b/test/build/unknown/unknown.test.js index a4896531460..b30d8d7fa2f 100644 --- a/test/build/unknown/unknown.test.js +++ b/test/build/unknown/unknown.test.js @@ -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']); diff --git a/test/serve/basic/same-ports-dev-serever.config.js b/test/serve/basic/same-ports-dev-serever.config.js new file mode 100644 index 00000000000..d85e05a7341 --- /dev/null +++ b/test/serve/basic/same-ports-dev-serever.config.js @@ -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, + }, + }, +]; diff --git a/test/serve/basic/serve-basic.test.js b/test/serve/basic/serve-basic.test.js index 53880fc7921..f7528ab1c9f 100644 --- a/test/serve/basic/serve-basic.test.js +++ b/test/serve/basic/serve-basic.test.js @@ -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'); + }); }); diff --git a/test/serve/basic/stats.config.js b/test/serve/basic/stats.config.js new file mode 100644 index 00000000000..9d27015e4cc --- /dev/null +++ b/test/serve/basic/stats.config.js @@ -0,0 +1,7 @@ +module.exports = { + mode: 'development', + devtool: false, + devServer: { + stats: 'minimal', + }, +}; diff --git a/test/serve/invalid-schema/invalid-schema.test.js b/test/serve/invalid-schema/invalid-schema.test.js index 90e29bf4b44..a4a4e4c4dd5 100644 --- a/test/serve/invalid-schema/invalid-schema.test.js +++ b/test/serve/invalid-schema/invalid-schema.test.js @@ -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(); + }); }); diff --git a/test/serve/invalid-schema/webpack-dev-server.config.mock.js b/test/serve/invalid-schema/webpack-dev-server.config.mock.js new file mode 100644 index 00000000000..37e88e65591 --- /dev/null +++ b/test/serve/invalid-schema/webpack-dev-server.config.mock.js @@ -0,0 +1,6 @@ +module.exports = { + mode: 'development', + devServer: { + bonjour: '', + }, +};