From 04c80df41d498d4404cf8886118e20bb97ed5aab Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Fri, 30 Apr 2021 02:34:18 +0530 Subject: [PATCH 1/2] test: watch option snapshot --- test/utils/test-utils.js | 13 +++ .../analyze-flag.test.js.snap.webpack4 | 9 ++ .../analyze-flag.test.js.snap.webpack5 | 9 ++ test/watch/analyze/analyze-flag.test.js | 6 +- .../__snapshots__/bail.test.js.snap.webpack4 | 89 +++++++++++++++++++ .../__snapshots__/bail.test.js.snap.webpack5 | 55 ++++++++++++ test/watch/bail/bail.test.js | 32 +++---- .../__snapshots__/basic.test.js.snap.webpack4 | 39 ++++++++ .../__snapshots__/basic.test.js.snap.webpack5 | 36 ++++++++ test/watch/basic/basic.test.js | 28 +++--- .../stats-and-watch.test.js.snap.webpack4 | 13 +++ .../stats-and-watch.test.js.snap.webpack5 | 17 ++++ test/watch/stats/stats-and-watch.test.js | 14 +-- 13 files changed, 318 insertions(+), 42 deletions(-) create mode 100644 test/watch/analyze/__snapshots__/analyze-flag.test.js.snap.webpack4 create mode 100644 test/watch/analyze/__snapshots__/analyze-flag.test.js.snap.webpack5 create mode 100644 test/watch/bail/__snapshots__/bail.test.js.snap.webpack4 create mode 100644 test/watch/bail/__snapshots__/bail.test.js.snap.webpack5 create mode 100644 test/watch/basic/__snapshots__/basic.test.js.snap.webpack4 create mode 100644 test/watch/basic/__snapshots__/basic.test.js.snap.webpack5 create mode 100644 test/watch/stats/__snapshots__/stats-and-watch.test.js.snap.webpack4 create mode 100644 test/watch/stats/__snapshots__/stats-and-watch.test.js.snap.webpack5 diff --git a/test/utils/test-utils.js b/test/utils/test-utils.js index ed707c23691..96d5c28f401 100644 --- a/test/utils/test-utils.js +++ b/test/utils/test-utils.js @@ -251,6 +251,17 @@ const normalizeError = (output) => { .replace(/\s+at .+(}|\)|\d)/gs, "\n at stack"); }; +const normalizeCompileTime = (output) => { + return output.replace(/in \d+ ms/gm, "in ms").replace(); +}; + +const normalizeV4Output = (output) => { + return output + .replace(/Hash: .*/gm, "Hash: ") + .replace(/Time: .*/gm, "Time: ") + .replace(/Built at: .*/gm, "Built at: "); +}; + const normalizeStdout = (stdout) => { if (typeof stdout !== "string") { return stdout; @@ -264,6 +275,8 @@ const normalizeStdout = (stdout) => { normalizedStdout = normalizeCwd(normalizedStdout); normalizedStdout = normalizeVersions(normalizedStdout); normalizedStdout = normalizeError(normalizedStdout); + normalizedStdout = normalizeCompileTime(normalizedStdout); + normalizedStdout = normalizeV4Output(normalizedStdout); return normalizedStdout; }; diff --git a/test/watch/analyze/__snapshots__/analyze-flag.test.js.snap.webpack4 b/test/watch/analyze/__snapshots__/analyze-flag.test.js.snap.webpack4 new file mode 100644 index 00000000000..58e6c254071 --- /dev/null +++ b/test/watch/analyze/__snapshots__/analyze-flag.test.js.snap.webpack4 @@ -0,0 +1,9 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`"analyze" option should load webpack-bundle-analyzer plugin with --analyze flag: stderr 1`] = `""`; + +exports[`"analyze" option should load webpack-bundle-analyzer plugin with --analyze flag: stdout 1`] = ` +"Webpack Bundle Analyzer is started + at stack +Use Ctrl+C to close it" +`; diff --git a/test/watch/analyze/__snapshots__/analyze-flag.test.js.snap.webpack5 b/test/watch/analyze/__snapshots__/analyze-flag.test.js.snap.webpack5 new file mode 100644 index 00000000000..58e6c254071 --- /dev/null +++ b/test/watch/analyze/__snapshots__/analyze-flag.test.js.snap.webpack5 @@ -0,0 +1,9 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`"analyze" option should load webpack-bundle-analyzer plugin with --analyze flag: stderr 1`] = `""`; + +exports[`"analyze" option should load webpack-bundle-analyzer plugin with --analyze flag: stdout 1`] = ` +"Webpack Bundle Analyzer is started + at stack +Use Ctrl+C to close it" +`; diff --git a/test/watch/analyze/analyze-flag.test.js b/test/watch/analyze/analyze-flag.test.js index cee8687346d..76bfc8d8d9b 100644 --- a/test/watch/analyze/analyze-flag.test.js +++ b/test/watch/analyze/analyze-flag.test.js @@ -1,6 +1,6 @@ "use strict"; -const { runWatch } = require("../../utils/test-utils"); +const { runWatch, normalizeStderr, normalizeStdout } = require("../../utils/test-utils"); describe('"analyze" option', () => { it("should load webpack-bundle-analyzer plugin with --analyze flag", async () => { @@ -8,7 +8,7 @@ describe('"analyze" option', () => { killString: /Webpack Bundle Analyzer is started at/, }); - expect(stderr).toBeFalsy(); - expect(stdout).toContain("Webpack Bundle Analyzer is started at"); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); }); diff --git a/test/watch/bail/__snapshots__/bail.test.js.snap.webpack4 b/test/watch/bail/__snapshots__/bail.test.js.snap.webpack4 new file mode 100644 index 00000000000..5bc295f5527 --- /dev/null +++ b/test/watch/bail/__snapshots__/bail.test.js.snap.webpack4 @@ -0,0 +1,89 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`"bail" option should log warning in case of multiple compilers: stderr 1`] = `" [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`; + +exports[`"bail" option should log warning in case of multiple compilers: stdout 1`] = ` +"Hash: +Version: webpack x.x.x +Child first: + Hash: + Time: + Built at: + Asset Size Chunks Chunk Names + ./dist-first.js 3.77 KiB main [emitted] main + Entrypoint main = ./dist-first.js + [./src/first.js] 50 bytes {main} [built] +Child second: + Hash: + Time: + Built at: + Asset Size Chunks Chunk Names + ./dist-second.js 3.77 KiB main [emitted] main + Entrypoint main = ./dist-second.js + [./src/second.js] 51 bytes {main} [built]" +`; + +exports[`"bail" option should log warning in watch mode: stderr 1`] = `" [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`; + +exports[`"bail" option should log warning in watch mode: stderr 2`] = `" [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`; + +exports[`"bail" option should log warning in watch mode: stdout 1`] = ` +"Hash: +Version: webpack x.x.x +Time: +Built at: + Asset Size Chunks Chunk Names +main.js 3.77 KiB main [emitted] main +Entrypoint main = main.js +[./src/first.js] 50 bytes {main} [built]" +`; + +exports[`"bail" option should log warning in watch mode: stdout 2`] = ` +"Hash: +Version: webpack x.x.x +Time: +Built at: + Asset Size Chunks Chunk Names +main.js 3.77 KiB main [emitted] main +Entrypoint main = main.js +[./src/first.js] 50 bytes {main} [built]" +`; + +exports[`"bail" option should not log warning in not watch mode without the "watch" option: stderr 1`] = `""`; + +exports[`"bail" option should not log warning in not watch mode without the "watch" option: stdout 1`] = ` +"Hash: +Version: webpack x.x.x +Time: +Built at: + Asset Size Chunks Chunk Names +main.js 3.77 KiB main [emitted] main +Entrypoint main = main.js +[./src/first.js] 50 bytes {main} [built]" +`; + +exports[`"bail" option should not log warning without the "bail" option: stderr 1`] = `""`; + +exports[`"bail" option should not log warning without the "bail" option: stderr 2`] = `""`; + +exports[`"bail" option should not log warning without the "bail" option: stdout 1`] = ` +"Hash: +Version: webpack x.x.x +Time: +Built at: + Asset Size Chunks Chunk Names +main.js 3.77 KiB main [emitted] main +Entrypoint main = main.js +[./src/first.js] 50 bytes {main} [built]" +`; + +exports[`"bail" option should not log warning without the "bail" option: stdout 2`] = ` +"Hash: +Version: webpack x.x.x +Time: +Built at: + Asset Size Chunks Chunk Names +main.js 3.77 KiB main [emitted] main +Entrypoint main = main.js +[./src/first.js] 50 bytes {main} [built]" +`; diff --git a/test/watch/bail/__snapshots__/bail.test.js.snap.webpack5 b/test/watch/bail/__snapshots__/bail.test.js.snap.webpack5 new file mode 100644 index 00000000000..1fd26a728da --- /dev/null +++ b/test/watch/bail/__snapshots__/bail.test.js.snap.webpack5 @@ -0,0 +1,55 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`"bail" option should log warning in case of multiple compilers: stderr 1`] = `" [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`; + +exports[`"bail" option should log warning in case of multiple compilers: stdout 1`] = ` +"first: + asset ./dist-first.js 217 bytes [emitted] (name: main) + ./src/first.js 50 bytes [built] [code generated] + first (webpack x.x.x) compiled successfully in ms + +second: + asset ./dist-second.js 221 bytes [emitted] (name: main) + ./src/second.js 51 bytes [built] [code generated] + second (webpack x.x.x) compiled successfully in ms" +`; + +exports[`"bail" option should log warning in watch mode: stderr 1`] = `" [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`; + +exports[`"bail" option should log warning in watch mode: stderr 2`] = `" [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`; + +exports[`"bail" option should log warning in watch mode: stdout 1`] = ` +"asset main.js 217 bytes [compared for emit] (name: main) +./src/first.js 50 bytes [built] [code generated] +webpack x.x.x compiled successfully in ms" +`; + +exports[`"bail" option should log warning in watch mode: stdout 2`] = ` +"asset main.js 217 bytes [compared for emit] (name: main) +./src/first.js 50 bytes [built] [code generated] +webpack x.x.x compiled successfully in ms" +`; + +exports[`"bail" option should not log warning in not watch mode without the "watch" option: stderr 1`] = `""`; + +exports[`"bail" option should not log warning in not watch mode without the "watch" option: stdout 1`] = ` +"asset main.js 217 bytes [emitted] (name: main) +./src/first.js 50 bytes [built] [code generated] +webpack x.x.x compiled successfully in ms" +`; + +exports[`"bail" option should not log warning without the "bail" option: stderr 1`] = `""`; + +exports[`"bail" option should not log warning without the "bail" option: stderr 2`] = `""`; + +exports[`"bail" option should not log warning without the "bail" option: stdout 1`] = ` +"asset main.js 217 bytes [compared for emit] (name: main) +./src/first.js 50 bytes [built] [code generated] +webpack x.x.x compiled successfully in ms" +`; + +exports[`"bail" option should not log warning without the "bail" option: stdout 2`] = ` +"asset main.js 217 bytes [compared for emit] (name: main) +./src/first.js 50 bytes [built] [code generated] +webpack x.x.x compiled successfully in ms" +`; diff --git a/test/watch/bail/bail.test.js b/test/watch/bail/bail.test.js index ba351e6c71d..22a6dae1586 100644 --- a/test/watch/bail/bail.test.js +++ b/test/watch/bail/bail.test.js @@ -1,13 +1,13 @@ "use strict"; -const { runWatch } = require("../../utils/test-utils"); +const { runWatch, normalizeStderr, normalizeStdout } = require("../../utils/test-utils"); describe('"bail" option', () => { it('should not log warning in not watch mode without the "watch" option', async () => { const { stderr, stdout } = await runWatch(__dirname, ["-c", "watch-webpack.config.js"]); - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); it('should not log warning without the "bail" option', async () => { @@ -17,8 +17,8 @@ describe('"bail" option', () => { "--watch", ]); - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); it('should not log warning without the "bail" option', async () => { @@ -28,8 +28,8 @@ describe('"bail" option', () => { "--watch", ]); - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); it("should log warning in watch mode", async () => { @@ -39,10 +39,8 @@ describe('"bail" option', () => { "--watch", ]); - expect(stderr).toContain( - `You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`, - ); - expect(stdout).toBeTruthy(); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); it("should log warning in watch mode", async () => { @@ -51,18 +49,14 @@ describe('"bail" option', () => { "bail-and-watch-webpack.config.js", ]); - expect(stderr).toContain( - `You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`, - ); - expect(stdout).toBeTruthy(); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); it("should log warning in case of multiple compilers", async () => { const { stderr, stdout } = await runWatch(__dirname, ["-c", "multi-webpack.config.js"]); - expect(stderr).toContain( - `You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.`, - ); - expect(stdout).toBeTruthy(); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); }); diff --git a/test/watch/basic/__snapshots__/basic.test.js.snap.webpack4 b/test/watch/basic/__snapshots__/basic.test.js.snap.webpack4 new file mode 100644 index 00000000000..65592723415 --- /dev/null +++ b/test/watch/basic/__snapshots__/basic.test.js.snap.webpack4 @@ -0,0 +1,39 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`basic should log supplied config with watch: stderr 1`] = ` +" [webpack-cli] Compiler starting... + [webpack-cli] Compiler is using config: '/test/watch/basic/log.config.js' + [webpack-cli] Compiler finished +" +`; + +exports[`basic should recompile upon file change using the \`command\` option and the \`--no-watch\` option and log warning: stderr 1`] = ` +"[webpack-cli] Error: Unknown option '--no-watch' +[webpack-cli] Run 'webpack --help' to see available commands and options" +`; + +exports[`basic should recompile upon file change using the \`command\` option and the \`--no-watch\` option and log warning: stdout 1`] = `""`; + +exports[`basic should recompile upon file change using the \`command\` option and the \`--watch\` option and log warning: stderr 1`] = ` +"[webpack-cli] Error: Unknown option '--watch' +[webpack-cli] Run 'webpack --help' to see available commands and options" +`; + +exports[`basic should recompile upon file change using the \`command\` option and the \`--watch\` option and log warning: stdout 1`] = `""`; + +exports[`basic should work with negative value: stderr 1`] = `""`; + +exports[`basic should work with negative value: stdout 1`] = ` +"Hash: +Version: webpack x.x.x +Time: +Built at: + Asset Size Chunks Chunk Names +main.js 960 bytes 0 [emitted] main +Entrypoint main = main.js +[0] ./src/index.js 32 bytes {0} [built] + +WARNING in configuration +The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment. +You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/" +`; diff --git a/test/watch/basic/__snapshots__/basic.test.js.snap.webpack5 b/test/watch/basic/__snapshots__/basic.test.js.snap.webpack5 new file mode 100644 index 00000000000..7a48dc3ee9a --- /dev/null +++ b/test/watch/basic/__snapshots__/basic.test.js.snap.webpack5 @@ -0,0 +1,36 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`basic should log supplied config with watch: stderr 1`] = ` +" [webpack-cli] Compiler starting... + [webpack-cli] Compiler is using config: '/test/watch/basic/log.config.js' + [webpack-cli] Compiler finished +" +`; + +exports[`basic should recompile upon file change using the \`command\` option and the \`--no-watch\` option and log warning: stderr 1`] = ` +"[webpack-cli] Error: Unknown option '--no-watch' +[webpack-cli] Run 'webpack --help' to see available commands and options" +`; + +exports[`basic should recompile upon file change using the \`command\` option and the \`--no-watch\` option and log warning: stdout 1`] = `""`; + +exports[`basic should recompile upon file change using the \`command\` option and the \`--watch\` option and log warning: stderr 1`] = ` +"[webpack-cli] Error: Unknown option '--watch' +[webpack-cli] Run 'webpack --help' to see available commands and options" +`; + +exports[`basic should recompile upon file change using the \`command\` option and the \`--watch\` option and log warning: stdout 1`] = `""`; + +exports[`basic should work with negative value: stderr 1`] = `""`; + +exports[`basic should work with negative value: stdout 1`] = ` +"asset main.js 31 bytes [emitted] [minimized] (name: main) +./src/index.js 32 bytes [built] [code generated] + +WARNING in configuration +The 'mode' option has not been set, webpack will fallback to 'production' for this value. +Set 'mode' option to 'development' or 'production' to enable defaults for each environment. +You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/ + +webpack x.x.x compiled with 1 warning in ms" +`; diff --git a/test/watch/basic/basic.test.js b/test/watch/basic/basic.test.js index a414b432f40..06f67e39dc2 100644 --- a/test/watch/basic/basic.test.js +++ b/test/watch/basic/basic.test.js @@ -1,6 +1,13 @@ "use strict"; -const { run, runAndGetProcess, isWebpack5, processKill } = require("../../utils/test-utils"); +const { + run, + runAndGetProcess, + isWebpack5, + processKill, + normalizeStderr, + normalizeStdout, +} = require("../../utils/test-utils"); const { writeFileSync } = require("fs"); const { resolve } = require("path"); @@ -16,8 +23,8 @@ describe("basic", () => { ]); expect(exitCode).toBe(0); - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); it("should recompile upon file change using the `--watch` option", (done) => { @@ -187,7 +194,6 @@ describe("basic", () => { it("should log supplied config with watch", (done) => { const proc = runAndGetProcess(__dirname, ["watch", "--config", "log.config.js"]); - const configPath = resolve(__dirname, "./log.config.js"); let stderr = ""; @@ -197,9 +203,7 @@ describe("basic", () => { stderr += data; if (/Compiler finished/.test(data)) { - expect(stderr).toContain("Compiler starting..."); - expect(stderr).toContain(`Compiler is using config: '${configPath}'`); - expect(stderr).toContain("Compiler finished"); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); processKill(proc); done(); @@ -216,9 +220,8 @@ describe("basic", () => { ]); expect(exitCode).toBe(2); - expect(stderr).toContain("Error: Unknown option '--watch'"); - expect(stderr).toContain("Run 'webpack --help' to see available commands and options"); - expect(stdout).toBeFalsy(); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); it("should recompile upon file change using the `command` option and the `--no-watch` option and log warning", async () => { @@ -230,8 +233,7 @@ describe("basic", () => { ]); expect(exitCode).toBe(2); - expect(stderr).toContain("Error: Unknown option '--no-watch'"); - expect(stderr).toContain("Run 'webpack --help' to see available commands and options"); - expect(stdout).toBeFalsy(); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); }); diff --git a/test/watch/stats/__snapshots__/stats-and-watch.test.js.snap.webpack4 b/test/watch/stats/__snapshots__/stats-and-watch.test.js.snap.webpack4 new file mode 100644 index 00000000000..a0dac6fb561 --- /dev/null +++ b/test/watch/stats/__snapshots__/stats-and-watch.test.js.snap.webpack4 @@ -0,0 +1,13 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`stats and watch should log stats with the "normal" value in arguments: stderr 1`] = `""`; + +exports[`stats and watch should log stats with the "normal" value in arguments: stdout 1`] = `"webpack x.x.x"`; + +exports[`stats and watch should not log stats with the "none" value from the configuration and multi compiler mode: stderr 1`] = `""`; + +exports[`stats and watch should not log stats with the "none" value from the configuration and multi compiler mode: stdout 1`] = `"webpack x.x.x"`; + +exports[`stats and watch should not log stats with the "none" value from the configuration: stderr 1`] = `""`; + +exports[`stats and watch should not log stats with the "none" value from the configuration: stdout 1`] = `"webpack x.x.x"`; diff --git a/test/watch/stats/__snapshots__/stats-and-watch.test.js.snap.webpack5 b/test/watch/stats/__snapshots__/stats-and-watch.test.js.snap.webpack5 new file mode 100644 index 00000000000..46ba1626ed8 --- /dev/null +++ b/test/watch/stats/__snapshots__/stats-and-watch.test.js.snap.webpack5 @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`stats and watch should log stats with the "normal" value in arguments: stderr 1`] = `""`; + +exports[`stats and watch should log stats with the "normal" value in arguments: stdout 1`] = ` +"asset main.js 1.19 KiB [compared for emit] (name: main) +./src/index.js 20 bytes [built] [code generated] +webpack x.x.x compiled successfully in ms" +`; + +exports[`stats and watch should not log stats with the "none" value from the configuration and multi compiler mode: stderr 1`] = `""`; + +exports[`stats and watch should not log stats with the "none" value from the configuration and multi compiler mode: stdout 1`] = `"webpack x.x.x"`; + +exports[`stats and watch should not log stats with the "none" value from the configuration: stderr 1`] = `""`; + +exports[`stats and watch should not log stats with the "none" value from the configuration: stdout 1`] = `"webpack x.x.x"`; diff --git a/test/watch/stats/stats-and-watch.test.js b/test/watch/stats/stats-and-watch.test.js index b2c3beee283..cd30579d748 100644 --- a/test/watch/stats/stats-and-watch.test.js +++ b/test/watch/stats/stats-and-watch.test.js @@ -1,20 +1,20 @@ "use strict"; -const { runWatch } = require("../../utils/test-utils"); +const { runWatch, normalizeStderr, normalizeStdout } = require("../../utils/test-utils"); describe("stats and watch", () => { it('should not log stats with the "none" value from the configuration', async () => { const { stderr, stdout } = await runWatch(__dirname, ["-c", "./webpack.config.js"]); - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); it('should not log stats with the "none" value from the configuration and multi compiler mode', async () => { const { stderr, stdout } = await runWatch(__dirname, ["-c", "./multi-webpack.config.js"]); - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); it('should log stats with the "normal" value in arguments', async () => { @@ -25,7 +25,7 @@ describe("stats and watch", () => { "normal", ]); - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); }); From 12e787f032278ced78bd1a94da76cd7267eb5623 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 17 May 2021 21:21:15 +0300 Subject: [PATCH 2/2] test: update normalize --- test/utils/test-utils.js | 17 ++++++-- .../__snapshots__/bail.test.js.snap.webpack5 | 42 +++++++++---------- .../__snapshots__/basic.test.js.snap.webpack5 | 12 ++---- test/watch/basic/watch.config.js | 1 + .../stats-and-watch.test.js.snap.webpack5 | 6 +-- 5 files changed, 41 insertions(+), 37 deletions(-) diff --git a/test/utils/test-utils.js b/test/utils/test-utils.js index 96d5c28f401..eec87aa012c 100644 --- a/test/utils/test-utils.js +++ b/test/utils/test-utils.js @@ -251,8 +251,14 @@ const normalizeError = (output) => { .replace(/\s+at .+(}|\)|\d)/gs, "\n at stack"); }; -const normalizeCompileTime = (output) => { - return output.replace(/in \d+ ms/gm, "in ms").replace(); +const normalizeTime = (output) => { + return output.replace(/[\d.]+ ms/gm, " ms").replace(); +}; + +const normalizeSize = (output) => { + return output + .replace(/\d+(.\d+)? (bytes|KiB|MiB|GiB)/g, " ") + .replace(); }; const normalizeV4Output = (output) => { @@ -272,11 +278,13 @@ const normalizeStdout = (stdout) => { } let normalizedStdout = stripAnsi(stdout); + normalizedStdout = normalizeCwd(normalizedStdout); normalizedStdout = normalizeVersions(normalizedStdout); - normalizedStdout = normalizeError(normalizedStdout); - normalizedStdout = normalizeCompileTime(normalizedStdout); + normalizedStdout = normalizeTime(normalizedStdout); + normalizedStdout = normalizeSize(normalizedStdout); normalizedStdout = normalizeV4Output(normalizedStdout); + normalizedStdout = normalizeError(normalizedStdout); return normalizedStdout; }; @@ -291,6 +299,7 @@ const normalizeStderr = (stderr) => { } let normalizedStderr = stripAnsi(stderr); + normalizedStderr = normalizeCwd(normalizedStderr); const networkIPv4 = internalIp.v4.sync(); diff --git a/test/watch/bail/__snapshots__/bail.test.js.snap.webpack5 b/test/watch/bail/__snapshots__/bail.test.js.snap.webpack5 index 1fd26a728da..a158001d26e 100644 --- a/test/watch/bail/__snapshots__/bail.test.js.snap.webpack5 +++ b/test/watch/bail/__snapshots__/bail.test.js.snap.webpack5 @@ -4,14 +4,14 @@ exports[`"bail" option should log warning in case of multiple compilers: stderr exports[`"bail" option should log warning in case of multiple compilers: stdout 1`] = ` "first: - asset ./dist-first.js 217 bytes [emitted] (name: main) - ./src/first.js 50 bytes [built] [code generated] - first (webpack x.x.x) compiled successfully in ms + asset ./dist-first.js [emitted] (name: main) + ./src/first.js [built] [code generated] + first (webpack x.x.x) compiled successfully in ms second: - asset ./dist-second.js 221 bytes [emitted] (name: main) - ./src/second.js 51 bytes [built] [code generated] - second (webpack x.x.x) compiled successfully in ms" + asset ./dist-second.js [emitted] (name: main) + ./src/second.js [built] [code generated] + second (webpack x.x.x) compiled successfully in ms" `; exports[`"bail" option should log warning in watch mode: stderr 1`] = `" [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`; @@ -19,23 +19,23 @@ exports[`"bail" option should log warning in watch mode: stderr 1`] = `" [web exports[`"bail" option should log warning in watch mode: stderr 2`] = `" [webpack-cli] You are using \\"bail\\" with \\"watch\\". \\"bail\\" will still exit webpack when the first error is found."`; exports[`"bail" option should log warning in watch mode: stdout 1`] = ` -"asset main.js 217 bytes [compared for emit] (name: main) -./src/first.js 50 bytes [built] [code generated] -webpack x.x.x compiled successfully in ms" +"asset main.js [compared for emit] (name: main) +./src/first.js [built] [code generated] +webpack x.x.x compiled successfully in ms" `; exports[`"bail" option should log warning in watch mode: stdout 2`] = ` -"asset main.js 217 bytes [compared for emit] (name: main) -./src/first.js 50 bytes [built] [code generated] -webpack x.x.x compiled successfully in ms" +"asset main.js [compared for emit] (name: main) +./src/first.js [built] [code generated] +webpack x.x.x compiled successfully in ms" `; exports[`"bail" option should not log warning in not watch mode without the "watch" option: stderr 1`] = `""`; exports[`"bail" option should not log warning in not watch mode without the "watch" option: stdout 1`] = ` -"asset main.js 217 bytes [emitted] (name: main) -./src/first.js 50 bytes [built] [code generated] -webpack x.x.x compiled successfully in ms" +"asset main.js [emitted] (name: main) +./src/first.js [built] [code generated] +webpack x.x.x compiled successfully in ms" `; exports[`"bail" option should not log warning without the "bail" option: stderr 1`] = `""`; @@ -43,13 +43,13 @@ exports[`"bail" option should not log warning without the "bail" option: stderr exports[`"bail" option should not log warning without the "bail" option: stderr 2`] = `""`; exports[`"bail" option should not log warning without the "bail" option: stdout 1`] = ` -"asset main.js 217 bytes [compared for emit] (name: main) -./src/first.js 50 bytes [built] [code generated] -webpack x.x.x compiled successfully in ms" +"asset main.js [compared for emit] (name: main) +./src/first.js [built] [code generated] +webpack x.x.x compiled successfully in ms" `; exports[`"bail" option should not log warning without the "bail" option: stdout 2`] = ` -"asset main.js 217 bytes [compared for emit] (name: main) -./src/first.js 50 bytes [built] [code generated] -webpack x.x.x compiled successfully in ms" +"asset main.js [compared for emit] (name: main) +./src/first.js [built] [code generated] +webpack x.x.x compiled successfully in ms" `; diff --git a/test/watch/basic/__snapshots__/basic.test.js.snap.webpack5 b/test/watch/basic/__snapshots__/basic.test.js.snap.webpack5 index 7a48dc3ee9a..97f8a68bf2c 100644 --- a/test/watch/basic/__snapshots__/basic.test.js.snap.webpack5 +++ b/test/watch/basic/__snapshots__/basic.test.js.snap.webpack5 @@ -24,13 +24,7 @@ exports[`basic should recompile upon file change using the \`command\` option an exports[`basic should work with negative value: stderr 1`] = `""`; exports[`basic should work with negative value: stdout 1`] = ` -"asset main.js 31 bytes [emitted] [minimized] (name: main) -./src/index.js 32 bytes [built] [code generated] - -WARNING in configuration -The 'mode' option has not been set, webpack will fallback to 'production' for this value. -Set 'mode' option to 'development' or 'production' to enable defaults for each environment. -You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/ - -webpack x.x.x compiled with 1 warning in ms" +"asset main.js [emitted] (name: main) +./src/index.js [built] [code generated] +webpack x.x.x compiled successfully in ms" `; diff --git a/test/watch/basic/watch.config.js b/test/watch/basic/watch.config.js index 861575d0712..32f2663dcd7 100644 --- a/test/watch/basic/watch.config.js +++ b/test/watch/basic/watch.config.js @@ -1,3 +1,4 @@ module.exports = { + mode: "development", watch: true, }; diff --git a/test/watch/stats/__snapshots__/stats-and-watch.test.js.snap.webpack5 b/test/watch/stats/__snapshots__/stats-and-watch.test.js.snap.webpack5 index 46ba1626ed8..ac4eb2bed37 100644 --- a/test/watch/stats/__snapshots__/stats-and-watch.test.js.snap.webpack5 +++ b/test/watch/stats/__snapshots__/stats-and-watch.test.js.snap.webpack5 @@ -3,9 +3,9 @@ exports[`stats and watch should log stats with the "normal" value in arguments: stderr 1`] = `""`; exports[`stats and watch should log stats with the "normal" value in arguments: stdout 1`] = ` -"asset main.js 1.19 KiB [compared for emit] (name: main) -./src/index.js 20 bytes [built] [code generated] -webpack x.x.x compiled successfully in ms" +"asset main.js [compared for emit] (name: main) +./src/index.js [built] [code generated] +webpack x.x.x compiled successfully in ms" `; exports[`stats and watch should not log stats with the "none" value from the configuration and multi compiler mode: stderr 1`] = `""`;