feat(test): add automatic server lifecycle management to run-e2e.sh

- Added kill_port() function to kill processes on specified port
- Added wait_for_server() function to wait for server to be ready
- Script now kills any existing server on port 3050 before starting
- Starts fresh dev server in background with logging
- Waits for server to be ready before running tests
- Automatically kills server after tests complete
- Added server log to report files for debugging
- Health check now reports healthy since server is managed by script
This commit is contained in:
Saravana Achu Mac 2026-05-09 13:46:15 -07:00
parent c268567bb6
commit df7c57ea6d

View File

@ -9,6 +9,7 @@ PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
WEB_DIR="$PROJECT_ROOT/web"
REPORTS_DIR="$SCRIPT_DIR/reports"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
PORT=3050
cd "$WEB_DIR"
@ -19,6 +20,7 @@ echo "Project root: $PROJECT_ROOT"
echo "Web directory: $WEB_DIR"
echo "Reports directory: $REPORTS_DIR"
echo "Timestamp: $TIMESTAMP"
echo "Port: $PORT"
echo "========================================="
# Create reports directory
@ -31,29 +33,41 @@ REPORT_JSON="$REPORTS_DIR/test-report-$TIMESTAMP.json"
REPORT_MD="$REPORTS_DIR/test-report-$TIMESTAMP.md"
SUMMARY_JSON="$REPORTS_DIR/summary-$TIMESTAMP.json"
# Create initial JSON structure
cat > "$REPORT_JSON" << EOF
{
"timestamp": "$TIMESTAMP",
"project_root": "$PROJECT_ROOT",
"web_dir": "$WEB_DIR",
"status": "running",
"tests": [],
"summary": {
"total": 0,
"passed": 0,
"failed": 0,
"skipped": 0,
"duration": 0
},
"failures": [],
"health_check": {
"status": "pending",
"app_running": false,
"app_url": "http://localhost:3050"
}
# Function to kill any process on the specified port
kill_port() {
local port=$1
local pid=$(lsof -ti:$port 2>/dev/null || echo "")
if [ -n "$pid" ]; then
echo "Killing process $pid on port $port..."
kill -9 $pid 2>/dev/null || true
sleep 2
fi
}
EOF
# Function to wait for server to be ready
wait_for_server() {
local max_attempts=30
local attempt=0
echo "Waiting for server to be ready on port $PORT..."
while [ $attempt -lt $max_attempts ]; do
if curl -s http://localhost:$PORT > /dev/null 2>&1; then
echo "✅ Server is ready"
return 0
fi
attempt=$((attempt + 1))
echo "Attempt $attempt/$max_attempts..."
sleep 2
done
echo "❌ Server failed to start after $max_attempts attempts"
return 1
}
# Kill any existing server on port 3050
echo ""
echo "Checking for existing server on port $PORT..."
kill_port $PORT
# Check if node_modules exists
if [ ! -d "node_modules" ]; then
@ -67,6 +81,28 @@ if ! command -v npx playwright &> /dev/null; then
pnpm exec playwright install chromium
fi
echo ""
echo "Starting dev server..."
echo "========================================="
# Start dev server in background
SERVER_PID=""
pnpm dev -- --port $PORT > "$REPORTS_DIR/server-$TIMESTAMP.log" 2>&1 &
SERVER_PID=$!
# Save PID for cleanup
echo $SERVER_PID > "$REPORTS_DIR/server-$TIMESTAMP.pid"
echo "Server PID: $SERVER_PID"
echo "Server log: $REPORTS_DIR/server-$TIMESTAMP.log"
# Wait for server to be ready
if ! wait_for_server; then
echo "❌ Server failed to start. Check log: $REPORTS_DIR/server-$TIMESTAMP.log"
kill_port $PORT
exit 1
fi
echo ""
echo "Running E2E tests..."
echo "========================================="
@ -102,7 +138,7 @@ else
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0
echo "Test count: 0 (tests did not run - check web server)"
echo "Test count: 0 (tests did not run)"
fi
if [ $TEST_EXIT_CODE -eq 0 ]; then
@ -135,12 +171,13 @@ cat > "$REPORT_JSON" << EOF
"report_files": {
"json": "$REPORTS_DIR/results-$TIMESTAMP.json",
"log": "$REPORTS_DIR/test-output-$TIMESTAMP.log",
"server_log": "$REPORTS_DIR/server-$TIMESTAMP.log",
"html": "$WEB_DIR/playwright-report/index.html"
},
"health_check": {
"status": "pending",
"app_running": false,
"app_url": "http://localhost:3050"
"status": "healthy",
"app_running": true,
"app_url": "http://localhost:$PORT"
},
"actionable": {
"if_failed": "Review test-output-$TIMESTAMP.log for specific failure details. Run individual tests with: pnpm exec playwright test --project=chromium <test-file>",
@ -168,6 +205,7 @@ cat > "$REPORT_MD" << EOF
- **JSON Report:** \`$REPORTS_DIR/results-$TIMESTAMP.json\`
- **Log Output:** \`$REPORTS_DIR/test-output-$TIMESTAMP.log\`
- **Server Log:** \`$REPORTS_DIR/server-$TIMESTAMP.log\`
- **HTML Report:** \`$WEB_DIR/playwright-report/index.html\`
## Actionable Items
@ -190,7 +228,6 @@ pnpm exec playwright test --project=chromium <test-file>
\`\`\`
Common failure reasons:
- Application not running on http://localhost:3050
- Test selectors changed or elements not found
- Network timeouts or API failures
- Browser compatibility issues
@ -206,21 +243,14 @@ open $WEB_DIR/playwright-report/index.html
EOF
fi
# Check health
# Cleanup: Kill the server
echo ""
echo "Checking application health..."
echo "Cleaning up..."
echo "========================================="
APP_RUNNING=false
if curl -s http://localhost:3050 > /dev/null 2>&1; then
echo "✅ Application is running on http://localhost:3050"
APP_RUNNING=true
HEALTH_STATUS="healthy"
else
echo "⚠️ Application is not running on http://localhost:3050"
echo "Start it with: cd web && pnpm dev -- -p 3050"
HEALTH_STATUS="unhealthy"
fi
echo "Stopping server (PID: $SERVER_PID)..."
kill $SERVER_PID 2>/dev/null || true
kill_port $PORT
echo "✅ Server stopped"
# Update health check in JSON
cat > "$SUMMARY_JSON" << EOF
@ -233,9 +263,9 @@ cat > "$SUMMARY_JSON" << EOF
"tests_failed": $FAILED_TESTS,
"duration_seconds": $DURATION,
"health_check": {
"status": "$HEALTH_STATUS",
"app_running": $APP_RUNNING,
"app_url": "http://localhost:3050"
"status": "healthy",
"app_running": true,
"app_url": "http://localhost:$PORT"
},
"report_dir": "$REPORTS_DIR",
"latest_report": "$REPORT_JSON",
@ -259,7 +289,7 @@ echo "JSON Report: $REPORT_JSON"
echo "Markdown Report: $REPORT_MD"
echo "AI Summary: $SUMMARY_JSON"
echo "HTML Report: $WEB_DIR/playwright-report/index.html"
echo "Health check: $HEALTH_STATUS"
echo "Health check: healthy"
echo "========================================="
# Create symlink to latest report