Skip to content

Commit 6ea0928

Browse files
committed
test: add API plugin tests and snapshots for webpack config integration
1 parent de2a8d0 commit 6ea0928

5 files changed

Lines changed: 212 additions & 201 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`API (plugin) plugin in webpack config should work when added to webpack config plugins array: console messages 1`] = `
4+
[
5+
"[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.",
6+
"[HMR] Waiting for update signal from WDS...",
7+
"Hey.",
8+
]
9+
`;
10+
11+
exports[`API (plugin) plugin in webpack config should work when added to webpack config plugins array: page errors 1`] = `[]`;
12+
13+
exports[`API (plugin) plugin in webpack config should work with output.clean: true: console messages 1`] = `
14+
[
15+
"[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.",
16+
"[HMR] Waiting for update signal from WDS...",
17+
"Hey.",
18+
]
19+
`;
20+
21+
exports[`API (plugin) plugin in webpack config should work with output.clean: true: page errors 1`] = `[]`;
22+
23+
exports[`API (plugin) should work with plugin API: console messages 1`] = `
24+
[
25+
"[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.",
26+
"[HMR] Waiting for update signal from WDS...",
27+
"Hey.",
28+
]
29+
`;
30+
31+
exports[`API (plugin) should work with plugin API: page errors 1`] = `[]`;

test/e2e/__snapshots__/api.test.js.snap.webpack5

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -165,33 +165,3 @@ exports[`API latest async API should work with callback API: console messages 1`
165165
`;
166166

167167
exports[`API latest async API should work with callback API: page errors 1`] = `[]`;
168-
169-
exports[`API plugin in webpack config should work when added to webpack config plugins array: console messages 1`] = `
170-
[
171-
"[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.",
172-
"[HMR] Waiting for update signal from WDS...",
173-
"Hey.",
174-
]
175-
`;
176-
177-
exports[`API plugin in webpack config should work when added to webpack config plugins array: page errors 1`] = `[]`;
178-
179-
exports[`API plugin in webpack config should work with output.clean: true: console messages 1`] = `
180-
[
181-
"[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.",
182-
"[HMR] Waiting for update signal from WDS...",
183-
"Hey.",
184-
]
185-
`;
186-
187-
exports[`API plugin in webpack config should work with output.clean: true: page errors 1`] = `[]`;
188-
189-
exports[`API should work with plugin API: console messages 1`] = `
190-
[
191-
"[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.",
192-
"[HMR] Waiting for update signal from WDS...",
193-
"Hey.",
194-
]
195-
`;
196-
197-
exports[`API should work with plugin API: page errors 1`] = `[]`;

test/e2e/api-plugin.test.js

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
"use strict";
2+
3+
const webpack = require("webpack");
4+
const Server = require("../../lib/Server");
5+
const config = require("../fixtures/client-config/webpack.config");
6+
const compile = require("../helpers/compile");
7+
const runBrowser = require("../helpers/run-browser");
8+
const port = require("../ports-map")["api-plugin"];
9+
10+
describe("API (plugin)", () => {
11+
it("should work with plugin API", async () => {
12+
const compiler = webpack(config);
13+
const server = new Server({ port });
14+
15+
server.apply(compiler);
16+
17+
await compile(compiler, port);
18+
19+
const { page, browser } = await runBrowser();
20+
21+
const pageErrors = [];
22+
const consoleMessages = [];
23+
24+
page
25+
.on("console", (message) => {
26+
consoleMessages.push(message);
27+
})
28+
.on("pageerror", (error) => {
29+
pageErrors.push(error);
30+
});
31+
32+
await page.goto(`http://127.0.0.1:${port}/`, {
33+
waitUntil: "networkidle0",
34+
});
35+
36+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
37+
"console messages",
38+
);
39+
expect(pageErrors).toMatchSnapshot("page errors");
40+
41+
await browser.close();
42+
await new Promise((resolve) => {
43+
compiler.close(resolve);
44+
});
45+
});
46+
47+
it("should not start the server multiple times on recompilation", async () => {
48+
const compiler = webpack(config);
49+
const server = new Server({ port });
50+
const setupSpy = jest.spyOn(server, "setup");
51+
const listenSpy = jest.spyOn(server, "listen");
52+
53+
server.apply(compiler);
54+
55+
const { watching } = await compile(compiler, port);
56+
57+
// Trigger a recompilation by invalidating
58+
await new Promise((resolve) => {
59+
watching.invalidate(() => {
60+
resolve();
61+
});
62+
});
63+
64+
// Wait for the recompilation to finish
65+
await new Promise((resolve) => {
66+
setTimeout(resolve, 2000);
67+
});
68+
69+
expect(setupSpy).toHaveBeenCalledTimes(1);
70+
expect(listenSpy).toHaveBeenCalledTimes(1);
71+
72+
setupSpy.mockRestore();
73+
listenSpy.mockRestore();
74+
await new Promise((resolve) => {
75+
compiler.close(resolve);
76+
});
77+
});
78+
79+
it("should stop the server cleanly via compiler.close()", async () => {
80+
const compiler = webpack(config);
81+
const server = new Server({ port });
82+
const stopSpy = jest.spyOn(server, "stop");
83+
84+
server.apply(compiler);
85+
86+
await compile(compiler, port);
87+
88+
await new Promise((resolve) => {
89+
compiler.close(resolve);
90+
});
91+
92+
expect(stopSpy).toHaveBeenCalledTimes(1);
93+
stopSpy.mockRestore();
94+
});
95+
96+
describe("plugin in webpack config", () => {
97+
it("should work when added to webpack config plugins array", async () => {
98+
const server = new Server({ port });
99+
const compiler = webpack({
100+
...config,
101+
plugins: [...config.plugins, server],
102+
});
103+
104+
await compile(compiler, port);
105+
106+
const { page, browser } = await runBrowser();
107+
108+
try {
109+
const pageErrors = [];
110+
const consoleMessages = [];
111+
112+
page
113+
.on("console", (message) => {
114+
consoleMessages.push(message);
115+
})
116+
.on("pageerror", (error) => {
117+
pageErrors.push(error);
118+
});
119+
120+
await page.goto(`http://127.0.0.1:${port}/`, {
121+
waitUntil: "networkidle0",
122+
});
123+
124+
expect(
125+
consoleMessages.map((message) => message.text()),
126+
).toMatchSnapshot("console messages");
127+
expect(pageErrors).toMatchSnapshot("page errors");
128+
} finally {
129+
await browser.close();
130+
await new Promise((resolve) => {
131+
compiler.close(resolve);
132+
});
133+
}
134+
});
135+
136+
it("should work with output.clean: true", async () => {
137+
const server = new Server({ port });
138+
const compiler = webpack({
139+
...config,
140+
output: {
141+
...config.output,
142+
clean: true,
143+
},
144+
plugins: [...config.plugins, server],
145+
});
146+
147+
await compile(compiler, port);
148+
149+
const { page, browser } = await runBrowser();
150+
151+
try {
152+
const pageErrors = [];
153+
const consoleMessages = [];
154+
155+
page
156+
.on("console", (message) => {
157+
consoleMessages.push(message);
158+
})
159+
.on("pageerror", (error) => {
160+
pageErrors.push(error);
161+
});
162+
163+
const response = await page.goto(`http://127.0.0.1:${port}/`, {
164+
waitUntil: "networkidle0",
165+
});
166+
167+
expect(response.status()).toBe(200);
168+
expect(
169+
consoleMessages.map((message) => message.text()),
170+
).toMatchSnapshot("console messages");
171+
expect(pageErrors).toMatchSnapshot("page errors");
172+
} finally {
173+
await browser.close();
174+
await new Promise((resolve) => {
175+
compiler.close(resolve);
176+
});
177+
}
178+
});
179+
});
180+
});

0 commit comments

Comments
 (0)