arbret/frontend/e2e/helpers/setup.ts
2025-12-26 19:21:34 +01:00

40 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
try {
await resetDatabase(request);
} catch (error) {
// If reset fails, log but don't fail the test
// This allows tests to run even if reset endpoint is unavailable
console.warn(`Failed to reset database: ${error}`);
}
// 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");
}
});
});