Skip to content

Commit a6bea75

Browse files
snitin315alexander-akait
authored andcommitted
test: refactor ipc tests to cleanup properly (#4912)
1 parent ef1c89d commit a6bea75

File tree

1 file changed

+148
-136
lines changed

1 file changed

+148
-136
lines changed

test/e2e/ipc.test.js

Lines changed: 148 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -57,51 +57,55 @@ describe("web socket server URL", () => {
5757

5858
const { page, browser } = await runBrowser();
5959

60-
const pageErrors = [];
61-
const consoleMessages = [];
62-
63-
page
64-
.on("console", (message) => {
65-
consoleMessages.push(message);
66-
})
67-
.on("pageerror", (error) => {
68-
pageErrors.push(error);
60+
try {
61+
const pageErrors = [];
62+
const consoleMessages = [];
63+
64+
page
65+
.on("console", (message) => {
66+
consoleMessages.push(message);
67+
})
68+
.on("pageerror", (error) => {
69+
pageErrors.push(error);
70+
});
71+
72+
const webSocketRequests = [];
73+
74+
if (webSocketServer === "ws") {
75+
const client = page._client;
76+
77+
client.on("Network.webSocketCreated", (test) => {
78+
webSocketRequests.push(test);
79+
});
80+
} else {
81+
page.on("request", (request) => {
82+
if (/\/ws\//.test(request.url())) {
83+
webSocketRequests.push({ url: request.url() });
84+
}
85+
});
86+
}
87+
88+
await page.goto(`http://${proxyHost}:${proxyPort}/`, {
89+
waitUntil: "networkidle0",
6990
});
7091

71-
const webSocketRequests = [];
72-
73-
if (webSocketServer === "ws") {
74-
const client = page._client;
75-
76-
client.on("Network.webSocketCreated", (test) => {
77-
webSocketRequests.push(test);
78-
});
79-
} else {
80-
page.on("request", (request) => {
81-
if (/\/ws\//.test(request.url())) {
82-
webSocketRequests.push({ url: request.url() });
83-
}
84-
});
92+
const webSocketRequest = webSocketRequests[0];
93+
94+
expect(webSocketRequest.url).toContain(
95+
`${websocketURLProtocol}://${devServerHost}:${proxyPort}/ws`
96+
);
97+
expect(
98+
consoleMessages.map((message) => message.text())
99+
).toMatchSnapshot("console messages");
100+
expect(pageErrors).toMatchSnapshot("page errors");
101+
} catch (error) {
102+
throw error;
103+
} finally {
104+
proxy.close();
105+
106+
await browser.close();
107+
await server.stop();
85108
}
86-
87-
await page.goto(`http://${proxyHost}:${proxyPort}/`, {
88-
waitUntil: "networkidle0",
89-
});
90-
91-
const webSocketRequest = webSocketRequests[0];
92-
93-
expect(webSocketRequest.url).toContain(
94-
`${websocketURLProtocol}://${devServerHost}:${proxyPort}/ws`
95-
);
96-
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
97-
"console messages"
98-
);
99-
expect(pageErrors).toMatchSnapshot("page errors");
100-
101-
proxy.close();
102-
103-
await browser.close();
104-
await server.stop();
105109
});
106110

107111
it(`should work with the "ipc" option using "string" value ("${webSocketServer}")`, async () => {
@@ -149,51 +153,55 @@ describe("web socket server URL", () => {
149153

150154
const { page, browser } = await runBrowser();
151155

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);
156+
try {
157+
const pageErrors = [];
158+
const consoleMessages = [];
159+
160+
page
161+
.on("console", (message) => {
162+
consoleMessages.push(message);
163+
})
164+
.on("pageerror", (error) => {
165+
pageErrors.push(error);
166+
});
167+
168+
const webSocketRequests = [];
169+
170+
if (webSocketServer === "ws") {
171+
const client = page._client;
172+
173+
client.on("Network.webSocketCreated", (test) => {
174+
webSocketRequests.push(test);
175+
});
176+
} else {
177+
page.on("request", (request) => {
178+
if (/\/ws\//.test(request.url())) {
179+
webSocketRequests.push({ url: request.url() });
180+
}
181+
});
182+
}
183+
184+
await page.goto(`http://${proxyHost}:${proxyPort}/`, {
185+
waitUntil: "networkidle0",
161186
});
162187

163-
const webSocketRequests = [];
164-
165-
if (webSocketServer === "ws") {
166-
const client = page._client;
167-
168-
client.on("Network.webSocketCreated", (test) => {
169-
webSocketRequests.push(test);
170-
});
171-
} else {
172-
page.on("request", (request) => {
173-
if (/\/ws\//.test(request.url())) {
174-
webSocketRequests.push({ url: request.url() });
175-
}
176-
});
188+
const webSocketRequest = webSocketRequests[0];
189+
190+
expect(webSocketRequest.url).toContain(
191+
`${websocketURLProtocol}://${devServerHost}:${proxyPort}/ws`
192+
);
193+
expect(
194+
consoleMessages.map((message) => message.text())
195+
).toMatchSnapshot("console messages");
196+
expect(pageErrors).toMatchSnapshot("page errors");
197+
} catch (error) {
198+
throw error;
199+
} finally {
200+
proxy.close();
201+
202+
await browser.close();
203+
await server.stop();
177204
}
178-
179-
await page.goto(`http://${proxyHost}:${proxyPort}/`, {
180-
waitUntil: "networkidle0",
181-
});
182-
183-
const webSocketRequest = webSocketRequests[0];
184-
185-
expect(webSocketRequest.url).toContain(
186-
`${websocketURLProtocol}://${devServerHost}:${proxyPort}/ws`
187-
);
188-
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
189-
"console messages"
190-
);
191-
expect(pageErrors).toMatchSnapshot("page errors");
192-
193-
proxy.close();
194-
195-
await browser.close();
196-
await server.stop();
197205
});
198206

199207
// TODO un skip after implement new API
@@ -256,62 +264,66 @@ describe("web socket server URL", () => {
256264

257265
const { page, browser } = await runBrowser();
258266

259-
const pageErrors = [];
260-
const consoleMessages = [];
261-
262-
page
263-
.on("console", (message) => {
264-
consoleMessages.push(message);
265-
})
266-
.on("pageerror", (error) => {
267-
pageErrors.push(error);
267+
try {
268+
const pageErrors = [];
269+
const consoleMessages = [];
270+
271+
page
272+
.on("console", (message) => {
273+
consoleMessages.push(message);
274+
})
275+
.on("pageerror", (error) => {
276+
pageErrors.push(error);
277+
});
278+
279+
const webSocketRequests = [];
280+
281+
if (webSocketServer === "ws") {
282+
const client = page._client;
283+
284+
client.on("Network.webSocketCreated", (test) => {
285+
webSocketRequests.push(test);
286+
});
287+
} else {
288+
page.on("request", (request) => {
289+
if (/\/ws\//.test(request.url())) {
290+
webSocketRequests.push({ url: request.url() });
291+
}
292+
});
293+
}
294+
295+
await page.goto(`http://${proxyHost}:${proxyPort}/`, {
296+
waitUntil: "networkidle0",
268297
});
269298

270-
const webSocketRequests = [];
271-
272-
if (webSocketServer === "ws") {
273-
const client = page._client;
274-
275-
client.on("Network.webSocketCreated", (test) => {
276-
webSocketRequests.push(test);
277-
});
278-
} else {
279-
page.on("request", (request) => {
280-
if (/\/ws\//.test(request.url())) {
281-
webSocketRequests.push({ url: request.url() });
282-
}
299+
const webSocketRequest = webSocketRequests[0];
300+
301+
expect(webSocketRequest.url).toContain(
302+
`${websocketURLProtocol}://${devServerHost}:${proxyPort}/ws`
303+
);
304+
expect(
305+
consoleMessages.map((message) => message.text())
306+
).toMatchSnapshot("console messages");
307+
expect(pageErrors).toMatchSnapshot("page errors");
308+
} catch (error) {
309+
throw error;
310+
} finally {
311+
proxy.close();
312+
313+
await new Promise((resolve, reject) => {
314+
ipcServer.close((error) => {
315+
if (error) {
316+
reject(error);
317+
318+
return;
319+
}
320+
321+
resolve();
322+
});
283323
});
324+
await browser.close();
325+
await server.stop();
284326
}
285-
286-
await page.goto(`http://${proxyHost}:${proxyPort}/`, {
287-
waitUntil: "networkidle0",
288-
});
289-
290-
const webSocketRequest = webSocketRequests[0];
291-
292-
expect(webSocketRequest.url).toContain(
293-
`${websocketURLProtocol}://${devServerHost}:${proxyPort}/ws`
294-
);
295-
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
296-
"console messages"
297-
);
298-
expect(pageErrors).toMatchSnapshot("page errors");
299-
300-
proxy.close();
301-
302-
await new Promise((resolve, reject) => {
303-
ipcServer.close((error) => {
304-
if (error) {
305-
reject(error);
306-
307-
return;
308-
}
309-
310-
resolve();
311-
});
312-
});
313-
await browser.close();
314-
await server.stop();
315327
});
316328
}
317329
});

0 commit comments

Comments
 (0)