Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

✅ Fix flaky integration tests caused by races in loginWithEmail (#2539)

Luke Vella2f6049d5b5a7
diff --git a/apps/web/tests/admin-setup.spec.ts b/apps/web/tests/admin-setup.spec.ts
index d1edd8fa0..a727ff795 100644
--- a/apps/web/tests/admin-setup.spec.ts
+++ b/apps/web/tests/admin-setup.spec.ts
@@ -105,9 +105,14 @@ test.describe("Admin Setup Page Access", () => {
await page.goto("/admin-setup");
await expect(page.getByText("Are you the admin?")).toBeVisible();
- await page.getByRole("button", { name: "Make me an admin" }).click();
- await expect(page).toHaveURL("/control-panel", { timeout: 10000 });
+ // The button is visible before React hydrates it and its onClick
+ // handler is attached, so a click that lands too early is silently
+ // lost. Retry until the action redirects.
+ await expect(async () => {
+ await page.getByRole("button", { name: "Make me an admin" }).click();
+ await page.waitForURL("/control-panel", { timeout: 5000 });
+ }).toPass();
const user = await prisma.user.findUnique({
where: { email: INITIAL_ADMIN_TEST_EMAIL },
diff --git a/packages/test-helpers/src/login.ts b/packages/test-helpers/src/login.ts
index 8fb0eb3e4..6cd5400c9 100644
--- a/packages/test-helpers/src/login.ts
+++ b/packages/test-helpers/src/login.ts
@@ -1,4 +1,5 @@
import type { Page } from "@playwright/test";
+import { expect } from "@playwright/test";
import { getCode } from "./email";
@@ -9,17 +10,31 @@ export async function loginWithEmail(
await page.goto("/login");
await page.getByText("Welcome").waitFor();
- await page.getByPlaceholder("[email protected]").fill(email);
- await page.getByRole("button", { name: "Continue with email" }).click();
+ // The login form is visible before React has hydrated it, so input that
+ // lands too early can be silently lost. Retry until the next screen
+ // appears. The OTP is reused across resends (resendStrategy: "reuse"),
+ // so submitting more than once is safe.
+ await expect(async () => {
+ await page.getByPlaceholder("[email protected]").fill(email);
+ await page.getByRole("button", { name: "Continue with email" }).click();
+ await page
+ .getByRole("heading", { name: "Finish Logging In" })
+ .or(page.getByLabel("Password"))
+ .first()
+ .waitFor({ timeout: 5000 });
+ }).toPass();
if (password) {
await page.getByLabel("Password").fill(password);
await page.getByRole("button", { name: "Login with password" }).click();
} else {
- await page.getByRole("heading", { name: "Finish Logging In" }).waitFor();
const code = await getCode(email);
await page.getByLabel("Enter your 6-digit code").fill(code);
}
- await page.waitForLoadState("networkidle");
+ // Wait until the login flow navigates away before handing control back
+ // to the test. Waiting for "networkidle" returned too early — while the
+ // verification request was still in flight — so tests could navigate
+ // before the session cookie was set.
+ await page.waitForURL((url) => !url.pathname.startsWith("/login"));
}