Skip to content

Commit 906d27e

Browse files
committed
test: add error handling tests and configuration files for dev server
1 parent 8774afe commit 906d27e

9 files changed

Lines changed: 108 additions & 0 deletions

File tree

‎.prettierignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ test/build/config/error-commonjs/syntax-error.js
1111
test/build/config/error-array/webpack.config.js
1212
test/build/config/error-mjs/syntax-error.mjs
1313
test/configtest/with-config-path/syntax-error.config.js
14+
test/serve/error-handling/src/syntax-error.js
1415
test/build/build-errors/stats.json
1516

1617
/.nx/workspace-data

‎eslint.config.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default defineConfig([
1717
"test/configtest/with-config-path/syntax-error.config.js",
1818
"test/build/config-format/auto/webpack.config.js",
1919
"test/build/config-format/typescript-tsx/webpack.config.jsx",
20+
"test/serve/error-handling/src/syntax-error.js",
2021
]),
2122
{
2223
extends: [config],
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
mode: "development",
3+
entry: "./src/index.js",
4+
devServer: {
5+
devMiddleware: { unknownOption: true },
6+
},
7+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
mode: "development",
3+
entry: "./src/syntax-error.js",
4+
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
mode: "development",
3+
entry: "./src/index.js",
4+
devServer: {
5+
setupMiddlewares: () => {
6+
throw new Error("Injected middleware failure");
7+
},
8+
},
9+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("ok");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const broken = {;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
mode: "development",
3+
entry: "./src/index.js",
4+
performance: {
5+
hints: "warning",
6+
maxAssetSize: 1,
7+
maxEntrypointSize: 1,
8+
},
9+
};

0 commit comments

Comments
 (0)