test setup

This commit is contained in:
2026-01-04 00:39:17 +01:00
parent 5308d3ee61
commit 99092e2759
10 changed files with 638 additions and 106 deletions

View File

@@ -60,21 +60,99 @@ Access:
- `npm run docker:prod:clean` - Stop and remove volumes
### Testing (Docker)
The goal of Docker-backed tests is to catch *wiring* issues between Website ↔ API (wrong hostnames/ports/env vars, missing CORS for credentialed requests, etc.) in a deterministic environment.
- `npm run test:docker:website` - Start API/DB in Docker, run website locally via Playwright, and execute e2e tests.
- Uses [`docker-compose.test.yml`](docker-compose.test.yml:1) for API and PostgreSQL.
- Playwright starts the website locally via `webServer` config (not in Docker).
- Tests run against `http://localhost:3000` (website) talking to `http://localhost:3101` (API).
- Validates that pages render, middleware works, and API connections succeed.
#### Available Commands
**Important**: The website runs locally (not in Docker) to avoid Next.js SWC/compilation issues in containers.
**Unified E2E Testing (Recommended):**
- `npm run test:e2e:website` - Run complete e2e test suite
- `npm run docker:e2e:up` - Start all e2e services
- `npm run docker:e2e:down` - Stop e2e services
- `npm run docker:e2e:logs` - View e2e service logs
- `npm run docker:e2e:ps` - Check e2e service status
- `npm run docker:e2e:clean` - Clean e2e environment
Supporting scripts:
- `npm run docker:test:deps` - Verify monorepo dependencies are installed.
- `npm run docker:test:up` - Start API and PostgreSQL containers.
- `npm run docker:test:wait` - Wait for API health check at `http://localhost:3101/health`.
- `npm run docker:test:down` - Stop containers and clean up.
**Legacy Testing (Deprecated):**
- `npm run test:docker:website` - Run legacy hybrid tests
- `npm run docker:test:up` - Start legacy API/DB
- `npm run docker:test:down` - Stop legacy services
- `npm run docker:test:clean` - Clean legacy environment
#### Quick Comparison
| Feature | Legacy (Hybrid) | Unified (E2E) |
|---------|-----------------|---------------|
| Website | Local (Playwright webServer) | Docker container |
| API | Docker container | Docker container |
| Database | Docker container | Docker container |
| Playwright | Local | Docker container |
| SWC Issues | ❌ Yes | ✅ No |
| CI Compatible | ❌ No | ✅ Yes |
| Single Command | ❌ No | ✅ Yes |
| Port Conflicts | ❌ Possible | ✅ No |
#### Unified E2E Test Environment (Recommended)
The new unified e2e test environment runs **everything in Docker** - website, API, database, and Playwright tests. This eliminates the hybrid approach and solves Next.js SWC compilation issues.
**Quick Start:**
```bash
# Run complete e2e test suite
npm run test:e2e:website
# Or step-by-step:
npm run docker:e2e:up # Start all services
npm run docker:e2e:logs # View logs
npm run docker:e2e:down # Stop services
npm run docker:e2e:clean # Clean everything
```
**What this does:**
- Builds optimized website image with all SWC dependencies
- Starts PostgreSQL database (port 5434)
- Starts API server (port 3101)
- Starts website server (port 3100)
- Runs Playwright tests in container
- All services communicate via isolated Docker network
**Architecture:**
```
┌─────────────────────────────────────────┐
│ Docker Network: gridpilot-e2e-network │
│ │
│ ┌──────────┐ ┌──────────┐ ┌───────┐ │
│ │ Playwright│→│ Website │→│ API │ │
│ │ Runner │ │ (Next.js)│ │(NestJS)│ │
│ └──────────┘ └──────────┘ └───────┘ │
│ ↓ ↓ ↓ │
│ └──────────────┴────────┴──────┘
│ ↓
│ PostgreSQL DB
└─────────────────────────────────────────┘
```
**Benefits:**
-**Fully containerized** - identical to CI environment
-**No SWC issues** - optimized Dockerfile with build tools
-**No port conflicts** - isolated network and unique ports
-**Single command** - one script runs everything
-**Deterministic** - no local dependencies
#### Legacy Testing (Deprecated)
The old hybrid approach (API/DB in Docker, website locally) is still available but deprecated:
- `npm run test:docker:website` - Start API/DB in Docker, run website locally via Playwright
- Uses [`docker-compose.test.yml`](docker-compose.test.yml:1)
- **Note**: This approach has SWC compilation issues and won't work in CI
**Supporting scripts (legacy):**
- `npm run docker:test:deps` - Verify monorepo dependencies
- `npm run docker:test:up` - Start API and PostgreSQL
- `npm run docker:test:wait` - Wait for API health
- `npm run docker:test:down` - Stop containers
**Recommendation**: Use the unified e2e environment above instead.
## Environment Variables
@@ -117,7 +195,31 @@ The single source of truth for "what base URL should I use?" is [`getWebsiteApiB
- `NEXT_PUBLIC_API_BASE_URL=http://localhost:3001` (browser → host port)
- `API_BASE_URL=http://api:3000` (website container → api container)
#### Test Docker defaults (docker-compose.test.yml)
#### E2E Docker defaults (docker-compose.e2e.yml)
This stack runs **everything in Docker** for fully containerized e2e testing:
- Website: `http://website:3000` (containerized Next.js, exposed as `localhost:3100`)
- API: `http://api:3000` (containerized NestJS, exposed as `localhost:3101`)
- PostgreSQL: `db:5432` (containerized, exposed as `localhost:5434`)
- Playwright: Runs in container, connects via Docker network
- `NEXT_PUBLIC_API_BASE_URL=http://api:3000` (browser → container)
- `API_BASE_URL=http://api:3000` (website → API container)
**Key differences from legacy approach**:
- ✅ Website runs in Docker (no SWC issues)
- ✅ Playwright runs in Docker (identical to CI)
- ✅ All services on isolated network
- ✅ No port conflicts with local dev
- ✅ Single command execution
**Accessing services during development**:
- Website: `http://localhost:3100`
- API: `http://localhost:3101`
- Database: `localhost:5434`
#### Test Docker defaults (docker-compose.test.yml) - Legacy
**Deprecated**: Use `docker-compose.e2e.yml` instead.
This stack is intended for deterministic smoke tests and uses different host ports to avoid colliding with `docker:dev`:
- Website: `http://localhost:3000` (started by Playwright webServer, not Docker)
@@ -131,7 +233,48 @@ This stack is intended for deterministic smoke tests and uses different host por
- The API is a real TypeORM/PostgreSQL server (not a mock) for testing actual database interactions.
- Playwright automatically starts the website server before running tests.
#### Troubleshooting
#### Troubleshooting (E2E)
**Common Issues:**
- **"Website not building"**: Ensure Docker has enough memory (4GB+). SWC compilation is memory-intensive.
- **"Port already in use"**: Use `npm run docker:e2e:down` to stop conflicting services.
- **"Module not found"**: Run `npm run docker:e2e:clean` to rebuild from scratch.
- **"Database connection failed"**: Wait for health checks. Use `npm run docker:e2e:logs` to check status.
- **"Playwright timeout"**: Increase timeout in `playwright.website.config.ts` if needed.
**Debug Commands:**
```bash
# View all service logs
npm run docker:e2e:logs
# Check service status
npm run docker:e2e:ps
# Clean everything and restart
npm run docker:e2e:clean && npm run docker:e2e:up
# Run specific service logs
docker-compose -f docker-compose.e2e.yml logs -f website
docker-compose -f docker-compose.e2e.yml logs -f api
docker-compose -f docker-compose.e2e.yml logs -f db
```
**Migration from Legacy to Unified:**
If you were using the old `test:docker:website` approach:
1. **Stop old services**: `npm run docker:test:down`
2. **Clean up**: `npm run docker:test:clean`
3. **Use new approach**: `npm run test:e2e:website`
The new approach is:
- ✅ More reliable (no SWC issues)
- ✅ Faster (no local server startup)
- ✅ CI-compatible (identical environment)
- ✅ Simpler (single command)
#### Troubleshooting (Legacy - Deprecated)
- **Port conflicts**: If `docker:dev` is running, use `npm run docker:dev:down` before `npm run test:docker:website` to avoid port conflicts (dev uses 3001, test uses 3101).
- **Website not starting**: Playwright's webServer may fail if dependencies are missing. Run `npm install` first.
- **Cookie errors**: The `WebsiteAuthManager` requires both `url` and `path` properties for cookies. Check Playwright version compatibility.
@@ -312,8 +455,11 @@ Before deploying to production:
.
├── docker-compose.dev.yml # Development orchestration
├── docker-compose.prod.yml # Production orchestration
├── docker-compose.e2e.yml # E2E testing orchestration (NEW)
├── docker-compose.test.yml # Legacy test orchestration (deprecated)
├── .env.development # Dev environment variables
├── .env.production # Prod environment variables
├── .env.test.example # Test env template
├── apps/
│ ├── api/
│ │ ├── Dockerfile.dev # API dev image
@@ -322,6 +468,18 @@ Before deploying to production:
│ └── website/
│ ├── Dockerfile.dev # Website dev image
│ ├── Dockerfile.prod # Website prod image
│ ├── Dockerfile.e2e # E2E optimized image (NEW)
│ └── .dockerignore
├── playwright.website.config.ts # E2E test config (updated)
├── playwright.website-integration.config.ts
├── playwright.smoke.config.ts
├── package.json # Updated scripts (NEW commands)
└── nginx/
└── nginx.conf # Nginx configuration
└── nginx.conf # Nginx configuration
```
**Key Changes for E2E Testing:**
- `docker-compose.e2e.yml` - Unified test environment
- `apps/website/Dockerfile.e2e` - SWC-optimized Next.js image
- Updated `playwright.website.config.ts` - Containerized setup
- New npm scripts in `package.json`