Spaces:
Sleeping
Sleeping
File size: 4,383 Bytes
05c5ed5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | # Playwright Test Strategy with First-User Testing
## Overview
This project uses Playwright's project dependencies to orchestrate complex test scenarios including first-user admin role testing.
## Test Execution Flow
```mermaid
graph TD
A[Start Tests] --> B{First User Tests?}
B -->|Yes| C[first-user-setup]
C --> D[Clear Database]
D --> E[first-user tests]
E --> F[Test Admin Role Assignment]
F --> G[setup]
B -->|No/Skip| G[setup]
G --> H[Seed Test Users]
H --> I[Create Auth States]
I --> J[chromium tests]
I --> K[admin tests]
I --> L[mobile tests]
```
## Available Test Commands
### Run Everything (First-User + Standard Tests)
```bash
pnpm test:e2e:all
# or just
pnpm test:e2e
```
This runs:
1. First-user tests with clean database
2. Seeds test users
3. Runs all standard tests
### Run Only First-User Tests
```bash
pnpm test:e2e:first-user
```
This runs:
1. Clears database
2. Tests first user gets admin role
3. Tests second user gets regular role
### Run Only Standard Tests (Skip First-User)
```bash
pnpm test:e2e:standard
```
This runs:
1. Seeds test users (if needed)
2. Creates auth states
3. Runs chromium + admin tests
### Run with UI
```bash
pnpm test:e2e:ui
```
Opens Playwright UI to run/debug any test configuration
## Project Configuration
### `first-user-setup` Project
- **Purpose**: Clear database for first-user testing
- **File**: `tests/lifecycle/first-user.setup.ts`
- **Runs**: Before first-user tests only
### `first-user` Project
- **Purpose**: Test first user admin role assignment
- **File**: `tests/auth/first-user-admin.spec.ts`
- **Dependencies**: `first-user-setup`
- **Database State**: Empty (0 users)
### `setup` Project
- **Purpose**: Seed users and create auth states
- **File**: `tests/lifecycle/auth-states.setup.ts`
- **Dependencies**: `first-user` (unless SKIP_FIRST_USER_TEST=1)
- **Database State**: Populated with test users
### `chromium` Project
- **Purpose**: Standard browser tests
- **Dependencies**: `setup`
- **Database State**: Seeded with test users
### `admin` Project
- **Purpose**: Admin functionality tests
- **Dependencies**: `setup`
- **Database State**: Seeded with admin user
## Environment Variables
### `SKIP_FIRST_USER_TEST=1`
Skip first-user tests and go straight to standard tests.
Useful for faster testing when not changing auth logic.
```bash
SKIP_FIRST_USER_TEST=1 pnpm test:e2e
```
## How It Works
### 1. Database State Management
The projects handle database state transitions:
- **Clean → First-User Tests → Seeded → Standard Tests**
### 2. Dependency Chain
Playwright ensures tests run in the correct order:
```
first-user-setup → first-user → setup → [chromium, admin, mobile]
```
### 3. Smart Seeding
The `setup` project checks if users exist before seeding:
- If < 3 users: Runs seed script
- If ≥ 3 users: Skips seeding
### 4. Auth State Creation
After seeding, `setup` creates browser auth states for:
- Admin user
- Editor user
- Regular user
These are saved as JSON files and reused by other tests.
## Troubleshooting
### Tests fail with "User already exists"
The database has leftover data. Run:
```bash
pnpm test:e2e:clean # Clean all test data
pnpm test:e2e # Run tests fresh
```
### First-user not getting admin role
1. Check Better Auth hook is enabled in `auth-instance.ts`
2. Verify database is truly empty before test
3. Check logs for "User creation hook" message
### Tests hanging on sign-in
1. Check for translation errors in console
2. Verify no duplicate keys in `messages/en.json`
3. Check for circular dependencies in role definitions
### Want to skip first-user tests
```bash
pnpm test:e2e:standard
# or
SKIP_FIRST_USER_TEST=1 pnpm test:e2e
```
## Best Practices
1. **Don't mix test types**: First-user tests need empty DB, standard tests need seeded DB
2. **Use projects for dependencies**: Let Playwright handle the orchestration
3. **Check logs**: Each setup phase logs its actions for debugging
4. **Clean state for CI**: CI should always run full suite to ensure clean state
## CI Configuration
```yaml
# Example GitHub Actions
- name: Run E2E Tests
run: |
# Always run full suite in CI for consistency
pnpm test:e2e:all
```
This ensures CI always tests:
1. First-user admin assignment
2. All standard functionality
3. Admin features
4. Mobile responsiveness |