Skip to content

Commit 2ddeec7

Browse files
snitin315alexander-akait
authored andcommitted
test: refactor host tests to cleanup properly (#4911)
1 parent 62e4e29 commit 2ddeec7

File tree

1 file changed

+71
-59
lines changed

1 file changed

+71
-59
lines changed

test/e2e/host.test.js

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,33 @@ describe("host", () => {
9090

9191
const { page, browser } = await runBrowser();
9292

93-
const pageErrors = [];
94-
const consoleMessages = [];
95-
96-
page
97-
.on("console", (message) => {
98-
consoleMessages.push(message);
99-
})
100-
.on("pageerror", (error) => {
101-
pageErrors.push(error);
93+
try {
94+
const pageErrors = [];
95+
const consoleMessages = [];
96+
97+
page
98+
.on("console", (message) => {
99+
consoleMessages.push(message);
100+
})
101+
.on("pageerror", (error) => {
102+
pageErrors.push(error);
103+
});
104+
105+
await page.goto(`http://${hostname}:${port}/`, {
106+
waitUntil: "networkidle0",
102107
});
103108

104-
await page.goto(`http://${hostname}:${port}/`, {
105-
waitUntil: "networkidle0",
106-
});
109+
expect(
110+
consoleMessages.map((message) => message.text())
111+
).toMatchSnapshot("console messages");
107112

108-
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
109-
"console messages"
110-
);
111-
112-
expect(pageErrors).toMatchSnapshot("page errors");
113-
114-
await browser.close();
115-
await server.stop();
113+
expect(pageErrors).toMatchSnapshot("page errors");
114+
} catch (error) {
115+
throw error;
116+
} finally {
117+
await browser.close();
118+
await server.stop();
119+
}
116120
});
117121

118122
it(`should work using "${host}" host and port as string`, async () => {
@@ -159,29 +163,33 @@ describe("host", () => {
159163

160164
const { page, browser } = await runBrowser();
161165

162-
const pageErrors = [];
163-
const consoleMessages = [];
166+
try {
167+
const pageErrors = [];
168+
const consoleMessages = [];
164169

165-
page
166-
.on("console", (message) => {
167-
consoleMessages.push(message);
168-
})
169-
.on("pageerror", (error) => {
170-
pageErrors.push(error);
171-
});
172-
173-
await page.goto(`http://${hostname}:${port}/`, {
174-
waitUntil: "networkidle0",
175-
});
170+
page
171+
.on("console", (message) => {
172+
consoleMessages.push(message);
173+
})
174+
.on("pageerror", (error) => {
175+
pageErrors.push(error);
176+
});
176177

177-
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
178-
"console messages"
179-
);
178+
await page.goto(`http://${hostname}:${port}/`, {
179+
waitUntil: "networkidle0",
180+
});
180181

181-
expect(pageErrors).toMatchSnapshot("page errors");
182+
expect(
183+
consoleMessages.map((message) => message.text())
184+
).toMatchSnapshot("console messages");
182185

183-
await browser.close();
184-
await server.stop();
186+
expect(pageErrors).toMatchSnapshot("page errors");
187+
} catch (error) {
188+
throw error;
189+
} finally {
190+
await browser.close();
191+
await server.stop();
192+
}
185193
});
186194

187195
it(`should work using "${host}" host and "auto" port`, async () => {
@@ -231,31 +239,35 @@ describe("host", () => {
231239
const address = server.server.address();
232240
const { page, browser } = await runBrowser();
233241

234-
const pageErrors = [];
235-
const consoleMessages = [];
236-
237-
page
238-
.on("console", (message) => {
239-
consoleMessages.push(message);
240-
})
241-
.on("pageerror", (error) => {
242-
pageErrors.push(error);
243-
});
242+
try {
243+
const pageErrors = [];
244+
const consoleMessages = [];
244245

245-
await page.goto(`http://${hostname}:${address.port}/`, {
246-
waitUntil: "networkidle0",
247-
});
246+
page
247+
.on("console", (message) => {
248+
consoleMessages.push(message);
249+
})
250+
.on("pageerror", (error) => {
251+
pageErrors.push(error);
252+
});
248253

249-
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
250-
"console messages"
251-
);
254+
await page.goto(`http://${hostname}:${address.port}/`, {
255+
waitUntil: "networkidle0",
256+
});
252257

253-
expect(pageErrors).toMatchSnapshot("page errors");
258+
expect(
259+
consoleMessages.map((message) => message.text())
260+
).toMatchSnapshot("console messages");
254261

255-
delete process.env.WEBPACK_DEV_SERVER_BASE_PORT;
262+
expect(pageErrors).toMatchSnapshot("page errors");
263+
} catch (error) {
264+
throw error;
265+
} finally {
266+
delete process.env.WEBPACK_DEV_SERVER_BASE_PORT;
256267

257-
await browser.close();
258-
await server.stop();
268+
await browser.close();
269+
await server.stop();
270+
}
259271
});
260272
}
261273

0 commit comments

Comments
 (0)