From d86e06d5810f19e987fa79643a04bcdb685c97b2 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Mon, 18 Jul 2022 22:53:29 +0530 Subject: [PATCH] refactor: drop webpack v4 from tests, packages, and CI (#3338) --- .cspell.json | 5 +- packages/configtest/package.json | 2 +- packages/generators/package.json | 3 +- packages/info/package.json | 1 + packages/serve/package.json | 1 + packages/serve/src/index.ts | 1 - packages/webpack-cli/package.json | 2 +- test/build/cache/cache.test.js | 146 ++++++---------- test/build/colors/colors.test.js | 72 +++----- .../basic-config/default-js-config.test.js | 12 +- .../cjs-config/default-cjs-config.test.js | 11 +- .../mjs-config/default-mjs-config.test.js | 11 +- .../top-multi-compilers-options.test.js | 16 +- test/build/env/array/array-env.test.js | 12 +- test/build/env/object/object-env.test.js | 11 +- .../invalid-schema/invalid-schema.test.js | 84 ++------- .../mode-single-arg/mode-single-arg.test.js | 13 +- test/build/progress/progress-flag.test.js | 8 +- .../start-finish-force-log.test.js | 17 +- .../config-no/no-stats-with-config.test.js | 18 +- test/build/stats/config/stats.test.js | 59 ++----- test/build/stats/flags/stats.test.js | 63 ++----- .../target-flag.test.js.snap.webpack5 | 24 +++ .../target/flag-test/target-flag.test.js | 159 ++++++++++-------- test/help/help.test.js | 18 +- test/serve/basic/serve-basic.test.js | 86 +++++----- test/utils/test-utils.js | 4 +- test/watch/basic/basic.test.js | 43 +---- .../watch-variable/watch-variable.test.js | 23 +-- 29 files changed, 342 insertions(+), 583 deletions(-) diff --git a/.cspell.json b/.cspell.json index f1acf3776de..529e1c64485 100644 --- a/.cspell.json +++ b/.cspell.json @@ -94,7 +94,10 @@ "Nitin", "smoketest", "dbaeumer", - "esbenp" + "esbenp", + "msuccessfully", + "mfirst", + "msecond" ], "dictionaries": ["npm", "software-terms"], "ignorePaths": [ diff --git a/packages/configtest/package.json b/packages/configtest/package.json index e63229c524e..6ddc50bcdfa 100644 --- a/packages/configtest/package.json +++ b/packages/configtest/package.json @@ -20,7 +20,7 @@ "lib" ], "peerDependencies": { - "webpack": "4.x.x || 5.x.x", + "webpack": "5.x.x", "webpack-cli": "4.x.x" } } diff --git a/packages/generators/package.json b/packages/generators/package.json index 7cf6c35dcb1..4ddabeefaef 100644 --- a/packages/generators/package.json +++ b/packages/generators/package.json @@ -30,7 +30,8 @@ "yeoman-generator": "^4.12.0" }, "peerDependencies": { - "webpack": "4.x.x || 5.x.x" + "webpack": "5.x.x", + "webpack-cli": "4.x.x" }, "peerDependenciesMeta": { "prettier": { diff --git a/packages/info/package.json b/packages/info/package.json index 1cbfb72e3ab..5adfde3a678 100644 --- a/packages/info/package.json +++ b/packages/info/package.json @@ -24,6 +24,7 @@ }, "gitHead": "fb50f766851f500ca12867a2aa9de81fa6e368f9", "peerDependencies": { + "webpack": "5.x.x", "webpack-cli": "4.x.x" }, "devDependencies": { diff --git a/packages/serve/package.json b/packages/serve/package.json index a88cdc572a3..f93d1bb492b 100644 --- a/packages/serve/package.json +++ b/packages/serve/package.json @@ -21,6 +21,7 @@ "lib" ], "peerDependencies": { + "webpack": "5.x.x", "webpack-cli": "4.x.x" }, "peerDependenciesMeta": { diff --git a/packages/serve/src/index.ts b/packages/serve/src/index.ts index bcef8c03a94..2fd0a59abcd 100644 --- a/packages/serve/src/index.ts +++ b/packages/serve/src/index.ts @@ -1,4 +1,3 @@ -// eslint-disable-next-line node/no-extraneous-import import type { Compiler, cli } from "webpack"; import { IWebpackCLI, WebpackDevServerOptions } from "webpack-cli"; diff --git a/packages/webpack-cli/package.json b/packages/webpack-cli/package.json index 0011c5a3d4d..e08fd2a0fa4 100644 --- a/packages/webpack-cli/package.json +++ b/packages/webpack-cli/package.json @@ -48,7 +48,7 @@ "webpack-merge": "^5.7.3" }, "peerDependencies": { - "webpack": "4.x.x || 5.x.x" + "webpack": "5.x.x" }, "peerDependenciesMeta": { "@webpack-cli/generators": { diff --git a/test/build/cache/cache.test.js b/test/build/cache/cache.test.js index d2d8bbaab7e..cd05719877c 100644 --- a/test/build/cache/cache.test.js +++ b/test/build/cache/cache.test.js @@ -3,7 +3,7 @@ const path = require("path"); // eslint-disable-next-line node/no-unpublished-require const rimraf = require("rimraf"); -const { run, isWebpack5 } = require("../../utils/test-utils"); +const { run } = require("../../utils/test-utils"); describe("cache", () => { it("should work", async () => { @@ -14,25 +14,19 @@ describe("cache", () => { let { exitCode, stderr, stdout } = await run(__dirname, ["-c", "./webpack.config.js"]); expect(exitCode).toEqual(0); - - if (isWebpack5) { - expect(stderr.match(/No pack exists at/g)).toHaveLength(1); - expect(stderr.match(/Stored pack/g)).toHaveLength(1); - expect(stderr).toBeTruthy(); - expect(stdout).toBeTruthy(); - } + expect(stderr.match(/No pack exists at/g)).toHaveLength(1); + expect(stderr.match(/Stored pack/g)).toHaveLength(1); + expect(stderr).toBeTruthy(); + expect(stdout).toBeTruthy(); ({ exitCode, stderr, stdout } = await run(__dirname, ["-c", "./webpack.config.js"])); expect(exitCode).toEqual(0); - - if (isWebpack5) { - expect(stderr.match(/restore cache container:/g)).toHaveLength(1); - expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1); - expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1); - expect(stderr).toBeTruthy(); - expect(stdout).toBeTruthy(); - } + expect(stderr.match(/restore cache container:/g)).toHaveLength(1); + expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1); + expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1); + expect(stderr).toBeTruthy(); + expect(stdout).toBeTruthy(); }); it("should work in multi compiler mode", async () => { @@ -46,25 +40,19 @@ describe("cache", () => { let { exitCode, stderr, stdout } = await run(__dirname, ["-c", "./multi.config.js"]); expect(exitCode).toEqual(0); - - if (isWebpack5) { - expect(stderr.match(/No pack exists at/g)).toHaveLength(2); - expect(stderr.match(/Stored pack/g)).toHaveLength(2); - expect(stderr).toBeTruthy(); - expect(stdout).toBeTruthy(); - } + expect(stderr.match(/No pack exists at/g)).toHaveLength(2); + expect(stderr.match(/Stored pack/g)).toHaveLength(2); + expect(stderr).toBeTruthy(); + expect(stdout).toBeTruthy(); ({ exitCode, stderr, stdout } = await run(__dirname, ["-c", "./multi.config.js"])); expect(exitCode).toEqual(0); - - if (isWebpack5) { - expect(stderr.match(/restore cache container:/g)).toHaveLength(2); - expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(2); - expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(2); - expect(stderr).toBeTruthy(); - expect(stdout).toBeTruthy(); - } + expect(stderr.match(/restore cache container:/g)).toHaveLength(2); + expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(2); + expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(2); + expect(stderr).toBeTruthy(); + expect(stdout).toBeTruthy(); }); it("should work in multi compiler mode with the `--config-name` argument", async () => { @@ -82,13 +70,10 @@ describe("cache", () => { ]); expect(exitCode).toEqual(0); - - if (isWebpack5) { - expect(stderr.match(/No pack exists at/g)).toHaveLength(1); - expect(stderr.match(/Stored pack/g)).toHaveLength(1); - expect(stderr).toBeTruthy(); - expect(stdout).toBeTruthy(); - } + expect(stderr.match(/No pack exists at/g)).toHaveLength(1); + expect(stderr.match(/Stored pack/g)).toHaveLength(1); + expect(stderr).toBeTruthy(); + expect(stdout).toBeTruthy(); ({ exitCode, stderr, stdout } = await run(__dirname, [ "-c", @@ -100,14 +85,11 @@ describe("cache", () => { ])); expect(exitCode).toEqual(0); - - if (isWebpack5) { - expect(stderr.match(/restore cache container:/g)).toHaveLength(1); - expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1); - expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1); - expect(stderr).toBeTruthy(); - expect(stdout).toBeTruthy(); - } + expect(stderr.match(/restore cache container:/g)).toHaveLength(1); + expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1); + expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1); + expect(stderr).toBeTruthy(); + expect(stdout).toBeTruthy(); }); it("should work with the `--merge` argument", async () => { @@ -126,13 +108,10 @@ describe("cache", () => { ]); expect(exitCode).toEqual(0); - - if (isWebpack5) { - expect(stderr.match(/No pack exists at/g)).toHaveLength(1); - expect(stderr.match(/Stored pack/g)).toHaveLength(1); - expect(stderr).toBeTruthy(); - expect(stdout).toBeTruthy(); - } + expect(stderr.match(/No pack exists at/g)).toHaveLength(1); + expect(stderr.match(/Stored pack/g)).toHaveLength(1); + expect(stderr).toBeTruthy(); + expect(stdout).toBeTruthy(); ({ exitCode, stderr, stdout } = await run(__dirname, [ "-c", @@ -145,14 +124,11 @@ describe("cache", () => { ])); expect(exitCode).toEqual(0); - - if (isWebpack5) { - expect(stderr.match(/restore cache container:/g)).toHaveLength(1); - expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1); - expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1); - expect(stderr).toBeTruthy(); - expect(stdout).toBeTruthy(); - } + expect(stderr.match(/restore cache container:/g)).toHaveLength(1); + expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1); + expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1); + expect(stderr).toBeTruthy(); + expect(stdout).toBeTruthy(); }); it("should work with the `--config-name` and `--merge` argument", async () => { @@ -175,13 +151,10 @@ describe("cache", () => { ]); expect(exitCode).toEqual(0); - - if (isWebpack5) { - expect(stderr.match(/No pack exists at/g)).toHaveLength(1); - expect(stderr.match(/Stored pack/g)).toHaveLength(1); - expect(stderr).toBeTruthy(); - expect(stdout).toBeTruthy(); - } + expect(stderr.match(/No pack exists at/g)).toHaveLength(1); + expect(stderr.match(/Stored pack/g)).toHaveLength(1); + expect(stderr).toBeTruthy(); + expect(stdout).toBeTruthy(); ({ exitCode, stderr, stdout } = await run(__dirname, [ "-c", @@ -198,14 +171,11 @@ describe("cache", () => { ])); expect(exitCode).toEqual(0); - - if (isWebpack5) { - expect(stderr.match(/restore cache container:/g)).toHaveLength(1); - expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1); - expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1); - expect(stderr).toBeTruthy(); - expect(stdout).toBeTruthy(); - } + expect(stderr.match(/restore cache container:/g)).toHaveLength(1); + expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1); + expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1); + expect(stderr).toBeTruthy(); + expect(stdout).toBeTruthy(); }); it("should work with autoloading configuration", async () => { @@ -219,24 +189,18 @@ describe("cache", () => { let { exitCode, stderr, stdout } = await run(__dirname, ["--name", "cache-test-autoloading"]); expect(exitCode).toEqual(0); - - if (isWebpack5) { - expect(stderr.match(/No pack exists at/g)).toHaveLength(1); - expect(stderr.match(/Stored pack/g)).toHaveLength(1); - expect(stderr).toBeTruthy(); - expect(stdout).toBeTruthy(); - } + expect(stderr.match(/No pack exists at/g)).toHaveLength(1); + expect(stderr.match(/Stored pack/g)).toHaveLength(1); + expect(stderr).toBeTruthy(); + expect(stdout).toBeTruthy(); ({ exitCode, stderr, stdout } = await run(__dirname, ["--name", "cache-test-autoloading"])); expect(exitCode).toEqual(0); - - if (isWebpack5) { - expect(stderr.match(/restore cache container:/g)).toHaveLength(1); - expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1); - expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1); - expect(stderr).toBeTruthy(); - expect(stdout).toBeTruthy(); - } + expect(stderr.match(/restore cache container:/g)).toHaveLength(1); + expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1); + expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1); + expect(stderr).toBeTruthy(); + expect(stdout).toBeTruthy(); }); }); diff --git a/test/build/colors/colors.test.js b/test/build/colors/colors.test.js index 08fdecab7b1..df0a590a17b 100644 --- a/test/build/colors/colors.test.js +++ b/test/build/colors/colors.test.js @@ -1,6 +1,6 @@ "use strict"; -const { run, isWebpack5 } = require("../../utils/test-utils"); +const { run } = require("../../utils/test-utils"); const { resolve } = require("path"); describe("colors", () => { @@ -11,8 +11,7 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`); + expect(stdout).toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); }); it('should work with the "stats" option from flags', async () => { @@ -22,8 +21,7 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`); + expect(stdout).toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); }); it('should work with the "stats" option from flags and from configuration', async () => { @@ -35,8 +33,7 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`); + expect(stdout).toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); }); it('should work with the "stats" option from flags and from configuration #2', async () => { @@ -50,8 +47,7 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`); + expect(stdout).toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); }); it('should work with the "stats" option and --color flags', async () => { @@ -59,8 +55,7 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`); + expect(stdout).toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); }); it("should disable colored output with --no-color", async () => { @@ -68,9 +63,8 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).not.toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`); - expect(stdout).toContain(output); + expect(stdout).not.toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); + expect(stdout).toContain("successfully"); }); it('should work with the "stats" option from the configuration', async () => { @@ -84,8 +78,7 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`); + expect(stdout).toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); }); it('should work with the "stats" option from the configuration #1', async () => { @@ -99,8 +92,7 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`); + expect(stdout).toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); }); it('should work with the "stats" option from the configuration #2', async () => { @@ -114,8 +106,7 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`); + expect(stdout).toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); }); it('should work with the "stats" option from the configuration #3', async () => { @@ -125,8 +116,7 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`); + expect(stdout).toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); }); it('should work with the "stats" option from the configuration #4', async () => { @@ -136,9 +126,8 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).not.toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`); - expect(stdout).toContain(output); + expect(stdout).not.toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); + expect(stdout).toContain("successfully"); }); it('should work with the "stats" option from the configuration #5', async () => { @@ -152,8 +141,7 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).toContain(`\u001b[31m${output}\u001b[39m\u001b[22m`); + expect(stdout).toContain(`\u001b[31msuccessfully\u001b[39m\u001b[22m`); }); it('should work with the "stats" option from the configuration in multi compiler mode', async () => { @@ -167,14 +155,10 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - - if (isWebpack5) { - // cSpell:ignore msuccessfully - // red from first config - expect(stdout).toContain(`\u001b[31msuccessfully`); - // blue from second config - expect(stdout).toContain(`\u001b[34msuccessfully`); - } + // red from first config + expect(stdout).toContain(`\u001b[31msuccessfully`); + // blue from second config + expect(stdout).toContain(`\u001b[34msuccessfully`); }); it("should prioritize --color over colors in config", async () => { @@ -185,8 +169,7 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`); + expect(stdout).toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); }); it("should prioritize --no-color over colors in config", async () => { @@ -197,9 +180,8 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - const output = isWebpack5 ? "successfully" : "main.js"; - expect(stdout).not.toContain(`\u001b[1m\u001b[32m${output}\u001b[39m\u001b[22m`); - expect(stdout).toContain(output); + expect(stdout).not.toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); + expect(stdout).toContain("successfully"); }); it("should work in multi compiler mode", async () => { @@ -210,12 +192,8 @@ describe("colors", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - - if (isWebpack5) { - // cSpell:ignore mfirst, msecond - expect(stdout).toContain(`\u001b[1mfirst-config`); - expect(stdout).toContain(`\u001b[1msecond-config`); - expect(stdout).toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); - } + expect(stdout).toContain(`\u001b[1mfirst-config`); + expect(stdout).toContain(`\u001b[1msecond-config`); + expect(stdout).toContain(`\u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`); }); }); diff --git a/test/build/config/defaults/basic-config/default-js-config.test.js b/test/build/config/defaults/basic-config/default-js-config.test.js index 6450af39762..69b3378bf28 100644 --- a/test/build/config/defaults/basic-config/default-js-config.test.js +++ b/test/build/config/defaults/basic-config/default-js-config.test.js @@ -1,6 +1,6 @@ const fs = require("fs"); const path = require("path"); -const { run, isWebpack5 } = require("../../../../utils/test-utils"); +const { run } = require("../../../../utils/test-utils"); describe("default config", () => { it("should build and not throw error when config is present but not supplied via flag", async () => { @@ -8,19 +8,11 @@ describe("default config", () => { expect(exitCode).toEqual(0); expect(stderr).toBeFalsy(); - // default entry should be used expect(stdout).toContain("./src/index.js"); // should pick up the output path from config expect(stdout).toContain("test-output"); - - if (!isWebpack5) { - expect(stdout).toContain("Hash"); - expect(stdout).toContain("Version"); - expect(stdout).toContain("Built at"); - expect(stdout).toContain("Time"); - } - + expect(stdout).toContain("compiled successfully"); // check that the output file exists expect(fs.existsSync(path.join(__dirname, "/dist/test-output.js"))).toBeTruthy(); }); diff --git a/test/build/config/defaults/cjs-config/default-cjs-config.test.js b/test/build/config/defaults/cjs-config/default-cjs-config.test.js index bdeba37c4ad..acf61b76014 100644 --- a/test/build/config/defaults/cjs-config/default-cjs-config.test.js +++ b/test/build/config/defaults/cjs-config/default-cjs-config.test.js @@ -1,6 +1,6 @@ const fs = require("fs"); const path = require("path"); -const { run, isWebpack5 } = require("../../../../utils/test-utils"); +const { run } = require("../../../../utils/test-utils"); describe("default config with cjs extension", () => { it("should build and not throw error with cjs config by default", async () => { @@ -12,14 +12,7 @@ describe("default config with cjs extension", () => { expect(stdout).toContain("./src/index.js"); // should pick up the output path from config expect(stdout).toContain("test-output"); - - if (!isWebpack5) { - expect(stdout).toContain("Hash"); - expect(stdout).toContain("Version"); - expect(stdout).toContain("Built at"); - expect(stdout).toContain("Time"); - } - + expect(stdout).toContain("compiled successfully"); // check that the output file exists expect(fs.existsSync(path.join(__dirname, "/dist/test-output.js"))).toBeTruthy(); }); diff --git a/test/build/config/defaults/mjs-config/default-mjs-config.test.js b/test/build/config/defaults/mjs-config/default-mjs-config.test.js index 3cc8cff9c02..45b3c17a6f0 100644 --- a/test/build/config/defaults/mjs-config/default-mjs-config.test.js +++ b/test/build/config/defaults/mjs-config/default-mjs-config.test.js @@ -1,6 +1,6 @@ const fs = require("fs"); const path = require("path"); -const { run, isWebpack5 } = require("../../../../utils/test-utils"); +const { run } = require("../../../../utils/test-utils"); describe("default config with mjs extension", () => { it("should build and not throw error with mjs config by default", async () => { @@ -18,14 +18,7 @@ describe("default config with mjs extension", () => { expect(stdout).toContain("./src/index.js"); // should pick up the output path from config expect(stdout).toContain("test-output"); - - if (!isWebpack5) { - expect(stdout).toContain("Hash"); - expect(stdout).toContain("Version"); - expect(stdout).toContain("Built at"); - expect(stdout).toContain("Time"); - } - + expect(stdout).toContain("compiled successfully"); // check that the output file exists expect(fs.existsSync(path.join(__dirname, "/dist/test-output.js"))).toBeTruthy(); } diff --git a/test/build/config/top-multi-compilers-options/top-multi-compilers-options.test.js b/test/build/config/top-multi-compilers-options/top-multi-compilers-options.test.js index 7602e464cd7..03bddbf2abd 100644 --- a/test/build/config/top-multi-compilers-options/top-multi-compilers-options.test.js +++ b/test/build/config/top-multi-compilers-options/top-multi-compilers-options.test.js @@ -1,7 +1,7 @@ "use strict"; const { resolve } = require("path"); -const { run, isWebpack5 } = require("../../../utils/test-utils"); +const { run } = require("../../../utils/test-utils"); describe("top multi compiler options", () => { it("should work without provided configuration", async () => { @@ -9,12 +9,7 @@ describe("top multi compiler options", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - - if (isWebpack5) { - expect(stdout).toContain("Done build0\nDone build1\nDone build2\nDone build3"); - } else { - expect(stdout).toBeTruthy(); - } + expect(stdout).toContain("Done build0\nDone build1\nDone build2\nDone build3"); }); it("should work with provided configuration", async () => { @@ -25,11 +20,6 @@ describe("top multi compiler options", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - - if (isWebpack5) { - expect(stdout).toContain("Done build0\nDone build1\nDone build2\nDone build3"); - } else { - expect(stdout).toBeTruthy(); - } + expect(stdout).toContain("Done build0\nDone build1\nDone build2\nDone build3"); }); }); diff --git a/test/build/env/array/array-env.test.js b/test/build/env/array/array-env.test.js index 6c744d98695..c50cce5a859 100644 --- a/test/build/env/array/array-env.test.js +++ b/test/build/env/array/array-env.test.js @@ -5,7 +5,7 @@ const path = require("path"); const execa = require("execa"); const { sync: spawnSync } = execa; -const { run, isWebpack5 } = require("../../../utils/test-utils"); +const { run } = require("../../../utils/test-utils"); const devFile = path.join(__dirname, "./dist/dev.js"); const prodFile = path.join(__dirname, "./dist/prod.js"); @@ -18,12 +18,10 @@ describe("env array", () => { expect(stderr).toBeFalsy(); expect(stdout).toBeTruthy(); - if (isWebpack5) { - const devScript = spawnSync("node", [devFile]); - const prodScript = spawnSync("node", [prodFile]); + const devScript = spawnSync("node", [devFile]); + const prodScript = spawnSync("node", [prodFile]); - expect(devScript.stdout).toBe("environment is development"); - expect(prodScript.stdout).toBe("environment is production"); - } + expect(devScript.stdout).toBe("environment is development"); + expect(prodScript.stdout).toBe("environment is production"); }); }); diff --git a/test/build/env/object/object-env.test.js b/test/build/env/object/object-env.test.js index ab4d9b7ec80..42942ea8ec3 100644 --- a/test/build/env/object/object-env.test.js +++ b/test/build/env/object/object-env.test.js @@ -5,7 +5,7 @@ const path = require("path"); const execa = require("execa"); const { sync: spawnSync } = execa; -const { run, isWebpack5 } = require("../../../utils/test-utils"); +const { run } = require("../../../utils/test-utils"); describe("env object", () => { it("is able to set env for an object", async () => { @@ -15,10 +15,9 @@ describe("env object", () => { expect(stderr).toBeFalsy(); expect(stdout).toBeTruthy(); - if (isWebpack5) { - const executable = path.join(__dirname, "./dist/main.js"); - const bundledScript = spawnSync("node", [executable]); - expect(bundledScript.stdout).toBe("environment is development"); - } + const executable = path.join(__dirname, "./dist/main.js"); + const bundledScript = spawnSync("node", [executable]); + + expect(bundledScript.stdout).toBe("environment is development"); }); }); diff --git a/test/build/error/invalid-schema/invalid-schema.test.js b/test/build/error/invalid-schema/invalid-schema.test.js index 184121aec87..ea9d95257f0 100644 --- a/test/build/error/invalid-schema/invalid-schema.test.js +++ b/test/build/error/invalid-schema/invalid-schema.test.js @@ -1,5 +1,5 @@ "use strict"; -const { run, isWebpack5 } = require("../../../utils/test-utils"); +const { run } = require("../../../utils/test-utils"); describe("invalid schema", () => { it("should log error on invalid config", async () => { @@ -20,7 +20,7 @@ describe("invalid schema", () => { ]); expect(exitCode).toEqual(2); - expect(stderr).toContain(isWebpack5 ? "Invalid options object" : "Invalid Options"); + expect(stderr).toContain("Invalid options object"); expect(stdout).toBeFalsy(); }); @@ -52,14 +52,8 @@ describe("invalid schema", () => { const { exitCode, stderr, stdout } = await run(__dirname, ["--mode", "Yukihira"]); expect(exitCode).toEqual(2); - - if (isWebpack5) { - expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); - expect(stderr).toContain("Expected: 'development | production | none'"); - } else { - expect(stderr).toContain("Invalid configuration object"); - } - + expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); + expect(stderr).toContain("Expected: 'development | production | none'"); expect(stdout).toBeFalsy(); }); @@ -67,14 +61,8 @@ describe("invalid schema", () => { const { exitCode, stderr, stdout } = await run(__dirname, ["build", "--mode", "Yukihira"]); expect(exitCode).toEqual(2); - - if (isWebpack5) { - expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); - expect(stderr).toContain("Expected: 'development | production | none'"); - } else { - expect(stderr).toContain("Invalid configuration object"); - } - + expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); + expect(stderr).toContain("Expected: 'development | production | none'"); expect(stdout).toBeFalsy(); }); @@ -82,14 +70,8 @@ describe("invalid schema", () => { const { exitCode, stderr, stdout } = await run(__dirname, ["bundle", "--mode", "Yukihira"]); expect(exitCode).toEqual(2); - - if (isWebpack5) { - expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); - expect(stderr).toContain("Expected: 'development | production | none'"); - } else { - expect(stderr).toContain("Invalid configuration object"); - } - + expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); + expect(stderr).toContain("Expected: 'development | production | none'"); expect(stdout).toBeFalsy(); }); @@ -97,14 +79,8 @@ describe("invalid schema", () => { const { exitCode, stderr, stdout } = await run(__dirname, ["b", "--mode", "Yukihira"]); expect(exitCode).toEqual(2); - - if (isWebpack5) { - expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); - expect(stderr).toContain("Expected: 'development | production | none'"); - } else { - expect(stderr).toContain("Invalid configuration object"); - } - + expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); + expect(stderr).toContain("Expected: 'development | production | none'"); expect(stdout).toBeFalsy(); }); @@ -112,14 +88,8 @@ describe("invalid schema", () => { const { exitCode, stderr, stdout } = await run(__dirname, ["watch", "--mode", "Yukihira"]); expect(exitCode).toEqual(2); - - if (isWebpack5) { - expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); - expect(stderr).toContain("Expected: 'development | production | none'"); - } else { - expect(stderr).toContain("Invalid configuration object"); - } - + expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); + expect(stderr).toContain("Expected: 'development | production | none'"); expect(stdout).toBeFalsy(); }); @@ -127,14 +97,8 @@ describe("invalid schema", () => { const { exitCode, stderr, stdout } = await run(__dirname, ["w", "--mode", "Yukihira"]); expect(exitCode).toEqual(2); - - if (isWebpack5) { - expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); - expect(stderr).toContain("Expected: 'development | production | none'"); - } else { - expect(stderr).toContain("Invalid configuration object"); - } - + expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); + expect(stderr).toContain("Expected: 'development | production | none'"); expect(stdout).toBeFalsy(); }); @@ -142,14 +106,8 @@ describe("invalid schema", () => { const { exitCode, stderr, stdout } = await run(__dirname, ["serve", "--mode", "Yukihira"]); expect(exitCode).toEqual(2); - - if (isWebpack5) { - expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); - expect(stderr).toContain("Expected: 'development | production | none'"); - } else { - expect(stderr).toContain("Invalid configuration object"); - } - + expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); + expect(stderr).toContain("Expected: 'development | production | none'"); expect(stdout).toBeFalsy(); }); @@ -157,14 +115,8 @@ describe("invalid schema", () => { const { exitCode, stderr, stdout } = await run(__dirname, ["s", "--mode", "Yukihira"]); expect(exitCode).toEqual(2); - - if (isWebpack5) { - expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); - expect(stderr).toContain("Expected: 'development | production | none'"); - } else { - expect(stderr).toContain("Invalid configuration object"); - } - + expect(stderr).toContain("Invalid value 'Yukihira' for the '--mode' option"); + expect(stderr).toContain("Expected: 'development | production | none'"); expect(stdout).toBeFalsy(); }); }); diff --git a/test/build/mode/mode-single-arg/mode-single-arg.test.js b/test/build/mode/mode-single-arg/mode-single-arg.test.js index 630c153be26..41fea708a20 100644 --- a/test/build/mode/mode-single-arg/mode-single-arg.test.js +++ b/test/build/mode/mode-single-arg/mode-single-arg.test.js @@ -1,6 +1,6 @@ "use strict"; -const { run, isWebpack5 } = require("../../../utils/test-utils"); +const { run } = require("../../../utils/test-utils"); describe("mode flags", () => { it("should not set mode=production by default", async () => { @@ -52,15 +52,8 @@ describe("mode flags", () => { const { exitCode, stderr, stdout } = await run(__dirname, ["--mode", "abcd"]); expect(exitCode).toBe(2); - - if (isWebpack5) { - expect(stderr).toContain("Invalid value 'abcd' for the '--mode' option"); - expect(stderr).toContain("Expected: 'development | production | none'"); - } else { - expect(stderr).toContain("configuration.mode should be one of these"); - expect(stderr).toContain(`"development" | "production" | "none"`); - } - + expect(stderr).toContain("Invalid value 'abcd' for the '--mode' option"); + expect(stderr).toContain("Expected: 'development | production | none'"); expect(stdout).toBeFalsy(); }); }); diff --git a/test/build/progress/progress-flag.test.js b/test/build/progress/progress-flag.test.js index 2b45d24eb6e..d60b7980aaa 100644 --- a/test/build/progress/progress-flag.test.js +++ b/test/build/progress/progress-flag.test.js @@ -1,6 +1,6 @@ "use strict"; -const { run, isWebpack5 } = require("../../utils/test-utils"); +const { run } = require("../../utils/test-utils"); describe("progress flag", () => { it("should show progress", async () => { @@ -16,11 +16,7 @@ describe("progress flag", () => { const { exitCode, stderr, stdout } = await run(__dirname, ["--progress=profile"]); expect(exitCode).toBe(0); - - if (isWebpack5) { - expect(stderr).toMatch(/\[webpack\.Progress] \d+ ms setup/); - } - + expect(stderr).toMatch(/\[webpack\.Progress] \d+ ms setup/); expect(stderr).toContain("[webpack.Progress] 100%"); expect(stdout).toContain("main.js"); }); diff --git a/test/build/start-finish-force-log/start-finish-force-log.test.js b/test/build/start-finish-force-log/start-finish-force-log.test.js index f42cb6eb019..cbffee1ae52 100644 --- a/test/build/start-finish-force-log/start-finish-force-log.test.js +++ b/test/build/start-finish-force-log/start-finish-force-log.test.js @@ -1,28 +1,28 @@ "use strict"; -const { run, runWatch, isWebpack5 } = require("../../utils/test-utils"); +const { run, runWatch } = require("../../utils/test-utils"); describe("start finish force log", () => { it("start finish force log when env is set", async () => { const { exitCode, stderr, stdout } = await run(__dirname, [], { env: { WEBPACK_CLI_START_FINISH_FORCE_LOG: true }, }); + expect(exitCode).toBe(0); expect(stderr).toContain("Compiler starting..."); expect(stderr).toContain("Compiler finished"); - const output = isWebpack5 ? "compiled successfully" : "main.js"; - expect(stdout).toContain(output); + expect(stdout).toContain("compiled successfully"); }); it("should show name of the config", async () => { const { exitCode, stderr, stdout } = await run(__dirname, ["--name", "log config"], { env: { WEBPACK_CLI_START_FINISH_FORCE_LOG: true }, }); + expect(exitCode).toBe(0); expect(stderr).toContain("Compiler 'log config' starting..."); expect(stderr).toContain("Compiler 'log config' finished"); - const output = isWebpack5 ? "compiled successfully" : "main.js"; - expect(stdout).toContain(output); + expect(stdout).toContain("compiled successfully"); }); it("should work with watch", async () => { @@ -32,8 +32,7 @@ describe("start finish force log", () => { }); expect(stderr).toContain("Compiler starting..."); expect(stderr).toContain("Compiler finished"); - const output = isWebpack5 ? "compiled successfully" : "main.js"; - expect(stdout).toContain(output); + expect(stdout).toContain("compiled successfully"); }); it("should work with multi compiler", async () => { @@ -45,11 +44,11 @@ describe("start finish force log", () => { }, ); expect(exitCode).toBe(0); + expect(exitCode).toBe(0); expect(stderr).toContain("Compiler 'Gojou' starting..."); expect(stderr).toContain("Compiler 'Satoru' starting..."); expect(stderr).toContain("Compiler 'Gojou' finished"); expect(stderr).toContain("Compiler 'Satoru' finished"); - const output = isWebpack5 ? "compiled successfully" : "main.js"; - expect(stdout).toContain(output); + expect(stdout).toContain("compiled successfully"); }); }); diff --git a/test/build/stats/config-no/no-stats-with-config.test.js b/test/build/stats/config-no/no-stats-with-config.test.js index 9096c8476d1..b9b8f120a39 100644 --- a/test/build/stats/config-no/no-stats-with-config.test.js +++ b/test/build/stats/config-no/no-stats-with-config.test.js @@ -1,6 +1,6 @@ "use strict"; -const { run, isWebpack5 } = require("../../../utils/test-utils"); +const { run } = require("../../../utils/test-utils"); describe("stats flag", () => { it(`should use stats 'detailed' as defined in webpack config`, async () => { @@ -8,14 +8,7 @@ describe("stats flag", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - - if (isWebpack5) { - expect(stdout).toContain("preset: 'detailed'"); - } else { - expect(stdout).toContain("entrypoints: true"); - expect(stdout).toContain("logging: true"); - expect(stdout).toContain("maxModules: Infinity"); - } + expect(stdout).toContain("preset: 'detailed'"); }); it(`should use --no-stats and override value in config`, async () => { @@ -23,11 +16,6 @@ describe("stats flag", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - - if (isWebpack5) { - expect(stdout).toContain("preset: 'none'"); - } else { - expect(stdout).toContain("all: false"); - } + expect(stdout).toContain("preset: 'none'"); }); }); diff --git a/test/build/stats/config/stats.test.js b/test/build/stats/config/stats.test.js index 04324688194..b2fe4465c7e 100644 --- a/test/build/stats/config/stats.test.js +++ b/test/build/stats/config/stats.test.js @@ -1,13 +1,17 @@ "use strict"; -const { run, isWebpack5 } = require("../../../utils/test-utils"); +const { run } = require("../../../utils/test-utils"); // 'normal' is used in webpack.config.js -const statsPresets = ["detailed", "errors-only", "errors-warnings", "minimal", "verbose", "none"]; - -if (isWebpack5) { - statsPresets.push("summary"); -} +const statsPresets = [ + "detailed", + "errors-only", + "errors-warnings", + "minimal", + "verbose", + "none", + "summary", +]; describe("stats flag with config", () => { it("should compile without stats flag", async () => { @@ -15,12 +19,7 @@ describe("stats flag with config", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - - if (isWebpack5) { - expect(stdout).toContain("preset: 'normal'"); - } else { - expect(stdout).toContain("stats: { colors: false }"); - } + expect(stdout).toContain("preset: 'normal'"); }); for (const preset of statsPresets) { @@ -29,41 +28,7 @@ describe("stats flag with config", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - - if (isWebpack5) { - expect(stdout).toContain(`preset: '${preset}'`); - } else { - switch (preset) { - case "normal": - expect(stdout).toContain("stats:"); - break; - case "detailed": - expect(stdout).toContain("entrypoints: true"); - expect(stdout).toContain("errorDetails: true"); - break; - case "errors-only": - expect(stdout).toContain("all: false"); - expect(stdout).toContain("errors: true"); - break; - case "errors-warnings": - expect(stdout).toContain("all: false"); - expect(stdout).toContain("errors: true"); - expect(stdout).toContain("warnings: true"); - break; - case "minimal": - expect(stdout).toContain("modules: true"); - expect(stdout).toContain("maxModules: 0"); - break; - case "verbose": - expect(stdout).toContain("logging: 'verbose'"); - break; - case "none": - expect(stdout).toContain("all: false"); - break; - default: - expect(stdout).toContain(`preset: '${preset}'`); - } - } + expect(stdout).toContain(`preset: '${preset}'`); }); } }); diff --git a/test/build/stats/flags/stats.test.js b/test/build/stats/flags/stats.test.js index 61a54ada27e..b6afd8c6e2a 100644 --- a/test/build/stats/flags/stats.test.js +++ b/test/build/stats/flags/stats.test.js @@ -1,6 +1,6 @@ "use strict"; -const { run, isWebpack5, normalizeStderr, normalizeStdout } = require("../../../utils/test-utils"); +const { run, normalizeStderr, normalizeStdout } = require("../../../utils/test-utils"); const presets = [ "normal", @@ -10,12 +10,9 @@ const presets = [ "minimal", "verbose", "none", + "summary", ]; -if (isWebpack5) { - presets.push("summary"); -} - describe("stats flag", () => { for (const preset of presets) { it(`should accept --stats "${preset}"`, async () => { @@ -23,41 +20,15 @@ describe("stats flag", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); + expect(stdout).toContain(`preset: '${preset}'`); + }); + + it("should accept stats as boolean", async () => { + const { exitCode, stderr, stdout } = await run(__dirname, ["--stats"]); - if (isWebpack5) { - expect(stdout).toContain(`preset: '${preset}'`); - } else { - switch (preset) { - case "normal": - expect(stdout).toContain("stats:"); - break; - case "detailed": - expect(stdout).toContain("entrypoints: true"); - expect(stdout).toContain("errorDetails: true"); - break; - case "errors-only": - expect(stdout).toContain("all: false"); - expect(stdout).toContain("errors: true"); - break; - case "errors-warnings": - expect(stdout).toContain("all: false"); - expect(stdout).toContain("errors: true"); - expect(stdout).toContain("warnings: true"); - break; - case "minimal": - expect(stdout).toContain("modules: true"); - expect(stdout).toContain("maxModules: 0"); - break; - case "verbose": - expect(stdout).toContain("logging: 'verbose'"); - break; - case "none": - expect(stdout).toContain("all: false"); - break; - default: - expect(stdout).toContain(`preset: '${preset}'`); - } - } + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toContain("preset: 'normal'"); }); } @@ -66,12 +37,7 @@ describe("stats flag", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - - if (isWebpack5) { - expect(stdout).toContain("preset: 'normal'"); - } else { - expect(stdout).toContain("stats:"); - } + expect(stdout).toContain("preset: 'normal'"); }); it("should accept --no-stats as boolean", async () => { @@ -79,12 +45,7 @@ describe("stats flag", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - - if (isWebpack5) { - expect(stdout).toContain("preset: 'none'"); - } else { - expect(stdout).toContain("all: false"); - } + expect(stdout).toContain("preset: 'none'"); }); it("should log error when an unknown flag stats value is passed", async () => { diff --git a/test/build/target/flag-test/__snapshots__/target-flag.test.js.snap.webpack5 b/test/build/target/flag-test/__snapshots__/target-flag.test.js.snap.webpack5 index cc162eb7d7b..a7bad0c3567 100644 --- a/test/build/target/flag-test/__snapshots__/target-flag.test.js.snap.webpack5 +++ b/test/build/target/flag-test/__snapshots__/target-flag.test.js.snap.webpack5 @@ -10,8 +10,18 @@ Select an appropriate 'target' to allow selecting one by default, or specify the at stack" `; +exports[`--target flag should throw an error for incompatible multiple targets: stderr 2`] = ` +"[webpack-cli] Error: For the selected environment is no default script chunk format available: +JSONP Array push can be chosen when 'document' or 'importScripts' is available. +CommonJs exports can be chosen when 'require' or node builtins are available. +Select an appropriate 'target' to allow selecting one by default, or specify the 'output.chunkFormat' directly. + at stack" +`; + exports[`--target flag should throw an error for incompatible multiple targets: stdout 1`] = `""`; +exports[`--target flag should throw an error for incompatible multiple targets: stdout 2`] = `""`; + exports[`--target flag should throw an error for invalid target in multiple syntax: stderr 1`] = ` "[webpack-cli] Error: Unknown target 'invalid'. The following targets are supported: * browserslist / browserslist:env / browserslist:query / browserslist:path-to-config / browserslist:path-to-config:env: Resolve features from browserslist. Will resolve browserslist config automatically. Only browser or node queries are supported (electron is not supported). Examples: 'browserslist:modern' to use 'modern' environment from browserslist config @@ -24,8 +34,22 @@ exports[`--target flag should throw an error for invalid target in multiple synt at stack" `; +exports[`--target flag should throw an error for invalid target in multiple syntax: stderr 2`] = ` +"[webpack-cli] Error: Unknown target 'invalid'. The following targets are supported: +* browserslist / browserslist:env / browserslist:query / browserslist:path-to-config / browserslist:path-to-config:env: Resolve features from browserslist. Will resolve browserslist config automatically. Only browser or node queries are supported (electron is not supported). Examples: 'browserslist:modern' to use 'modern' environment from browserslist config +* web: Web browser. +* webworker: Web Worker, SharedWorker or Service Worker. +* [async-]node[X[.Y]]: Node.js in version X.Y. The 'async-' prefix will load chunks asynchronously via 'fs' and 'vm' instead of 'require()'. Examples: node14.5, async-node10. +* electron[X[.Y]]-main/preload/renderer: Electron in version X.Y. Script is running in main, preload resp. renderer context. +* nwjs[X[.Y]] / node-webkit[X[.Y]]: NW.js in version X.Y. +* esX: EcmaScript in this version. Examples: es2020, es5. + at stack" +`; + exports[`--target flag should throw an error for invalid target in multiple syntax: stdout 1`] = `""`; +exports[`--target flag should throw an error for invalid target in multiple syntax: stdout 2`] = `""`; + exports[`--target flag should throw error if target is an empty array: stderr 1`] = ` "[webpack-cli] Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema. - configuration.target should be a non-empty array." diff --git a/test/build/target/flag-test/target-flag.test.js b/test/build/target/flag-test/target-flag.test.js index 8ac054ca794..297433dbb5a 100644 --- a/test/build/target/flag-test/target-flag.test.js +++ b/test/build/target/flag-test/target-flag.test.js @@ -1,5 +1,5 @@ "use strict"; -const { run, isWebpack5, normalizeStdout, normalizeStderr } = require("../../../utils/test-utils"); +const { run, normalizeStdout, normalizeStderr } = require("../../../utils/test-utils"); const targetValues = [ "web", @@ -19,12 +19,7 @@ describe("--target flag", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - - if (isWebpack5) { - expect(stdout).toContain(`target: [ '${val}' ]`); - } else { - expect(stdout).toContain(`target: '${val}'`); - } + expect(stdout).toContain(`target: [ '${val}' ]`); }); it(`should accept ${val} with -t alias`, async () => { @@ -32,12 +27,7 @@ describe("--target flag", () => { expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); - - if (isWebpack5) { - expect(stdout).toContain(`target: [ '${val}' ]`); - } else { - expect(stdout).toContain(`target: '${val}'`); - } + expect(stdout).toContain(`target: [ '${val}' ]`); }); }); @@ -49,64 +39,101 @@ describe("--target flag", () => { expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); - if (isWebpack5) { - it("should allow multiple targets", async () => { - const { exitCode, stderr, stdout } = await run(__dirname, [ - "--target", - "node", - "--target", - "async-node", - ]); + it("should allow multiple targets", async () => { + const { exitCode, stderr, stdout } = await run(__dirname, [ + "--target", + "node", + "--target", + "async-node", + ]); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toContain(`target: [ 'node', 'async-node' ]`); + }); - expect(exitCode).toBe(0); - expect(stderr).toBeFalsy(); - expect(stdout).toContain(`target: [ 'node', 'async-node' ]`); - }); + it("should throw an error for invalid target in multiple syntax", async () => { + const { exitCode, stderr, stdout } = await run(__dirname, [ + "--target", + "node", + "--target", + "invalid", + ]); - it("should throw an error for invalid target in multiple syntax", async () => { - const { exitCode, stderr, stdout } = await run(__dirname, [ - "--target", - "node", - "--target", - "invalid", - ]); - - expect(exitCode).toBe(2); - expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); - expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); - }); + expect(exitCode).toBe(2); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); + }); - it("should throw an error for incompatible multiple targets", async () => { - const { exitCode, stderr, stdout } = await run(__dirname, [ - "--target", - "node", - "--target", - "web", - ]); - - expect(exitCode).toBe(2); - expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); - expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); - }); + it("should throw an error for incompatible multiple targets", async () => { + const { exitCode, stderr, stdout } = await run(__dirname, [ + "--target", + "node", + "--target", + "web", + ]); - it("should reset target from node to async-node with --target-reset", async () => { - const { exitCode, stderr, stdout } = await run(__dirname, [ - "--target-reset", - "--target", - "async-node", - ]); + expect(exitCode).toBe(2); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); + }); - expect(exitCode).toBe(0); - expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); - expect(stdout).toContain(`target: [ 'async-node' ]`); - }); + it("should allow multiple targets", async () => { + const { exitCode, stderr, stdout } = await run(__dirname, [ + "--target", + "node", + "--target", + "async-node", + ]); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toContain(`target: [ 'node', 'async-node' ]`); + }); - it("should throw error if target is an empty array", async () => { - const { exitCode, stderr, stdout } = await run(__dirname, ["--target-reset"]); + it("should throw an error for invalid target in multiple syntax", async () => { + const { exitCode, stderr, stdout } = await run(__dirname, [ + "--target", + "node", + "--target", + "invalid", + ]); - expect(exitCode).toBe(2); - expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); - expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); - }); - } + expect(exitCode).toBe(2); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); + }); + + it("should throw an error for incompatible multiple targets", async () => { + const { exitCode, stderr, stdout } = await run(__dirname, [ + "--target", + "node", + "--target", + "web", + ]); + + expect(exitCode).toBe(2); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); + }); + + it("should reset target from node to async-node with --target-reset", async () => { + const { exitCode, stderr, stdout } = await run(__dirname, [ + "--target-reset", + "--target", + "async-node", + ]); + + expect(exitCode).toBe(0); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(stdout).toContain(`target: [ 'async-node' ]`); + }); + + it("should throw error if target is an empty array", async () => { + const { exitCode, stderr, stdout } = await run(__dirname, ["--target-reset"]); + + expect(exitCode).toBe(2); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); + }); }); diff --git a/test/help/help.test.js b/test/help/help.test.js index 8f7318e3e9d..1c0491b994e 100644 --- a/test/help/help.test.js +++ b/test/help/help.test.js @@ -1,12 +1,6 @@ "use strict"; -const { - run, - normalizeStderr, - normalizeStdout, - isWebpack5, - isDevServer4, -} = require("../utils/test-utils"); +const { run, normalizeStderr, normalizeStdout, isDevServer4 } = require("../utils/test-utils"); describe("help", () => { it('should show help information using the "--help" option', async () => { @@ -264,13 +258,9 @@ describe("help", () => { it('should show help information using the "help --cache-type" option', async () => { const { exitCode, stderr, stdout } = await run(__dirname, ["help", "--cache-type"]); - if (isWebpack5) { - expect(exitCode).toBe(0); - expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); - expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); - } else { - expect(exitCode).toBe(2); - } + expect(exitCode).toBe(0); + expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); }); it('should show help information using the "help --no-stats" option', async () => { diff --git a/test/serve/basic/serve-basic.test.js b/test/serve/basic/serve-basic.test.js index 3667329e2ea..bbbdbf597ad 100644 --- a/test/serve/basic/serve-basic.test.js +++ b/test/serve/basic/serve-basic.test.js @@ -5,7 +5,6 @@ const path = require("path"); const getPort = require("get-port"); const { runWatch, - isWebpack5, normalizeStderr, normalizeStdout, isDevServer4, @@ -173,7 +172,7 @@ describe("basic serve usage", () => { expect(stdout).not.toContain("HotModuleReplacementPlugin"); } - expect(stdout).toContain(isWebpack5 ? "compiled successfully" : "Version: webpack"); + expect(stdout).toContain("compiled successfully"); }); it('should work with the "--stats verbose" option', async () => { @@ -190,9 +189,7 @@ describe("basic serve usage", () => { const isMacOS = process.platform === "darwin"; if (!isMacOS) { - expect(stdout).toContain( - isWebpack5 ? "from webpack.Compiler" : "webpack.buildChunkGraph.visitModules", - ); + expect(stdout).toContain("from webpack.Compiler"); } expect(stdout).toContain("main.js"); }); @@ -222,7 +219,7 @@ describe("basic serve usage", () => { } else { expect(stdout).not.toContain("HotModuleReplacementPlugin"); } - + expect(stdout).toContain("compiled successfully"); expect(stdout).toContain("development"); expect(stdout).toContain("main.js"); }); @@ -238,6 +235,7 @@ describe("basic serve usage", () => { expect(stdout).not.toContain("HotModuleReplacementPlugin"); } + expect(stderr).toContain("webpack.Progress"); expect(stdout).toContain("main.js"); }); @@ -357,19 +355,13 @@ describe("basic serve usage", () => { ]); expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); + expect(stdout).toContain("/my-public-path/"); - if (isWebpack5) { - expect(stdout).toContain("/my-public-path/"); - - if (isDevServer4) { - expect(stdout).toContain("HotModuleReplacementPlugin"); - } else { - expect(stdout).not.toContain("HotModuleReplacementPlugin"); - } - - expect(stdout).toContain("main.js"); + if (isDevServer4) { + expect(stdout).toContain("HotModuleReplacementPlugin"); } else { - expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); + expect(stdout).not.toContain("HotModuleReplacementPlugin"); + expect(stdout).toContain("main.js"); } }); @@ -526,35 +518,6 @@ describe("basic serve usage", () => { expect(stdout).toContain("development"); }); - it("should log used supplied config with serve", async () => { - const { stderr, stdout } = await runWatch( - __dirname, - ["serve", "--config", "log.config.js", "--port", port], - { - killString: /Compiler is watching files for updates\.\.\./, - }, - ); - - // sort logs for CI - let normalizedStderr = normalizeStderr(stderr).split("\n"); - const lastString = normalizedStderr[normalizedStderr.length - 1]; - - if (lastString.includes("webpack-dev-middleware")) { - [ - normalizedStderr[normalizedStderr.length - 1], - normalizedStderr[normalizedStderr.length - 2], - ] = [ - normalizedStderr[normalizedStderr.length - 2], - normalizedStderr[normalizedStderr.length - 1], - ]; - } - - normalizedStderr = normalizedStderr.join("\n"); - - expect(normalizedStderr).toMatchSnapshot("stderr"); - expect(stdout).toBeTruthy(); - }); - it("should log error on using '--watch' flag with serve", async () => { const { exitCode, stdout, stderr } = await runWatch(testPath, ["serve", "--watch"]); @@ -590,10 +553,39 @@ describe("basic serve usage", () => { }); expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); - expect(stdout).toContain(isWebpack5 ? "compiled successfully" : "modules"); + expect(stdout).toContain("compiled successfully"); expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull(); }); + it("should log used supplied config with serve", async () => { + const { stderr, stdout } = await runWatch( + __dirname, + ["serve", "--config", "log.config.js", "--port", port], + { + killString: /Compiler is watching files for updates\.\.\./, + }, + ); + + // sort logs for CI + let normalizedStderr = normalizeStderr(stderr).split("\n"); + const lastString = normalizedStderr[normalizedStderr.length - 1]; + + if (lastString.includes("webpack-dev-middleware")) { + [ + normalizedStderr[normalizedStderr.length - 1], + normalizedStderr[normalizedStderr.length - 2], + ] = [ + normalizedStderr[normalizedStderr.length - 2], + normalizedStderr[normalizedStderr.length - 1], + ]; + } + + normalizedStderr = normalizedStderr.join("\n"); + + expect(normalizedStderr).toMatchSnapshot("stderr"); + expect(stdout).toBeTruthy(); + }); + it("should throw error when same ports in multicompiler", async () => { const { stderr, stdout } = await runWatch(__dirname, [ "serve", diff --git a/test/utils/test-utils.js b/test/utils/test-utils.js index 3c04c98d11b..2e9c4a18beb 100644 --- a/test/utils/test-utils.js +++ b/test/utils/test-utils.js @@ -12,8 +12,7 @@ const { exec } = require("child_process"); const { node: execaNode } = execa; const { Writable } = require("readable-stream"); const concat = require("concat-stream"); -const { cli, version } = require("webpack"); -const isWebpack5 = version.startsWith("5"); +const { cli } = require("webpack"); let devServerVersion; @@ -393,7 +392,6 @@ module.exports = { runAndGetProcess, runWatch, runPromptWithAnswers, - isWebpack5, isDevServer4, isWindows, normalizeStderr, diff --git a/test/watch/basic/basic.test.js b/test/watch/basic/basic.test.js index 1a016c13de0..45668612790 100644 --- a/test/watch/basic/basic.test.js +++ b/test/watch/basic/basic.test.js @@ -1,10 +1,9 @@ "use strict"; -const { run, runAndGetProcess, isWebpack5, processKill } = require("../../utils/test-utils"); +const { run, runAndGetProcess, processKill } = require("../../utils/test-utils"); const { writeFileSync } = require("fs"); const { resolve } = require("path"); -const wordsInStatsv4 = ["Hash", "Built at:", "main.js"]; const wordsInStatsv5 = ["asset", "index.js", "compiled successfully"]; describe("basic", () => { @@ -29,14 +28,8 @@ describe("basic", () => { const data = chunk.toString(); if (data.includes("index.js")) { - if (isWebpack5) { - for (const word of wordsInStatsv5) { - expect(data).toContain(word); - } - } else { - for (const word of wordsInStatsv4) { - expect(data).toContain(word); - } + for (const word of wordsInStatsv5) { + expect(data).toContain(word); } if (!modified) { @@ -65,14 +58,8 @@ describe("basic", () => { const data = chunk.toString(); if (data.includes("index.js")) { - if (isWebpack5) { - for (const word of wordsInStatsv5) { - expect(data).toContain(word); - } - } else { - for (const word of wordsInStatsv4) { - expect(data).toContain(word); - } + for (const word of wordsInStatsv5) { + expect(data).toContain(word); } if (!modified) { @@ -103,14 +90,8 @@ describe("basic", () => { const data = 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); - } + for (const word of wordsInStatsv5Entries) { + expect(data).toContain(word); } if (!modified) { @@ -145,14 +126,8 @@ describe("basic", () => { const data = chunk.toString(); if (data.includes("index.js")) { - if (isWebpack5) { - for (const word of wordsInStatsv5) { - expect(data).toContain(word); - } - } else { - for (const word of wordsInStatsv4) { - expect(data).toContain(word); - } + for (const word of wordsInStatsv5) { + expect(data).toContain(word); } if (!modified) { diff --git a/test/watch/watch-variable/watch-variable.test.js b/test/watch/watch-variable/watch-variable.test.js index 5c0279e51d5..266e0870540 100644 --- a/test/watch/watch-variable/watch-variable.test.js +++ b/test/watch/watch-variable/watch-variable.test.js @@ -1,10 +1,9 @@ "use strict"; -const { runAndGetProcess, isWebpack5, processKill } = require("../../utils/test-utils"); +const { runAndGetProcess, processKill } = require("../../utils/test-utils"); const { writeFileSync } = require("fs"); const { resolve } = require("path"); -const wordsInStatsv4 = ["Hash", "Built at:", "main.js"]; const wordsInStatsv5 = ["asset", "index.js", "compiled successfully"]; describe("watch variable", () => { @@ -19,14 +18,8 @@ describe("watch variable", () => { expect(data).not.toContain("FAIL"); if (data.includes("index.js")) { - if (isWebpack5) { - for (const word of wordsInStatsv5) { - expect(data).toContain(word); - } - } else { - for (const word of wordsInStatsv4) { - expect(data).toContain(word); - } + for (const word of wordsInStatsv5) { + expect(data).toContain(word); } if (!modified) { @@ -54,14 +47,8 @@ describe("watch variable", () => { expect(data).not.toContain("FAIL"); if (data.includes("index.js")) { - if (isWebpack5) { - for (const word of wordsInStatsv5) { - expect(data).toContain(word); - } - } else { - for (const word of wordsInStatsv4) { - expect(data).toContain(word); - } + for (const word of wordsInStatsv5) { + expect(data).toContain(word); } if (!modified) {