Files
goodgo-platform/playwright.config.ts
Ho Ngoc Hai db0fe8b9b7 fix(e2e): unblock E2E test environment — CSP, CORS, and env var fixes
Root causes of web E2E failures:
1. CSP connect-src only included API origin for NODE_ENV=development,
   blocking test mode (NODE_ENV=test) from fetching API data
2. CORS_ORIGINS missing the test web port (3010), so API rejected
   cross-origin requests from the web app
3. NEXT_PUBLIC_API_URL not set in .env.test or playwright config,
   causing web app to default to port 3001 instead of test port 3011
4. Playwright webServer config didn't inherit parent env vars,
   so API server lacked Redis/Typesense/MinIO connection info

Fixes:
- next.config.js: CSP connect-src allows API origins for all non-prod envs
- next.config.js: image remotePatterns allow localhost in test mode
- .env.test: add NEXT_PUBLIC_API_URL and CORS_ORIGINS
- playwright.config.ts: spread process.env into webServer env configs
- e2e.yml: add NEXT_PUBLIC_API_URL, API_PORT, WEB_PORT to GH Actions env
- homepage.spec.ts: update stale assertions to match current UI

Result: 147/202 tests passing (111 API + 36 web), up from 37/91.
Remaining 55 web failures are stale UI assertions needing frontend update.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-13 01:55:04 +07:00

91 lines
2.6 KiB
TypeScript

import path from 'node:path';
import { defineConfig, devices } from '@playwright/test';
import { config } from 'dotenv';
// Load .env.test so webServer processes and tests use the test database
if (!process.env.CI) {
config({ path: path.resolve(__dirname, '.env.test'), override: true });
}
// Server ports — configurable via env to avoid conflicts with dev containers.
// Defaults match .env.test (3011/3010); GitHub Actions uses 3001/3000.
const API_PORT = process.env.API_PORT ?? '3001';
const WEB_PORT = process.env.WEB_PORT ?? '3000';
/**
* Playwright E2E configuration for Goodgo Platform.
*
* Projects:
* - "api" — tests against the NestJS API (port 3011 local CI / 3001 GH Actions)
* - "web" — tests against the Next.js frontend (port 3010 local CI / 3000 GH Actions)
*
* Database isolation:
* - globalSetup runs migrations + seed on the test DB
* - globalTeardown cleans up test-generated data after all tests
*/
export default defineConfig({
testDir: './e2e',
globalSetup: './e2e/global-setup.ts',
globalTeardown: './e2e/global-teardown.ts',
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
reporter: process.env.CI
? [['html', { open: 'never' }], ['github']]
: [['html', { open: 'on-failure' }]],
use: {
trace: 'on-first-retry',
screenshot: 'only-on-failure',
},
projects: [
// API E2E tests — no browser needed, uses APIRequestContext
{
name: 'api',
testDir: './e2e/api',
use: {
baseURL: process.env.API_BASE_URL ?? `http://localhost:${API_PORT}/api/v1/`,
},
},
// Web E2E tests — Chromium browser
{
name: 'web',
testDir: './e2e/web',
use: {
...devices['Desktop Chrome'],
baseURL: process.env.WEB_BASE_URL ?? `http://localhost:${WEB_PORT}`,
},
},
],
webServer: [
{
command: `PORT=${API_PORT} pnpm --filter @goodgo/api run dev`,
url: `http://localhost:${API_PORT}/api/v1/docs`,
reuseExistingServer: !process.env.CI,
timeout: 60_000,
env: {
...process.env as Record<string, string>,
NODE_ENV: 'test',
PORT: API_PORT,
DATABASE_URL: process.env.DATABASE_URL ?? '',
},
},
{
command: `pnpm exec next dev --port ${WEB_PORT}`,
cwd: './apps/web',
url: `http://localhost:${WEB_PORT}`,
reuseExistingServer: !process.env.CI,
timeout: 30_000,
env: {
...process.env as Record<string, string>,
PORT: WEB_PORT,
NODE_ENV: 'test',
NEXT_PUBLIC_API_URL: `http://localhost:${API_PORT}/api/v1`,
},
},
],
});