From 39ec1a8bbf1c1cff2d184bbb703e3cf5b0b75398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joonas=20H=C3=A4kkinen?= Date: Fri, 28 Jul 2023 16:05:46 +0300 Subject: [PATCH] Run tests for Nest.js project in CI --- .github/workflows/build_image.yaml | 22 ++++++++- .github/workflows/run_tests.yaml | 77 ++++++++++++++++++----------- app-nest/package.json | 2 +- app-nest/test/app.int-spec.ts | 2 +- app-nest/test/jest-integration.json | 3 +- compose-ci.yaml | 5 +- compose-dev.yaml | 6 --- compose.yaml | 6 +++ 8 files changed, 84 insertions(+), 39 deletions(-) diff --git a/.github/workflows/build_image.yaml b/.github/workflows/build_image.yaml index 3324cf0..8dc4aa3 100644 --- a/.github/workflows/build_image.yaml +++ b/.github/workflows/build_image.yaml @@ -1,6 +1,6 @@ name: Build and upload image -on: +on: workflow_call: jobs: @@ -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 diff --git a/.github/workflows/run_tests.yaml b/.github/workflows/run_tests.yaml index 0ad3497..c1d0719 100644 --- a/.github/workflows/run_tests.yaml +++ b/.github/workflows/run_tests.yaml @@ -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 diff --git a/app-nest/package.json b/app-nest/package.json index 56b3f8b..2cf747a 100644 --- a/app-nest/package.json +++ b/app-nest/package.json @@ -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", diff --git a/app-nest/test/app.int-spec.ts b/app-nest/test/app.int-spec.ts index 3485fbe..68c5e6c 100644 --- a/app-nest/test/app.int-spec.ts +++ b/app-nest/test/app.int-spec.ts @@ -11,7 +11,7 @@ describe("App", () => { await app.init(); }); - afterAll(async () => { + afterEach(async () => { await app.close(); }); diff --git a/app-nest/test/jest-integration.json b/app-nest/test/jest-integration.json index e5c39be..6ede852 100644 --- a/app-nest/test/jest-integration.json +++ b/app-nest/test/jest-integration.json @@ -9,5 +9,6 @@ "testRegex": ".int-spec.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" - } + }, + "workerIdleMemoryLimit": "512MB" } \ No newline at end of file diff --git a/compose-ci.yaml b/compose-ci.yaml index 26101e9..6dabfb2 100644 --- a/compose-ci.yaml +++ b/compose-ci.yaml @@ -1,3 +1,6 @@ services: lusmu: - image: hybridilusmu-ci + image: hybridilusmu + + lusmu-nest: + image: hybridilusmu-nest diff --git a/compose-dev.yaml b/compose-dev.yaml index c788e24..69ebe29 100644 --- a/compose-dev.yaml +++ b/compose-dev.yaml @@ -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 diff --git a/compose.yaml b/compose.yaml index a87fa43..695854b 100644 --- a/compose.yaml +++ b/compose.yaml @@ -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