Skip to content

Commit d851b01

Browse files
committed
Improve resilience of e2e test
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 8a98fad commit d851b01

File tree

1 file changed

+64
-20
lines changed

1 file changed

+64
-20
lines changed

site/e2e/tests/presets/prebuilds.spec.ts

+64-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "node:path";
2-
import { expect, test } from "@playwright/test";
2+
import { type Locator, expect, test } from "@playwright/test";
33
import {
44
currentUser,
55
importTemplate,
@@ -14,18 +14,22 @@ test.beforeEach(async ({ page }) => {
1414
await login(page);
1515
});
1616

17+
const waitForBuildTimeout = 120_000; // Builds can take a while, let's give them at most 2m.
18+
19+
const templateFiles = [
20+
path.join(__dirname, "basic-presets-with-prebuild/main.tf"),
21+
path.join(__dirname, "basic-presets-with-prebuild/.terraform.lock.hcl"),
22+
];
23+
24+
const expectedPrebuilds = 2;
25+
1726
// NOTE: requires the `workspace-prebuilds` experiment enabled!
1827
test("create template with desired prebuilds", async ({ page, baseURL }) => {
1928
requiresLicense();
2029

21-
const expectedPrebuilds = 2;
22-
2330
// Create new template.
2431
const templateName = randomName();
25-
await importTemplate(page, templateName, [
26-
path.join(__dirname, "basic-presets-with-prebuild/main.tf"),
27-
path.join(__dirname, "basic-presets-with-prebuild/.terraform.lock.hcl"),
28-
]);
32+
await importTemplate(page, templateName, templateFiles);
2933

3034
await page.goto(
3135
`/workspaces?filter=owner:prebuilds%20template:${templateName}&page=1`,
@@ -34,13 +38,13 @@ test("create template with desired prebuilds", async ({ page, baseURL }) => {
3438

3539
// Wait for prebuilds to show up.
3640
const prebuilds = page.getByTestId(/^workspace-.+$/);
37-
await prebuilds.first().waitFor({ state: "visible", timeout: 120_000 });
38-
expect((await prebuilds.all()).length).toEqual(expectedPrebuilds);
41+
await waitForExpectedCount(prebuilds, expectedPrebuilds);
3942

4043
// Wait for prebuilds to start.
41-
const runningPrebuilds = page.getByTestId("build-status").getByText("Running");
42-
await runningPrebuilds.first().waitFor({ state: "visible", timeout: 120_000 });
43-
expect((await runningPrebuilds.all()).length).toEqual(expectedPrebuilds);
44+
const runningPrebuilds = page
45+
.getByTestId("build-status")
46+
.getByText("Running");
47+
await waitForExpectedCount(runningPrebuilds, expectedPrebuilds);
4448
});
4549

4650
// NOTE: requires the `workspace-prebuilds` experiment enabled!
@@ -51,10 +55,7 @@ test("claim prebuild matching selected preset", async ({ page, baseURL }) => {
5155

5256
// Create new template.
5357
const templateName = randomName();
54-
await importTemplate(page, templateName, [
55-
path.join(__dirname, "basic-presets-with-prebuild/main.tf"),
56-
path.join(__dirname, "basic-presets-with-prebuild/.terraform.lock.hcl"),
57-
]);
58+
await importTemplate(page, templateName, templateFiles);
5859

5960
await page.goto(
6061
`/workspaces?filter=owner:prebuilds%20template:${templateName}&page=1`,
@@ -63,11 +64,17 @@ test("claim prebuild matching selected preset", async ({ page, baseURL }) => {
6364

6465
// Wait for prebuilds to show up.
6566
const prebuilds = page.getByTestId(/^workspace-.+$/);
66-
await prebuilds.first().waitFor({ state: "visible", timeout: 120_000 });
67+
await waitForExpectedCount(prebuilds, expectedPrebuilds);
68+
69+
const previousWorkspaceNames = await Promise.all(
70+
(await prebuilds.all()).map((value) => {
71+
return value.getByText(/prebuild-.+/).textContent();
72+
}),
73+
);
6774

6875
// Wait for prebuilds to start.
69-
const runningPrebuilds = page.getByTestId("build-status").getByText("Running");
70-
await runningPrebuilds.first().waitFor({ state: "visible", timeout: 120_000 });
76+
let runningPrebuilds = page.getByTestId("build-status").getByText("Running");
77+
await waitForExpectedCount(runningPrebuilds, expectedPrebuilds);
7178

7279
// Open the first prebuild.
7380
await runningPrebuilds.first().click();
@@ -101,12 +108,49 @@ test("claim prebuild matching selected preset", async ({ page, baseURL }) => {
101108
// Wait for the workspace build display to be navigated to.
102109
const user = currentUser(page);
103110
await page.waitForURL(`/@${user.username}/${workspaceName}`, {
104-
timeout: 120_000, // Account for workspace build time.
111+
timeout: waitForBuildTimeout, // Account for workspace build time.
105112
});
106113

107114
// Validate the workspace metadata that it was indeed a claimed prebuild.
108115
const indicator = page.getByText("Was Prebuild");
109116
await indicator.waitFor({ timeout: 60_000 });
110117
const text = indicator.locator("xpath=..").getByText("Yes");
111118
await text.waitFor({ timeout: 30_000 });
119+
120+
// Navigate back to prebuilds page to see that a new prebuild replaced the claimed one.
121+
await page.goto(
122+
`/workspaces?filter=owner:prebuilds%20template:${templateName}&page=1`,
123+
{ waitUntil: "domcontentloaded" },
124+
);
125+
126+
// Wait for prebuilds to show up.
127+
const newPrebuilds = page.getByTestId(/^workspace-.+$/);
128+
await waitForExpectedCount(newPrebuilds, expectedPrebuilds);
129+
130+
const currentWorkspaceNames = await Promise.all(
131+
(await newPrebuilds.all()).map((value) => {
132+
return value.getByText(/prebuild-.+/).textContent();
133+
}),
134+
);
135+
136+
// Ensure the prebuilds have changed.
137+
expect(currentWorkspaceNames).not.toEqual(previousWorkspaceNames);
138+
139+
// Wait for prebuilds to start.
140+
runningPrebuilds = page.getByTestId("build-status").getByText("Running");
141+
await waitForExpectedCount(runningPrebuilds, expectedPrebuilds);
112142
});
143+
144+
function waitForExpectedCount(prebuilds: Locator, expectedCount: number) {
145+
return expect
146+
.poll(
147+
async () => {
148+
return (await prebuilds.all()).length === expectedCount;
149+
},
150+
{
151+
intervals: [100],
152+
timeout: waitForBuildTimeout,
153+
},
154+
)
155+
.toBe(true);
156+
}

0 commit comments

Comments
 (0)