|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/* eslint-disable jest/require-top-level-describe -- `describeDevServer6` is a version-gated `describe` the plugin cannot resolve */ |
| 4 | + |
| 5 | +const [devServerVersion] = require("webpack-dev-server/package.json").version; |
| 6 | +const { run, runWatch } = require("../../utils/test-utils"); |
| 7 | + |
| 8 | +const getGetPort = () => import("get-port"); |
| 9 | + |
| 10 | +// The CLI only owns the watch callback (stats printing and error handling) |
| 11 | +// when the dev server runs as a compiler plugin (webpack-dev-server 6+). |
| 12 | +const describeDevServer6 = devServerVersion === "5" ? describe.skip : describe; |
| 13 | + |
| 14 | +describeDevServer6("serve error handling", () => { |
| 15 | + let port; |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + port = await (await getGetPort()).default(); |
| 19 | + }); |
| 20 | + |
| 21 | + test("should print the stats with errors and keep serving when the compilation fails", async () => { |
| 22 | + const { stderr, stdout } = await runWatch( |
| 23 | + __dirname, |
| 24 | + ["serve", "--config", "error.config.js", "--port", port], |
| 25 | + { |
| 26 | + stdoutKillStr: /ERROR/, |
| 27 | + stderrKillStr: /Project is running at:/, |
| 28 | + }, |
| 29 | + ); |
| 30 | + |
| 31 | + expect(stdout).toContain("ERROR in"); |
| 32 | + expect(stderr).toContain("Project is running at:"); |
| 33 | + }); |
| 34 | + |
| 35 | + test("should print the stats with warnings using the '--fail-on-warnings' option", async () => { |
| 36 | + const { stderr, stdout } = await runWatch( |
| 37 | + __dirname, |
| 38 | + ["serve", "--config", "warning.config.js", "--fail-on-warnings", "--port", port], |
| 39 | + { |
| 40 | + stdoutKillStr: /WARNING/, |
| 41 | + stderrKillStr: /Project is running at:/, |
| 42 | + }, |
| 43 | + ); |
| 44 | + |
| 45 | + expect(stdout).toContain("WARNING"); |
| 46 | + expect(stderr).toContain("Project is running at:"); |
| 47 | + }); |
| 48 | + |
| 49 | + test("should log the error and exit when the dev server fails inside the watch run", async () => { |
| 50 | + const { exitCode, stderr, stdout } = await run(__dirname, [ |
| 51 | + "serve", |
| 52 | + "--config", |
| 53 | + "setup-failure.config.js", |
| 54 | + ]); |
| 55 | + |
| 56 | + expect(exitCode).toBe(2); |
| 57 | + expect(stderr).toContain("Injected middleware failure"); |
| 58 | + expect(stdout).toBeFalsy(); |
| 59 | + }); |
| 60 | + |
| 61 | + test("should log the validation error and exit when a middleware option is invalid", async () => { |
| 62 | + const { exitCode, stderr, stdout } = await run(__dirname, [ |
| 63 | + "serve", |
| 64 | + "--config", |
| 65 | + "bad-middleware.config.js", |
| 66 | + ]); |
| 67 | + |
| 68 | + expect(exitCode).toBe(2); |
| 69 | + expect(stderr).toContain( |
| 70 | + "Dev Middleware has been initialized using an options object that does not match the API schema", |
| 71 | + ); |
| 72 | + expect(stderr).not.toContain("at Server.setupMiddlewares"); |
| 73 | + expect(stdout).toBeFalsy(); |
| 74 | + }); |
| 75 | +}); |
0 commit comments