feat: add Playwright testing setup and update deployment script

This commit is contained in:
Peter Wood
2025-04-22 13:48:12 -04:00
parent 6d12242cfb
commit deb2134b82
3 changed files with 104 additions and 3 deletions

5
.gitignore vendored
View File

@@ -48,3 +48,8 @@ report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
.Trashes .Trashes
ehthumbs.db ehthumbs.db
Thumbs.db Thumbs.db
# playwright
tests/
test-results/
playwright-report/

View File

@@ -6,13 +6,64 @@ set -e
# It stops any existing container, builds a new Docker image, and runs the container. # It stops any existing container, builds a new Docker image, and runs the container.
# Ensure the script is run from the directory containing the Dockerfile # Ensure the script is run from the directory containing the Dockerfile
# and the application code. # and the application code.
# Usage: sudo ./deploy.sh
# This script also includes a testing stage using Playwright.
# It builds the application using Vite, starts a preview server, and runs Playwright tests.
# If the tests pass, it proceeds with the Docker deployment.
# Usage: ./deploy.sh
# Define container and image names # Define container and image names
CONTAINER_NAME="pokemon-app" CONTAINER_NAME="pokemon-app"
IMAGE_NAME="pokemon-finder" IMAGE_NAME="pokemon-finder"
HOST_PORT=8080 HOST_PORT=8080
CONTAINER_PORT=80 CONTAINER_PORT=80
PREVIEW_PORT=4173 # Default Vite preview port
# --- Pre-checks and Build ---
echo "Ensuring dependencies are installed..."
npm install
if [ $? -ne 0 ]; then
echo "npm install failed!"
exit 1
fi
echo "Building the application with Vite..."
npm run build
if [ $? -ne 0 ]; then
echo "Vite build (npm run build) failed!"
exit 1
fi
# --- Testing Stage ---
echo "Starting Vite preview server for testing..."
# Start in background and get PID
npm run preview -- --port $PREVIEW_PORT &
PREVIEW_PID=$!
# Wait a moment for the server to start (adjust sleep time if needed)
sleep 5
echo "Running Playwright tests..."
# Run tests; Playwright will use http://localhost:4173 based on test config/defaults
npx playwright test
TEST_RESULT=$?
echo "Stopping Vite preview server (PID: $PREVIEW_PID)..."
kill $PREVIEW_PID
# Wait for the process to terminate
wait $PREVIEW_PID 2>/dev/null
if [ $TEST_RESULT -ne 0 ]; then
echo "Playwright tests failed! Aborting deployment."
exit 1
else
echo "Playwright tests passed."
fi
# --- Deployment Stage (Only if tests passed) ---
echo "Proceeding with Docker deployment..."
# Stop the existing container (ignore errors if it doesn't exist) # Stop the existing container (ignore errors if it doesn't exist)
echo "Stopping existing container: $CONTAINER_NAME..." echo "Stopping existing container: $CONTAINER_NAME..."
@@ -26,7 +77,6 @@ docker rm $CONTAINER_NAME || true
echo "Building Docker image: $IMAGE_NAME..." echo "Building Docker image: $IMAGE_NAME..."
docker build -t $IMAGE_NAME . docker build -t $IMAGE_NAME .
# Check if build was successful
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Docker build failed!" echo "Docker build failed!"
exit 1 exit 1
@@ -36,7 +86,6 @@ fi
echo "Running new container: $CONTAINER_NAME..." echo "Running new container: $CONTAINER_NAME..."
docker run -d -p $HOST_PORT:$CONTAINER_PORT --name $CONTAINER_NAME $IMAGE_NAME docker run -d -p $HOST_PORT:$CONTAINER_PORT --name $CONTAINER_NAME $IMAGE_NAME
# Check if run was successful
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Docker run failed!" echo "Docker run failed!"
exit 1 exit 1

47
playwright.config.js Normal file
View File

@@ -0,0 +1,47 @@
import { defineConfig, devices } from "@playwright/test";
export default defineConfig({
testDir: "./tests",
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: "html",
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000', // We'll use the preview server URL
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: "on-first-retry",
},
/* Configure projects for major browsers */
projects: [
{
name: "chromium",
use: { ...devices["Desktop Chrome"] },
},
// Add other browsers if needed
// {
// name: 'firefox',
// use: { ...devices['Desktop Firefox'] },
// },
// {
// name: 'webkit',
// use: { ...devices['Desktop Safari'] },
// },
],
/* Run your local dev server before starting the tests */
// webServer: { // We will manage this manually in the deploy script
// command: 'npm run dev',
// url: 'http://127.0.0.1:5173', // Default Vite dev port
// reuseExistingServer: !process.env.CI,
// },
});