Skip to content

Commit b087461

Browse files
committedJan 23, 2024
test: improve
1 parent 017dd9d commit b087461

8 files changed

+409
-88
lines changed
 

‎test/e2e/api.test.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const webpack = require("webpack");
55
const Server = require("../../lib/Server");
66
const config = require("../fixtures/client-config/webpack.config");
77
const runBrowser = require("../helpers/run-browser");
8+
const sessionSubscribe = require("../helpers/session-subscribe");
89
const port = require("../ports-map").api;
910

1011
describe("API", () => {
@@ -697,12 +698,20 @@ describe("API", () => {
697698
});
698699

699700
const webSocketRequests = [];
700-
const client = page._client;
701+
const session = await page.target().createCDPSession();
701702

702-
client.on("Network.webSocketCreated", (test) => {
703+
session.on("Network.webSocketCreated", (test) => {
703704
webSocketRequests.push(test);
704705
});
705706

707+
await session.send("Target.setAutoAttach", {
708+
autoAttach: true,
709+
flatten: true,
710+
waitForDebuggerOnStart: true,
711+
});
712+
713+
sessionSubscribe(session);
714+
706715
const response = await page.goto(`http://127.0.0.1:${port}/`, {
707716
waitUntil: "networkidle0",
708717
});

‎test/e2e/built-in-routes.test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ describe("Built in routes", () => {
6363
});
6464

6565
it("should handles HEAD request to sockjs bundle", async () => {
66-
await page.setRequestInterception(true);
67-
6866
page
6967
.on("console", (message) => {
7068
consoleMessages.push(message);
@@ -73,7 +71,9 @@ describe("Built in routes", () => {
7371
pageErrors.push(error);
7472
})
7573
.on("request", (interceptedRequest) => {
76-
interceptedRequest.continue({ method: "HEAD" });
74+
if (interceptedRequest.isInterceptResolutionHandled()) return;
75+
76+
interceptedRequest.continue({ method: "HEAD" }, 10);
7777
});
7878

7979
const response = await page.goto(
@@ -155,8 +155,6 @@ describe("Built in routes", () => {
155155
});
156156

157157
it("should handle HEAD request to directory index", async () => {
158-
await page.setRequestInterception(true);
159-
160158
page
161159
.on("console", (message) => {
162160
consoleMessages.push(message);
@@ -165,6 +163,8 @@ describe("Built in routes", () => {
165163
pageErrors.push(error);
166164
})
167165
.on("request", (interceptedRequest) => {
166+
if (interceptedRequest.isInterceptResolutionHandled()) return;
167+
168168
interceptedRequest.continue({ method: "HEAD" });
169169
});
170170

@@ -215,8 +215,6 @@ describe("Built in routes", () => {
215215
});
216216

217217
it("should handle HEAD request to magic async chunk", async () => {
218-
await page.setRequestInterception(true);
219-
220218
page
221219
.on("console", (message) => {
222220
consoleMessages.push(message);
@@ -225,6 +223,8 @@ describe("Built in routes", () => {
225223
pageErrors.push(error);
226224
})
227225
.on("request", (interceptedRequest) => {
226+
if (interceptedRequest.isInterceptResolutionHandled()) return;
227+
228228
interceptedRequest.continue({ method: "HEAD" });
229229
});
230230

‎test/e2e/ipc.test.js

+31-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const httpProxy = require("http-proxy");
99
const Server = require("../../lib/Server");
1010
const config = require("../fixtures/client-config/webpack.config");
1111
const runBrowser = require("../helpers/run-browser");
12+
const sessionSubscribe = require("../helpers/session-subscribe");
1213
const port1 = require("../ports-map").ipc;
1314

1415
const webSocketServers = ["ws", "sockjs"];
@@ -72,11 +73,19 @@ describe("web socket server URL", () => {
7273
const webSocketRequests = [];
7374

7475
if (webSocketServer === "ws") {
75-
const client = page._client;
76+
const session = await page.target().createCDPSession();
7677

77-
client.on("Network.webSocketCreated", (test) => {
78+
session.on("Network.webSocketCreated", (test) => {
7879
webSocketRequests.push(test);
7980
});
81+
82+
await session.send("Target.setAutoAttach", {
83+
autoAttach: true,
84+
flatten: true,
85+
waitForDebuggerOnStart: true,
86+
});
87+
88+
sessionSubscribe(session);
8089
} else {
8190
page.on("request", (request) => {
8291
if (/\/ws\//.test(request.url())) {
@@ -168,11 +177,19 @@ describe("web socket server URL", () => {
168177
const webSocketRequests = [];
169178

170179
if (webSocketServer === "ws") {
171-
const client = page._client;
180+
const session = await page.target().createCDPSession();
172181

173-
client.on("Network.webSocketCreated", (test) => {
182+
session.on("Network.webSocketCreated", (test) => {
174183
webSocketRequests.push(test);
175184
});
185+
186+
await session.send("Target.setAutoAttach", {
187+
autoAttach: true,
188+
flatten: true,
189+
waitForDebuggerOnStart: true,
190+
});
191+
192+
sessionSubscribe(session);
176193
} else {
177194
page.on("request", (request) => {
178195
if (/\/ws\//.test(request.url())) {
@@ -279,11 +296,19 @@ describe("web socket server URL", () => {
279296
const webSocketRequests = [];
280297

281298
if (webSocketServer === "ws") {
282-
const client = page._client;
299+
const session = await page.target().createCDPSession();
283300

284-
client.on("Network.webSocketCreated", (test) => {
301+
session.on("Network.webSocketCreated", (test) => {
285302
webSocketRequests.push(test);
286303
});
304+
305+
await session.send("Target.setAutoAttach", {
306+
autoAttach: true,
307+
flatten: true,
308+
waitForDebuggerOnStart: true,
309+
});
310+
311+
sessionSubscribe(session);
287312
} else {
288313
page.on("request", (request) => {
289314
if (/\/ws\//.test(request.url())) {

‎test/e2e/progress.test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ describe("progress", () => {
4040
.on("console", (message) => {
4141
consoleMessages.push(message);
4242
})
43-
.on("request", (requestObj) => {
44-
if (/\.hot-update\.(json|js)$/.test(requestObj.url())) {
43+
.on("request", (interceptedRequest) => {
44+
if (interceptedRequest.isInterceptResolutionHandled()) return;
45+
46+
if (/\.hot-update\.(json|js)$/.test(interceptedRequest.url())) {
4547
doHotUpdate = true;
4648
}
4749
});

‎test/e2e/web-socket-server-url.test.js

+332-67
Large diffs are not rendered by default.

‎test/e2e/web-socket-server.test.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const webpack = require("webpack");
44
const Server = require("../../lib/Server");
55
const config = require("../fixtures/client-config/webpack.config");
66
const runBrowser = require("../helpers/run-browser");
7+
const sessionSubscribe = require("../helpers/session-subscribe");
78
const port = require("../ports-map")["web-socket-server-test"];
89

910
describe("web socket server", () => {
@@ -34,13 +35,20 @@ describe("web socket server", () => {
3435
});
3536

3637
const webSocketRequests = [];
38+
const session = await page.target().createCDPSession();
3739

38-
const client = page._client;
39-
40-
client.on("Network.webSocketCreated", (test) => {
40+
session.on("Network.webSocketCreated", (test) => {
4141
webSocketRequests.push(test);
4242
});
4343

44+
await session.send("Target.setAutoAttach", {
45+
autoAttach: true,
46+
flatten: true,
47+
waitForDebuggerOnStart: true,
48+
});
49+
50+
sessionSubscribe(session);
51+
4452
await page.goto(`http://127.0.0.1:${port}/`, {
4553
waitUntil: "networkidle0",
4654
});

‎test/helpers/run-browser.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ function runBrowser(config) {
6262
body: "Empty",
6363
});
6464
} else {
65-
interceptedRequest.continue();
65+
interceptedRequest.continue(
66+
interceptedRequest.continueRequestOverrides(),
67+
10,
68+
);
6669
}
6770
});
6871

‎test/helpers/session-subscribe.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
module.exports = async function sessionSubscribe(session) {
4+
session.on("sessionattached", (s) => {
5+
sessionSubscribe(s);
6+
});
7+
session.send("Network.enable");
8+
session.send("Runtime.runIfWaitingForDebugger");
9+
};

0 commit comments

Comments
 (0)
Please sign in to comment.