diff --git a/test/utils/test-utils.js b/test/utils/test-utils.js index ed707c23691..eec87aa012c 100644 --- a/test/utils/test-utils.js +++ b/test/utils/test-utils.js @@ -251,6 +251,23 @@ const normalizeError = (output) => { .replace(/\s+at .+(}|\)|\d)/gs, "\n at stack"); }; +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) => { + 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; @@ -261,8 +278,12 @@ const normalizeStdout = (stdout) => { } let normalizedStdout = stripAnsi(stdout); + normalizedStdout = normalizeCwd(normalizedStdout); normalizedStdout = normalizeVersions(normalizedStdout); + normalizedStdout = normalizeTime(normalizedStdout); + normalizedStdout = normalizeSize(normalizedStdout); + normalizedStdout = normalizeV4Output(normalizedStdout); normalizedStdout = normalizeError(normalizedStdout); return normalizedStdout; @@ -278,6 +299,7 @@ const normalizeStderr = (stderr) => { } let normalizedStderr = stripAnsi(stderr); + normalizedStderr = normalizeCwd(normalizedStderr); const networkIPv4 = internalIp.v4.sync(); 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..a158001d26e --- /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 [emitted] (name: main) + ./src/first.js [built] [code generated] + first (webpack x.x.x) compiled successfully in ms + +second: + 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."`; + +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 [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 [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 [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`] = `""`; + +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 [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 [compared for emit] (name: main) +./src/first.js [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..97f8a68bf2c --- /dev/null +++ b/test/watch/basic/__snapshots__/basic.test.js.snap.webpack5 @@ -0,0 +1,30 @@ +// 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 [emitted] (name: main) +./src/index.js [built] [code generated] +webpack x.x.x compiled successfully 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/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.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..ac4eb2bed37 --- /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 [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`] = `""`; + +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"); }); });