From 8c63e4d23857bba8966eee071c2ecf93ba2dfbe0 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Thu, 21 Jan 2021 19:57:17 +0300 Subject: [PATCH] tests: fix --- packages/serve/src/index.ts | 2 +- packages/webpack-cli/lib/webpack-cli.js | 2 +- test/build/basic/basic.test.js | 16 +++++++++++ test/build/basic/entry.config.js | 4 +++ test/build/basic/src/again.js | 1 + test/build/basic/src/entry.js | 1 + test/serve/basic/serve-basic.test.js | 36 ++++++++++++------------- test/serve/basic/serve.config.js | 7 +++++ test/serve/basic/src/entry.js | 1 + test/watch/basic/basic.test.js | 35 ++++++++++++++++++++++++ test/watch/basic/src/entry.js | 1 + 11 files changed, 86 insertions(+), 20 deletions(-) create mode 100644 test/build/basic/entry.config.js create mode 100644 test/build/basic/src/again.js create mode 100644 test/build/basic/src/entry.js create mode 100644 test/serve/basic/serve.config.js create mode 100644 test/serve/basic/src/entry.js create mode 100644 test/watch/basic/src/entry.js diff --git a/packages/serve/src/index.ts b/packages/serve/src/index.ts index 440bbe92b7c..bcc360714af 100644 --- a/packages/serve/src/index.ts +++ b/packages/serve/src/index.ts @@ -73,7 +73,7 @@ class ServeCommand { } if (entries.length > 0) { - webpackOptions.entry = entries; + webpackOptions.entry = [...entries, ...(webpackOptions.entry || [])]; } webpackOptions.argv = { ...options, env: { WEBPACK_SERVE: true, ...options.env } }; diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 2cc4d7ab7e3..542ae801455 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -343,7 +343,7 @@ class WebpackCLI { this.getBuiltInOptions(), async (entries, options) => { if (entries.length > 0) { - options.entry = entries; + options.entry = [...entries, ...(options.entry || [])]; } if (isWatchCommandUsed) { diff --git a/test/build/basic/basic.test.js b/test/build/basic/basic.test.js index 815941e0a57..d9ccbdca877 100644 --- a/test/build/basic/basic.test.js +++ b/test/build/basic/basic.test.js @@ -43,6 +43,22 @@ describe('bundle command', () => { expect(stdout).toBeTruthy(); }); + it('should work with multiple entries syntax without command with options #3 (default command)', async () => { + const { exitCode, stderr, stdout } = run(__dirname, ['./src/index.js', './src/other.js', '--entry', './src/again.js'], false); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + }); + + it('should work with and override entries from the configuration', async () => { + const { exitCode, stderr, stdout } = run(__dirname, ['./src/index.js', './src/other.js', '--config', './entry.config.js'], false); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + }); + it('should work with the "build" alias', async () => { const { exitCode, stderr, stdout } = run(__dirname, ['build'], false); diff --git a/test/build/basic/entry.config.js b/test/build/basic/entry.config.js new file mode 100644 index 00000000000..431945f5225 --- /dev/null +++ b/test/build/basic/entry.config.js @@ -0,0 +1,4 @@ +module.exports = { + mode: 'development', + entry: './src/entry.js', +}; diff --git a/test/build/basic/src/again.js b/test/build/basic/src/again.js new file mode 100644 index 00000000000..eec159b09dc --- /dev/null +++ b/test/build/basic/src/again.js @@ -0,0 +1 @@ +console.log('again'); diff --git a/test/build/basic/src/entry.js b/test/build/basic/src/entry.js new file mode 100644 index 00000000000..6daf0ddddd1 --- /dev/null +++ b/test/build/basic/src/entry.js @@ -0,0 +1 @@ +console.log('CONFIG'); diff --git a/test/serve/basic/serve-basic.test.js b/test/serve/basic/serve-basic.test.js index 3ad7aa8def4..c8feaae2326 100644 --- a/test/serve/basic/serve-basic.test.js +++ b/test/serve/basic/serve-basic.test.js @@ -28,7 +28,7 @@ describe('basic serve usage', () => { } it('should work', async () => { - const { stderr, stdout } = await runServe(__dirname, ['']); + const { stderr, stdout } = await runServe(__dirname, []); expect(stderr).toBeFalsy(); expect(stdout).toContain('main.js'); @@ -36,7 +36,7 @@ describe('basic serve usage', () => { }); it('should work with the "--config" option', async () => { - const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'webpack.config.js', '--port', port]); + const { stderr, stdout } = await runServe(__dirname, ['--config', 'serve.config.js', '--port', port]); expect(stderr).toBeFalsy(); expect(stdout).toContain('development'); @@ -45,7 +45,6 @@ describe('basic serve usage', () => { it('should work with the "--config" and "--env" options', async () => { const { stderr, stdout } = await runServe(__dirname, [ - 'serve', '--config', 'function-with-env.config.js', '--env', @@ -63,7 +62,6 @@ describe('basic serve usage', () => { it('should work with the "--config" and "--env" options and expose dev server options', async () => { const { stderr, stdout } = await runServe(__dirname, [ - 'serve', '--config', 'function-with-argv.config.js', '--env', @@ -82,7 +80,7 @@ describe('basic serve usage', () => { }); it('should work in multi compiler mode', async () => { - const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'multi.config.js', '--port', port]); + const { stderr, stdout } = await runServe(__dirname, ['--config', 'multi.config.js', '--port', port]); expect(stderr).toBeFalsy(); expect(stdout).toContain('one'); @@ -94,7 +92,7 @@ describe('basic serve usage', () => { // TODO need fix in future, edge case it.skip('should work in multi compiler mode with multiple dev servers', async () => { - const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'multi-dev-server.config.js']); + const { stderr, stdout } = await runServe(__dirname, ['--config', 'multi-dev-server.config.js']); expect(stderr).toBeFalsy(); expect(stdout).toContain('one'); @@ -228,7 +226,7 @@ describe('basic serve usage', () => { }); it('should work with the default "publicPath" option', async () => { - const { stderr, stdout } = await runServe(__dirname, ['serve']); + const { stderr, stdout } = await runServe(__dirname, []); expect(stderr).toBeFalsy(); expect(stdout).toContain('main.js'); @@ -237,7 +235,7 @@ describe('basic serve usage', () => { }); it('should work with the "--output-public-path" option', async () => { - const { stderr, stdout } = await runServe(__dirname, ['serve', '--output-public-path', '/my-public-path/']); + const { stderr, stdout } = await runServe(__dirname, ['--output-public-path', '/my-public-path/']); if (isWebpack5) { expect(stderr).toBeFalsy(); @@ -251,7 +249,7 @@ describe('basic serve usage', () => { }); it('should respect the "publicPath" option from configuration', async () => { - const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'output-public-path.config.js']); + const { stderr, stdout } = await runServe(__dirname, ['--config', 'output-public-path.config.js']); expect(stderr).toBeFalsy(); expect(stdout).toContain('main.js'); @@ -260,7 +258,7 @@ describe('basic serve usage', () => { }); it('should respect the "publicPath" option from configuration using multi compiler mode', async () => { - const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'multi-output-public-path.config.js', '--port', port]); + const { stderr, stdout } = await runServe(__dirname, ['--config', 'multi-output-public-path.config.js', '--port', port]); expect(stderr).toBeFalsy(); expect(stdout).toContain('one'); @@ -272,7 +270,7 @@ describe('basic serve usage', () => { }); it('should respect the "publicPath" option from configuration (from the "devServer" options)', async () => { - const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'dev-server-output-public-path.config.js']); + const { stderr, stdout } = await runServe(__dirname, ['--config', 'dev-server-output-public-path.config.js']); expect(stderr).toBeFalsy(); expect(stdout).toContain('main.js'); @@ -289,13 +287,7 @@ describe('basic serve usage', () => { }); it('should respect the "publicPath" option from configuration using multi compiler mode (from the "devServer" options)', async () => { - const { stderr, stdout } = await runServe(__dirname, [ - 'serve', - '--config', - 'multi-dev-server-output-public-path.config.js', - '--port', - port, - ]); + const { stderr, stdout } = await runServe(__dirname, ['--config', 'multi-dev-server-output-public-path.config.js', '--port', port]); expect(stderr).toBeFalsy(); expect(stderr).toBeFalsy(); @@ -307,6 +299,14 @@ describe('basic serve usage', () => { expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull(); }); + it('should work with entries syntax', async () => { + const { stderr, stdout } = await runServe(__dirname, ['./src/entry.js', '--port', port]); + + expect(stderr).toBeFalsy(); + expect(stdout).toContain('development'); + expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull(); + }); + it('should log and error on unknown flag', async () => { const { exitCode, stdout, stderr } = await runServe(testPath, ['--port', port, '--unknown-flag']); diff --git a/test/serve/basic/serve.config.js b/test/serve/basic/serve.config.js new file mode 100644 index 00000000000..cef4d803dc3 --- /dev/null +++ b/test/serve/basic/serve.config.js @@ -0,0 +1,7 @@ +const WebpackCLITestPlugin = require('../../utils/webpack-cli-test-plugin'); + +module.exports = { + mode: 'development', + devtool: false, + plugins: [new WebpackCLITestPlugin(['mode'], false, 'hooks.compilation.taps')], +}; diff --git a/test/serve/basic/src/entry.js b/test/serve/basic/src/entry.js new file mode 100644 index 00000000000..a136806e8f1 --- /dev/null +++ b/test/serve/basic/src/entry.js @@ -0,0 +1 @@ +console.log('Entry'); diff --git a/test/watch/basic/basic.test.js b/test/watch/basic/basic.test.js index 34fee148278..0d24c6dda3a 100644 --- a/test/watch/basic/basic.test.js +++ b/test/watch/basic/basic.test.js @@ -83,6 +83,41 @@ describe('basic', () => { }); }); + it('should recompile upon file change using the `watch` command and entries syntax', (done) => { + const proc = runAndGetWatchProc(__dirname, ['watch', './src/entry.js', '--mode', 'development'], false, '', true); + + let modified = false; + + const wordsInStatsv5Entries = ['asset', 'entry.js', 'compiled successfully']; + + proc.stdout.on('data', (chunk) => { + const data = stripAnsi(chunk.toString()); + + if (data.includes('entry.js')) { + if (isWebpack5) { + for (const word of wordsInStatsv5Entries) { + expect(data).toContain(word); + } + } else { + for (const word of wordsInStatsv4) { + expect(data).toContain(word); + } + } + + if (!modified) { + process.nextTick(() => { + writeFileSync(resolve(__dirname, './src/entry.js'), `console.log('watch flag test');`); + }); + + modified = true; + } else { + proc.kill(); + done(); + } + } + }); + }); + it('should recompile upon file change using the `command` option and the `--watch` option and log warning', (done) => { const proc = runAndGetWatchProc(__dirname, ['watch', '--watch', '--mode', 'development'], false, '', true); diff --git a/test/watch/basic/src/entry.js b/test/watch/basic/src/entry.js new file mode 100644 index 00000000000..1d8734ee1c8 --- /dev/null +++ b/test/watch/basic/src/entry.js @@ -0,0 +1 @@ +console.log('watch flag test');