Skip to content

Commit

Permalink
Run tests for Nest.js project in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
joonashak committed Jul 31, 2023
1 parent 5234506 commit 259a8b0
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 40 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/build_image.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Build and upload image

on:
on:
workflow_call:

jobs:
Expand All @@ -23,3 +23,23 @@ jobs:
with:
name: hybridilusmu
path: /tmp/hybridilusmu.tar

build-nest:
name: Build and upload image (Nest.js)
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build and export
uses: docker/build-push-action@v4
with:
context: app-nest
tags: hybridilusmu-nest
outputs: type=docker,dest=/tmp/hybridilusmu-nest.tar
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: hybridilusmu-nest
path: /tmp/hybridilusmu-nest.tar
77 changes: 49 additions & 28 deletions .github/workflows/run_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,56 @@ jobs:
run: |
docker load --input /tmp/hybridilusmu.tar
docker image ls -a
- name: Start containers
run: docker compose -f compose.yaml -f compose-ci.yaml up -d lusmu
- name: Run unit tests
run: npm test
run: docker compose -f compose.yaml -f compose-ci.yaml run lusmu npm test
- name: Stop containers
if: always()
run: npm stop

unit-tests-nest:
name: Unit tests (Nest.js)
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: hybridilusmu-nest
path: /tmp
- name: Load image
run: |
docker load --input /tmp/hybridilusmu-nest.tar
docker image ls -a
- name: Run unit tests
run: docker compose -f compose.yaml -f compose-ci.yaml run lusmu-nest npm test
- name: Stop containers
if: always()
run: npm stop

integration-tests-nest:
name: Integration tests (Nest.js)
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: hybridilusmu-nest
path: /tmp
- name: Load image
run: |
docker load --input /tmp/hybridilusmu-nest.tar
docker image ls -a
- name: Start containers
run: docker compose -f compose.yaml -f compose-ci.yaml up -d lusmu-nest
- name: Run integration tests
run: npm run test:integration
- name: Stop containers
if: always()
run: npm stop
# FIXME: Enable these once integration tests exit cleanly.
# integration-tests:
# name: Integration tests
# runs-on: ubuntu-latest
# needs: build
# steps:
# - name: Checkout
# uses: actions/checkout@v3
# - name: Set up Docker Buildx
# uses: docker/setup-buildx-action@v2
# - name: Download artifact
# uses: actions/download-artifact@v3
# with:
# name: hybridilusmu
# path: /tmp
# - name: Load image
# run: |
# docker load --input /tmp/hybridilusmu.tar
# docker image ls -a
# - name: Start containers
# run: docker compose -f compose.yaml -f compose-ci.yaml up -d lusmu
# - name: Run integration tests
# run: npm run test:integration
# - name: Stop containers
# run: npm stop
2 changes: 1 addition & 1 deletion app-nest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:integration": "jest --config ./test/jest-integration.json"
"test:integration": "jest --config ./test/jest-integration.json -w 1"
},
"dependencies": {
"@golevelup/nestjs-discovery": "^4.0.0",
Expand Down
4 changes: 2 additions & 2 deletions app-nest/test/app.int-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ describe("App", () => {
beforeEach(async () => {
const moduleFixture = await createTestingModule().compile();
app = moduleFixture.createNestApplication();
await app.init();
//await app.init();
});

afterAll(async () => {
afterEach(async () => {
await app.close();
});

Expand Down
3 changes: 2 additions & 1 deletion app-nest/test/jest-integration.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"testRegex": ".int-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
},
"workerIdleMemoryLimit": "512MB"
}
5 changes: 4 additions & 1 deletion compose-ci.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
services:
lusmu:
image: hybridilusmu-ci
image: hybridilusmu

lusmu-nest:
image: hybridilusmu-nest
6 changes: 0 additions & 6 deletions compose-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ services:
command: npm run watch

lusmu-nest:
environment:
NODE_ENV: development
DATABASE_HOST: db
DATABASE_NAME: nest
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgres
env_file:
- .env.development
command: npm run start:dev
6 changes: 6 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ services:
lusmu-nest:
container_name: lusmu-nest
build: app-nest/
environment:
NODE_ENV: development
DATABASE_HOST: db
DATABASE_NAME: nest
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgres
volumes:
- ./app-nest/src:/usr/src/app/src
- ./app-nest/test:/usr/src/app/test
Expand Down

0 comments on commit 259a8b0

Please sign in to comment.