Skip to content

Commit 37b73d5

Browse files
authored
test: add e2e test for WEBPACK_SERVE env variable (#4125)
1 parent f5a9d05 commit 37b73d5

File tree

4 files changed

+88
-25
lines changed

4 files changed

+88
-25
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ exports[`API Server.getFreePort should return the port when the port is undefine
110110

111111
exports[`API Server.getFreePort should throw the error when the port isn't found 1`] = `"busy"`;
112112

113+
exports[`API WEBPACK_SERVE environment variable should be present: console messages 1`] = `
114+
Array [
115+
"[HMR] Waiting for update signal from WDS...",
116+
"Hey.",
117+
"[webpack-dev-server] Hot Module Replacement enabled.",
118+
"[webpack-dev-server] Live Reloading enabled.",
119+
]
120+
`;
121+
122+
exports[`API WEBPACK_SERVE environment variable should be present: page errors 1`] = `Array []`;
123+
124+
exports[`API WEBPACK_SERVE environment variable should be present: response status 1`] = `200`;
125+
113126
exports[`API deprecated API should log warning when the "port" and "host" options from options different from arguments ('listen' method): console messages 1`] = `
114127
Array [
115128
"[HMR] Waiting for update signal from WDS...",

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ exports[`API Server.getFreePort should return the port when the port is undefine
110110

111111
exports[`API Server.getFreePort should throw the error when the port isn't found 1`] = `"busy"`;
112112

113+
exports[`API WEBPACK_SERVE environment variable should be present: console messages 1`] = `
114+
Array [
115+
"[HMR] Waiting for update signal from WDS...",
116+
"Hey.",
117+
"[webpack-dev-server] Hot Module Replacement enabled.",
118+
"[webpack-dev-server] Live Reloading enabled.",
119+
]
120+
`;
121+
122+
exports[`API WEBPACK_SERVE environment variable should be present: page errors 1`] = `Array []`;
123+
124+
exports[`API WEBPACK_SERVE environment variable should be present: response status 1`] = `200`;
125+
113126
exports[`API deprecated API should log warning when the "port" and "host" options from options different from arguments ('listen' method): console messages 1`] = `
114127
Array [
115128
"[HMR] Waiting for update signal from WDS...",

test/e2e/api.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,68 @@ const runBrowser = require("../helpers/run-browser");
99
const port = require("../ports-map").api;
1010

1111
describe("API", () => {
12+
describe("WEBPACK_SERVE environment variable", () => {
13+
const OLD_ENV = process.env;
14+
let server;
15+
let page;
16+
let browser;
17+
let pageErrors;
18+
let consoleMessages;
19+
20+
beforeEach(async () => {
21+
// this is important - it clears the cache
22+
jest.resetModules();
23+
24+
process.env = { ...OLD_ENV };
25+
26+
delete process.env.WEBPACK_SERVE;
27+
28+
({ page, browser } = await runBrowser());
29+
30+
pageErrors = [];
31+
consoleMessages = [];
32+
});
33+
34+
afterEach(async () => {
35+
await browser.close();
36+
await server.stop();
37+
process.env = OLD_ENV;
38+
});
39+
40+
it("should be present", async () => {
41+
expect(process.env.WEBPACK_SERVE).toBeUndefined();
42+
43+
page
44+
.on("console", (message) => {
45+
consoleMessages.push(message);
46+
})
47+
.on("pageerror", (error) => {
48+
pageErrors.push(error);
49+
});
50+
51+
const WebpackDevServer = require("../../lib/Server");
52+
53+
const compiler = webpack(config);
54+
server = new WebpackDevServer({ port }, compiler);
55+
56+
await server.start();
57+
58+
expect(process.env.WEBPACK_SERVE).toBe(true);
59+
60+
const response = await page.goto(`http://127.0.0.1:${port}/main`, {
61+
waitUntil: "networkidle0",
62+
});
63+
64+
expect(response.status()).toMatchSnapshot("response status");
65+
66+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
67+
"console messages"
68+
);
69+
70+
expect(pageErrors).toMatchSnapshot("page errors");
71+
});
72+
});
73+
1274
describe("latest async API", () => {
1375
it(`should work with async API`, async () => {
1476
const compiler = webpack(config);

test/server/Server.test.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -161,29 +161,4 @@ describe("Server", () => {
161161
await server.stop();
162162
});
163163
});
164-
165-
describe("WEBPACK_SERVE environment variable", () => {
166-
const OLD_ENV = process.env;
167-
168-
beforeEach(() => {
169-
// this is important - it clears the cache
170-
jest.resetModules();
171-
172-
process.env = { ...OLD_ENV };
173-
174-
delete process.env.WEBPACK_SERVE;
175-
});
176-
177-
afterEach(() => {
178-
process.env = OLD_ENV;
179-
});
180-
181-
it("should be present", () => {
182-
expect(process.env.WEBPACK_SERVE).toBeUndefined();
183-
184-
require("../../lib/Server");
185-
186-
expect(process.env.WEBPACK_SERVE).toBe(true);
187-
});
188-
});
189164
});

0 commit comments

Comments
 (0)