Skip to content

Commit

Permalink
Set test environment for e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
khshourov committed Nov 1, 2024
1 parent df2baed commit 2377c1d
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ lerna-debug.log*
.env.dev
.env.test
.env.prod
!test/resources/.env.test
# temp directory
.temp
.tmp
Expand Down
76 changes: 76 additions & 0 deletions scripts/run-e2e
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash

# Define constants
MARKER="\033[32m#\033[0m"

# Define paths
ROOT_ENV=".env.test"
TEST_ENV="test/resources/.env.test"
BACKUP_ENV=".env.test.backup"
POSTGRES_COMPOSE_FILE="test/resources/postgresql.yml"

get_env_var() {
local var_name="$1"
grep -E "^${var_name}=" "$ROOT_ENV" | cut -d '=' -f 2
}

title() {
echo -e "$MARKER $1" && sleep 1
}

linebreak() {
echo
}

title "Check if the test .env file exists in the source directory"
if [ ! -f "$TEST_ENV" ]; then
echo "Error: $TEST_ENV does not exist. Aborting."
exit 1
fi
linebreak

title "Back up the existing .env.test file if it exists"
if [ -f "$ROOT_ENV" ]; then
echo "Backing up existing $ROOT_ENV to $BACKUP_ENV..."
cp "$ROOT_ENV" "$BACKUP_ENV" || { echo "Failed to create backup. Aborting."; exit 1; }
fi
linebreak

title "Copy the new .env.test file to the root directory"
echo "Replacing $ROOT_ENV with $TEST_ENV..."
cp "$TEST_ENV" "$ROOT_ENV" || { echo "Failed to copy $TEST_ENV to $ROOT_ENV. Aborting."; exit 1; }
linebreak

title "Starting PostgreSQL container..."
docker-compose --env-file "$ROOT_ENV" -f "$POSTGRES_COMPOSE_FILE" up -d || { echo "Failed to start Docker container. Aborting."; exit 1; }
linebreak

title "Wait for PostgreSQL to be ready"
POSTGRES_USER=$(get_env_var "DB_USER")
until docker exec -it $(docker ps -q -f "name=postgres-e2e") pg_isready -U "$POSTGRES_USER"; do
echo "Waiting for PostgreSQL to be ready..."
sleep 2
done
linebreak

title "Run migration and the NestJS e2e tests"
export NODE_ENV="test"
yarn run migration:run && yarn run test:e2e
linebreak

title "Stopping PostgreSQL container..."
docker-compose --env-file "$ROOT_ENV" -f "$POSTGRES_COMPOSE_FILE" down --volumes
linebreak

title "Restore the original .env.test file if it existed"
if [ -f "$BACKUP_ENV" ]; then
echo "Restoring the original $ROOT_ENV from backup..."
mv "$BACKUP_ENV" "$ROOT_ENV" || { echo "Failed to restore original .env.test. Please check manually."; exit 1; }
else
# If no original .env.test existed, remove the temporary one after testing
echo "No original .env.test backup found. Cleaning up temporary file."
rm "$ROOT_ENV"
fi
linebreak

title "Test run completed successfully."
1 change: 1 addition & 0 deletions src/config/typeorm.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
process.env.DOTENV_CONFIG_PATH = `${process.cwd()}/.env.${process.env.NODE_ENV ? `${process.env.NODE_ENV}` : 'dev'}`;
import 'dotenv/config';
import { DataSource } from 'typeorm';
import { DictionaryWord } from '../dictionary-records/entities/dictionary-record.entity';
Expand Down
19 changes: 19 additions & 0 deletions test/resources/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Rate Limit
# General purpose
THROTTLE_TTL=60000
THROTTLE_LIMIT=120

# PostgreSQL connection properties
DB_HOST=localhost
DB_PORT=6432
DB_USER=dictionary_api_e2e
DB_PASSWORD=dictionary_api_e2e
DB_DATABASE=dictionary_api_e2e

# Google OAuth 2.0
GOOGLE_CLIENT_ID=google-client-id
GOOGLE_CLIENT_SECRET=google-client-secret
GOOGLE_REDIRECT_URL=http://localhost:3000/auth/google/redirect

# JWT Signing Secret
JWT_SECRET=secret
11 changes: 11 additions & 0 deletions test/resources/postgresql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
postgres:
image: postgres:latest
container_name: postgres-e2e
restart: always
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_DATABASE}
ports:
- "${DB_PORT}:5432"

0 comments on commit 2377c1d

Please sign in to comment.