arbret/frontend/e2e/helpers/setup.ts
2025-12-26 20:04:46 +01:00

39 lines
1.5 KiB
TypeScript

import { test } from "@playwright/test";
import { resetDatabase } from "./reset-db";
/**
* Set language to English for e2e tests.
* E2E tests should only test in English according to requirements.
* This is applied globally via test.beforeEach in the setup file.
*
* Also sets NEXT_PUBLIC_API_URL based on worker index so each worker
* connects to its own backend instance, and resets the database before
* each test for isolation.
*/
test.beforeEach(async ({ context, request }, testInfo) => {
// Set API URL based on worker index
// Each worker gets its own backend (port 8001 + workerIndex)
const workerIndex = testInfo.workerIndex;
const basePort = parseInt(process.env.E2E_PORT_START || "8001", 10);
const backendPort = basePort + workerIndex;
const backendUrl = `http://localhost:${backendPort}`;
// Set environment variable for this test run
process.env.NEXT_PUBLIC_API_URL = backendUrl;
// Reset database before each test for isolation
// This must complete successfully before tests run to avoid race conditions
await resetDatabase(request);
// Small delay to ensure database transaction commits are visible
// This prevents race conditions where tests start before reset completes
await new Promise((resolve) => setTimeout(resolve, 100));
// Add init script to set English language before any page loads
// This must be called before any page.goto() calls
await context.addInitScript(() => {
if (typeof window !== "undefined") {
window.localStorage.setItem("arbret-locale", "en");
}
});
});