Skip to content

Commit ef1c89d

Browse files
snitin315alexander-akait
authored andcommitted
test: refactor lazy compilation and logging tests to cleanup properly (#4913)
1 parent be84c4f commit ef1c89d

File tree

2 files changed

+109
-97
lines changed

2 files changed

+109
-97
lines changed

test/e2e/lazy-compilation.test.js

Lines changed: 76 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,39 @@ describe("lazy compilation", () => {
1717

1818
const { page, browser } = await runBrowser();
1919

20-
const pageErrors = [];
21-
const consoleMessages = [];
22-
23-
page
24-
.on("console", (message) => {
25-
consoleMessages.push(message.text());
26-
})
27-
.on("pageerror", (error) => {
28-
pageErrors.push(error);
20+
try {
21+
const pageErrors = [];
22+
const consoleMessages = [];
23+
24+
page
25+
.on("console", (message) => {
26+
consoleMessages.push(message.text());
27+
})
28+
.on("pageerror", (error) => {
29+
pageErrors.push(error);
30+
});
31+
32+
await page.goto(`http://127.0.0.1:${port}/test.html`, {
33+
waitUntil: "domcontentloaded",
34+
});
35+
await new Promise((resolve) => {
36+
const interval = setInterval(() => {
37+
if (consoleMessages.includes("Hey.")) {
38+
clearInterval(interval);
39+
40+
resolve();
41+
}
42+
}, 100);
2943
});
3044

31-
await page.goto(`http://127.0.0.1:${port}/test.html`, {
32-
waitUntil: "domcontentloaded",
33-
});
34-
await new Promise((resolve) => {
35-
const interval = setInterval(() => {
36-
if (consoleMessages.includes("Hey.")) {
37-
clearInterval(interval);
38-
39-
resolve();
40-
}
41-
}, 100);
42-
});
43-
44-
expect(consoleMessages).toMatchSnapshot("console messages");
45-
expect(pageErrors).toMatchSnapshot("page errors");
46-
47-
await browser.close();
48-
await server.stop();
45+
expect(consoleMessages).toMatchSnapshot("console messages");
46+
expect(pageErrors).toMatchSnapshot("page errors");
47+
} catch (error) {
48+
throw error;
49+
} finally {
50+
await browser.close();
51+
await server.stop();
52+
}
4953
});
5054

5155
it.skip(`should work with multiple entries`, async () => {
@@ -56,49 +60,53 @@ describe("lazy compilation", () => {
5660

5761
const { page, browser } = await runBrowser();
5862

59-
const pageErrors = [];
60-
const consoleMessages = [];
63+
try {
64+
const pageErrors = [];
65+
const consoleMessages = [];
66+
67+
page
68+
.on("console", (message) => {
69+
consoleMessages.push(message.text());
70+
})
71+
.on("pageerror", (error) => {
72+
pageErrors.push(error);
73+
});
6174

62-
page
63-
.on("console", (message) => {
64-
consoleMessages.push(message.text());
65-
})
66-
.on("pageerror", (error) => {
67-
pageErrors.push(error);
75+
await page.goto(`http://127.0.0.1:${port}/test-one.html`, {
76+
waitUntil: "domcontentloaded",
77+
});
78+
await new Promise((resolve) => {
79+
const interval = setInterval(() => {
80+
console.log(consoleMessages);
81+
if (consoleMessages.includes("One.")) {
82+
clearInterval(interval);
83+
84+
resolve();
85+
}
86+
}, 100);
87+
});
88+
89+
await page.goto(`http://127.0.0.1:${port}/test-two.html`, {
90+
waitUntil: "domcontentloaded",
91+
});
92+
await new Promise((resolve) => {
93+
const interval = setInterval(() => {
94+
console.log(consoleMessages);
95+
if (consoleMessages.includes("Two.")) {
96+
clearInterval(interval);
97+
98+
resolve();
99+
}
100+
}, 100);
68101
});
69102

70-
await page.goto(`http://127.0.0.1:${port}/test-one.html`, {
71-
waitUntil: "domcontentloaded",
72-
});
73-
await new Promise((resolve) => {
74-
const interval = setInterval(() => {
75-
console.log(consoleMessages);
76-
if (consoleMessages.includes("One.")) {
77-
clearInterval(interval);
78-
79-
resolve();
80-
}
81-
}, 100);
82-
});
83-
84-
await page.goto(`http://127.0.0.1:${port}/test-two.html`, {
85-
waitUntil: "domcontentloaded",
86-
});
87-
await new Promise((resolve) => {
88-
const interval = setInterval(() => {
89-
console.log(consoleMessages);
90-
if (consoleMessages.includes("Two.")) {
91-
clearInterval(interval);
92-
93-
resolve();
94-
}
95-
}, 100);
96-
});
97-
98-
expect(consoleMessages).toMatchSnapshot("console messages");
99-
expect(pageErrors).toMatchSnapshot("page errors");
100-
101-
await browser.close();
102-
await server.stop();
103+
expect(consoleMessages).toMatchSnapshot("console messages");
104+
expect(pageErrors).toMatchSnapshot("page errors");
105+
} catch (error) {
106+
throw error;
107+
} finally {
108+
await browser.close();
109+
await server.stop();
110+
}
103111
});
104112
});

test/e2e/logging.test.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -204,41 +204,45 @@ describe("logging", () => {
204204

205205
const { page, browser } = await runBrowser();
206206

207-
const consoleMessages = [];
207+
try {
208+
const consoleMessages = [];
208209

209-
page.on("console", (message) => {
210-
consoleMessages.push(message);
211-
});
212-
213-
await page.goto(`http://localhost:${port}/`, {
214-
waitUntil: "networkidle0",
215-
});
216-
217-
if (testCase.devServerOptions && testCase.devServerOptions.static) {
218-
fs.writeFileSync(
219-
path.join(testCase.devServerOptions.static, "./foo.txt"),
220-
"Text"
221-
);
210+
page.on("console", (message) => {
211+
consoleMessages.push(message);
212+
});
222213

223-
await page.waitForNavigation({
214+
await page.goto(`http://localhost:${port}/`, {
224215
waitUntil: "networkidle0",
225216
});
226-
}
227217

228-
expect(
229-
consoleMessages.map((message) =>
230-
message
231-
.text()
232-
.replace(/\\/g, "/")
233-
.replace(
234-
new RegExp(process.cwd().replace(/\\/g, "/"), "g"),
235-
"<cwd>"
236-
)
237-
)
238-
).toMatchSnapshot();
218+
if (testCase.devServerOptions && testCase.devServerOptions.static) {
219+
fs.writeFileSync(
220+
path.join(testCase.devServerOptions.static, "./foo.txt"),
221+
"Text"
222+
);
223+
224+
await page.waitForNavigation({
225+
waitUntil: "networkidle0",
226+
});
227+
}
239228

240-
await browser.close();
241-
await server.stop();
229+
expect(
230+
consoleMessages.map((message) =>
231+
message
232+
.text()
233+
.replace(/\\/g, "/")
234+
.replace(
235+
new RegExp(process.cwd().replace(/\\/g, "/"), "g"),
236+
"<cwd>"
237+
)
238+
)
239+
).toMatchSnapshot();
240+
} catch (error) {
241+
throw error;
242+
} finally {
243+
await browser.close();
244+
await server.stop();
245+
}
242246
});
243247
});
244248
});

0 commit comments

Comments
 (0)