Skip to content

Commit 258907d

Browse files
committed
test: update WebSocket handling in client tests to use CDP sessions
1 parent a181f84 commit 258907d

File tree

2 files changed

+90
-17
lines changed

2 files changed

+90
-17
lines changed

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,38 @@ exports[`client option configure client entry should disable client entry: page
66

77
exports[`client option configure client entry should disable client entry: response status 1`] = `200`;
88

9-
exports[`client option default behaviour responds with a 200 status code for /ws path: console messages 1`] = `[]`;
9+
exports[`client option configure client entry should disable client entry: webSockets 1`] = `[]`;
1010

11-
exports[`client option default behaviour responds with a 200 status code for /ws path: page errors 1`] = `[]`;
11+
exports[`client option default behaviour responds with a 200 status code for / path: console messages 1`] = `
12+
[
13+
"[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.",
14+
"[HMR] Waiting for update signal from WDS...",
15+
]
16+
`;
1217

13-
exports[`client option default behaviour responds with a 200 status code for /ws path: response status 1`] = `200`;
18+
exports[`client option default behaviour responds with a 200 status code for / path: page errors 1`] = `[]`;
19+
20+
exports[`client option default behaviour responds with a 200 status code for / path: response status 1`] = `200`;
21+
22+
exports[`client option default behaviour responds with a 200 status code for / path: webSockets 1`] = `
23+
[
24+
"ws://localhost:8104/ws",
25+
]
26+
`;
1427

1528
exports[`client option override client entry should disable client entry: response status 1`] = `200`;
1629

17-
exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: console messages 1`] = `[]`;
30+
exports[`client option should respect path option responds with a websocket with the /foo/test/bar path: console messages 1`] = `
31+
[
32+
"[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.",
33+
"[HMR] Waiting for update signal from WDS...",
34+
]
35+
`;
1836

19-
exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: page errors 1`] = `[]`;
37+
exports[`client option should respect path option responds with a websocket with the /foo/test/bar path: page errors 1`] = `[]`;
2038

21-
exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: response status 1`] = `200`;
39+
exports[`client option should respect path option responds with a websocket with the /foo/test/bar path: webSockets 1`] = `
40+
[
41+
"ws://localhost:8104/foo/test/bar",
42+
]
43+
`;

test/e2e/client.test.js

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
const webpack = require("webpack");
44
const Server = require("../../lib/Server");
5-
const config = require("../fixtures/simple-config-other/webpack.config");
5+
const config = require("../fixtures/provide-plugin-ws-config/webpack.config");
66
const runBrowser = require("../helpers/run-browser");
7+
const sessionSubscribe = require("../helpers/session-subscribe");
78
const port = require("../ports-map")["client-option"];
89

910
describe("client option", () => {
@@ -42,7 +43,7 @@ describe("client option", () => {
4243
await server.stop();
4344
});
4445

45-
it("responds with a 200 status code for /ws path", async () => {
46+
it("responds with a 200 status code for / path", async () => {
4647
page
4748
.on("console", (message) => {
4849
consoleMessages.push(message);
@@ -51,7 +52,22 @@ describe("client option", () => {
5152
pageErrors.push(error);
5253
});
5354

54-
const response = await page.goto(`http://localhost:${port}/ws`, {
55+
const webSocketRequests = [];
56+
const session = await page.target().createCDPSession();
57+
58+
session.on("Network.webSocketCreated", (test) => {
59+
webSocketRequests.push(test);
60+
});
61+
62+
await session.send("Target.setAutoAttach", {
63+
autoAttach: true,
64+
flatten: true,
65+
waitForDebuggerOnStart: true,
66+
});
67+
68+
sessionSubscribe(session);
69+
70+
const response = await page.goto(`http://localhost:${port}/`, {
5571
waitUntil: "networkidle0",
5672
});
5773

@@ -60,6 +76,10 @@ describe("client option", () => {
6076

6177
expect(response.status()).toMatchSnapshot("response status");
6278

79+
expect(webSocketRequests.map((request) => request.url)).toMatchSnapshot(
80+
"webSockets",
81+
);
82+
6383
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
6484
"console messages",
6585
);
@@ -110,7 +130,7 @@ describe("client option", () => {
110130
await server.stop();
111131
});
112132

113-
it("responds with a 200 status code for /foo/test/bar path", async () => {
133+
it("responds with a websocket with the /foo/test/bar path", async () => {
114134
page
115135
.on("console", (message) => {
116136
consoleMessages.push(message);
@@ -119,14 +139,28 @@ describe("client option", () => {
119139
pageErrors.push(error);
120140
});
121141

122-
const response = await page.goto(
123-
`http://localhost:${port}/foo/test/bar`,
124-
{
125-
waitUntil: "networkidle0",
126-
},
127-
);
142+
const webSocketRequests = [];
143+
const session = await page.target().createCDPSession();
128144

129-
expect(response.status()).toMatchSnapshot("response status");
145+
session.on("Network.webSocketCreated", (test) => {
146+
webSocketRequests.push(test);
147+
});
148+
149+
await session.send("Target.setAutoAttach", {
150+
autoAttach: true,
151+
flatten: true,
152+
waitForDebuggerOnStart: true,
153+
});
154+
155+
sessionSubscribe(session);
156+
157+
await page.goto(`http://localhost:${port}/`, {
158+
waitUntil: "networkidle0",
159+
});
160+
161+
expect(webSocketRequests.map((request) => request.url)).toMatchSnapshot(
162+
"webSockets",
163+
);
130164

131165
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
132166
"console messages",
@@ -177,10 +211,27 @@ describe("client option", () => {
177211
pageErrors.push(error);
178212
});
179213

214+
const webSocketRequests = [];
215+
const session = await page.target().createCDPSession();
216+
217+
session.on("Network.webSocketCreated", (test) => {
218+
webSocketRequests.push(test);
219+
});
220+
221+
await session.send("Target.setAutoAttach", {
222+
autoAttach: true,
223+
flatten: true,
224+
waitForDebuggerOnStart: true,
225+
});
226+
227+
sessionSubscribe(session);
228+
180229
const response = await page.goto(`http://localhost:${port}/main.js`, {
181230
waitUntil: "networkidle0",
182231
});
183232

233+
expect(webSocketRequests).toMatchSnapshot("webSockets");
234+
184235
expect(response.status()).toMatchSnapshot("response status");
185236

186237
expect(await response.text()).not.toMatch(/client\/index\.js/);

0 commit comments

Comments
 (0)