Skip to content

Commit

Permalink
feat: Setup e2e tests to be running in parallel by using Docker
Browse files Browse the repository at this point in the history
Additionally:
– add README for e2e tests
– update env variables template
– cleanup npm scripts
  • Loading branch information
letehaha committed Sep 15, 2024
1 parent eeaac30 commit 5e689fd
Show file tree
Hide file tree
Showing 19 changed files with 340 additions and 120 deletions.
7 changes: 7 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ API_LAYER_API_KEY=iVH7l3yBziOKwGSO7jYWYt1RDtb05oKf
APPLICATION_HOST=127.0.0.1
APPLICATION_PORT=8081
APPLICATION_JWT_SECRET=development
# for .env.test use docker/test/docker-compose db service name
APPLICATION_DB_HOST=127.0.0.1
APPLICATION_DB_USERNAME=
APPLICATION_DB_PASSWORD=
APPLICATION_DB_DATABASE=budget-tracker
APPLICATION_DB_PORT=5432
APPLICATION_DB_DIALECT=postgres
# for .env.test use docker/test/docker-compose redis service name
APPLICATION_REDIS_HOST=127.0.0.1

# Tests configurations
# e2e tests are running in parallel, so we need a strict amount of workers,
# so then we can dynamically create the same amount of DBs
JEST_WORKERS_AMOUNT=4
33 changes: 8 additions & 25 deletions .github/workflows/check-source-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: Prepare local deps
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- id: prepare-env
uses: ./.github/actions/prepare-local-env
- name: Install dependencies
Expand All @@ -23,7 +23,7 @@ jobs:
needs: prepare-dependencies
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare-local-env
- name: Lint source code
run: npm run lint
Expand All @@ -33,30 +33,11 @@ jobs:
needs: prepare-dependencies
runs-on: ubuntu-latest
environment: test
services:
postgres:
image: postgres:11.12-stretch
env:
POSTGRES_DB: budget-tracker_test
POSTGRES_USER: ${{ secrets.CI_POSTGRES_USER }}
POSTGRES_PASSWORD: ${{ secrets.CI_POSTGRES_PASSWORD }}
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
redis:
image: redis:latest
ports:
- 6379:6379

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare-local-env
- name: Make envfile
uses: SpicyPizza/create-envfile@v1
uses: SpicyPizza/create-envfile@v2
with:
envkey_APPLICATION_HOST: ${{ secrets.APPLICATION_HOST }}
envkey_APPLICATION_PORT: ${{ secrets.APPLICATION_PORT }}
Expand All @@ -68,6 +49,7 @@ jobs:
envkey_APPLICATION_DB_PORT: ${{ secrets.APPLICATION_DB_PORT }}
envkey_APPLICATION_DB_DIALECT: ${{ secrets.APPLICATION_DB_DIALECT }}
envkey_APPLICATION_REDIS_HOST: ${{ secrets.APPLICATION_REDIS_HOST }}
envkey_JEST_WORKERS_AMOUNT: ${{ secrets.JEST_WORKERS_AMOUNT }}
directory: ./
file_name: .env.test
- name: Unit and e2e testing
Expand All @@ -78,9 +60,9 @@ jobs:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Make envfile
uses: SpicyPizza/create-envfile@v1
uses: SpicyPizza/create-envfile@v2
with:
envkey_APPLICATION_HOST: ${{ secrets.APPLICATION_HOST }}
envkey_APPLICATION_PORT: ${{ secrets.APPLICATION_PORT }}
Expand All @@ -92,6 +74,7 @@ jobs:
envkey_APPLICATION_DB_PORT: ${{ secrets.APPLICATION_DB_PORT }}
envkey_APPLICATION_DB_DIALECT: ${{ secrets.APPLICATION_DB_DIALECT }}
envkey_APPLICATION_REDIS_HOST: ${{ secrets.APPLICATION_REDIS_HOST }}
envkey_JEST_WORKERS_AMOUNT: ${{ secrets.JEST_WORKERS_AMOUNT }}
directory: ./
file_name: .env.production
- uses: ./.github/actions/docker-build
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/image-to-docker-hub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:

steps:
- name: Check Out Repo
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Make envfile
uses: SpicyPizza/create-envfile@v1
uses: SpicyPizza/create-envfile@v2
with:
envkey_APPLICATION_HOST: ${{ secrets.APPLICATION_HOST }}
envkey_APPLICATION_PORT: ${{ secrets.APPLICATION_PORT }}
Expand All @@ -30,6 +30,7 @@ jobs:
envkey_APPLICATION_DB_PORT: ${{ secrets.APPLICATION_DB_PORT }}
envkey_APPLICATION_DB_DIALECT: ${{ secrets.APPLICATION_DB_DIALECT }}
envkey_APPLICATION_REDIS_HOST: ${{ secrets.APPLICATION_REDIS_HOST }}
envkey_JEST_WORKERS_AMOUNT: ${{ secrets.JEST_WORKERS_AMOUNT }}
directory: ./
file_name: .env.production

Expand Down
2 changes: 1 addition & 1 deletion docker/dev/docker-destroy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
echo "Starting removing all dev container completely..."

npm run docker:dev -- -d
npm run docker:dev:down -- --rmi all --volumes --remove-orphans
npm run docker:dev:down -- --rmi all --volumes
19 changes: 19 additions & 0 deletions docker/test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:21.7.3

WORKDIR /app

# Copy package.json and package-lock.json files. This allows Docker to cache the
# npm dependencies as long as these files don't change.
COPY package*.json ./

# Install dependencies
COPY post-install.sh ./
COPY docker ./docker
RUN chmod +x ./post-install.sh
RUN npm ci

# Copy the rest of the application
COPY . .

# Run this command to keep container alive. Without it will be demounted right after deps installation
CMD ["tail", "-f", "/dev/null"]
33 changes: 33 additions & 0 deletions docker/test/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
services:
test-db:
image: postgres:16
restart: always
container_name: test-budget-tracker-db
volumes: ['test_db_data:/var/lib/postgresql/data']
environment:
- POSTGRES_USER=${APPLICATION_DB_USERNAME}
- POSTGRES_PASSWORD=${APPLICATION_DB_PASSWORD}
- POSTGRES_DB=${APPLICATION_DB_DATABASE}
ports: ['${APPLICATION_DB_PORT}:5432']
env_file: ../../.env.test

test-redis:
image: redis:6
container_name: test-budget-tracker-redis
volumes: ['test_redis_data:/data']
ports: ['6379:6379']

test-runner:
build:
context: ../..
dockerfile: docker/test/Dockerfile
depends_on:
- test-db
- test-redis
environment:
- NODE_ENV=test
env_file: ../../.env.test

volumes:
test_db_data:
test_redis_data:
1 change: 1 addition & 0 deletions jest.config.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ console.log('❗ RUNNING INTEGRATION TESTS');
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
export default {
...baseConfig,
maxWorkers: Number(process.env.JEST_WORKERS_AMOUNT),
testMatch: ['<rootDir>/src/**/?(*.)+(e2e).[jt]s?(x)'],
setupFilesAfterEnv: ['<rootDir>/src/tests/setupIntegrationTests.ts'],
};
134 changes: 87 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
"migrate:dev:undo": "cross-env NODE_ENV=development npx sequelize-cli db:migrate:undo",
"migrate": "cross-env NODE_ENV=production npx sequelize-cli db:migrate",
"migrate:undo": "cross-env NODE_ENV=production npx sequelize-cli db:migrate:undo",
"db:reset": "cross-env NODE_ENV=test npx sequelize-cli db:drop && npx sequelize-cli db:create && npx sequelize-cli db:migrate",
"pretest": "cross-env NODE_ENV=test npm run db:reset",
"test": "cross-env NODE_ENV=test npm run test:unit && npm run test:e2e",
"test:unit": "cross-env NODE_ENV=test jest -c jest.config.unit.ts --passWithNoTests --forceExit --detectOpenHandles",
"test:e2e": "cross-env NODE_ENV=test jest -c jest.config.e2e.ts --runInBand --passWithNoTests --forceExit --detectOpenHandles",
"test:e2e": "chmod +x ./src/tests/setup-e2e-tests.sh && ./src/tests/setup-e2e-tests.sh",
"lint": "eslint .",
"docker:dev": "docker compose --env-file .env.development -f ./docker/dev/docker-compose.yml up --build",
"docker:dev:ps": "docker compose --env-file .env.development -f ./docker/dev/docker-compose.yml ps",
Expand Down Expand Up @@ -75,7 +73,7 @@
"passport-jwt": "4.0.1",
"pg": "8.11.5",
"pg-hstore": "2.3.4",
"redis": "3.1.1",
"redis": "4.7.0",
"sequelize": "6.37.3",
"sequelize-cli": "6.6.2",
"sequelize-typescript": "2.1.6",
Expand Down
Loading

0 comments on commit 5e689fd

Please sign in to comment.