diff --git a/.env.test-ci b/.env.test-ci index 55a1c65..7232abd 100644 --- a/.env.test-ci +++ b/.env.test-ci @@ -8,10 +8,10 @@ NODE_ENV=local SITE_ORIGIN=http://localhost:3000 # Database connection parameters -DB_HOST=db -DB_PORT=5432 -DB_DATABASE=ionicapp_test -DB_USERNAME=user_test +DB_HOST=localhost +DB_PORT=54321 +DB_DATABASE=ionicapp_test +DB_USERNAME=username_test DB_PASSWORD=secret_test # TypeOrm configuration (true or false) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 06db97a..734e939 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,6 +10,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 + with: + path: . - uses: actions/setup-node@v4 with: node-version: 22.14 @@ -21,7 +23,7 @@ jobs: **/node_modules **/.env.test **/.env.example - key: ${{ runner.os }}-node-npm-${{ hashFiles('**/package-lock.json', '**/.env.test', '**/.env.example') }} + key: ${{ runner.os }}-node-npm-${{ hashFiles('./package-lock.json', './.env.test', './.env.example') }} restore-keys: | ${{ runner.os }}-node-npm- - name: Install deps @@ -48,21 +50,17 @@ jobs: **/node_modules **/.env.test **/.env.example - key: ${{ runner.os }}-node-npm-${{ hashFiles('**/package-lock.json', '**/.env.test', '**/.env.example') }} + key: ${{ runner.os }}-node-npm-${{ hashFiles('./package-lock.json', './.env.test', './.env.example') }} - name: Run unit tests run: npm run test:unit - test-api-int: + test-api-int-e2e: runs-on: ubuntu-latest needs: build steps: - uses: actions/checkout@v4 - - uses: isbang/compose-action@v1.5.1 - with: - compose-file: "./docker/docker-compose.test-ci.yml" - services: | - postgres_test - redis_test + - run: cp .env.test-ci .env -f + - run: cat .env - uses: actions/cache@v4 id: npm-cache with: @@ -71,7 +69,12 @@ jobs: **/node_modules **/.env.test **/.env.example - key: ${{ runner.os }}-node-npm-${{ hashFiles('**/package-lock.json', '**/.env.test', '**/.env.example') }} + key: ${{ runner.os }}-node-npm-${{ hashFiles('./package-lock.json', './.env.test', './.env.example') }} + - uses: isbang/compose-action@v1.5.1 + with: + compose-file: "docker-compose.yml" + services: | + postgres_tests - run: npm ci - name: Get build result artifact uses: actions/download-artifact@v4 @@ -81,5 +84,7 @@ jobs: - run: cp .env.test-ci .env.test -f - name: Run migrations run: npm run migration:test:run - - name: Run ingegration tests + - name: Run ingegration and e2e tests run: npm run test:int + - name: Run e2e tests + run: npm run test:e2e diff --git a/package-lock.json b/package-lock.json index 9bb0a7a..8ab441b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ionicapp", - "version": "0.0.2", + "version": "0.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ionicapp", - "version": "0.0.2", + "version": "0.0.3", "license": "UNLICENSED", "dependencies": { "@nestjs/common": "^11.0.1", diff --git a/package.json b/package.json index 230adfe..44db976 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ionicapp", - "version": "0.0.2", + "version": "0.0.3", "description": "", "author": "", "private": true, @@ -22,11 +22,12 @@ "migration:revert": "npm run typeorm migration:revert", "migration:generate": "npm run typeorm migration:generate src/db/migrations/$npm_config_name", "migration:create": "npx typeorm-ts-node-commonjs migration:create src/db/migrations/$npm_config_name", - "test": "jest", + "test:unit": "jest --selectProjects api-unit", + "test:e2e": "jest --selectProjects api-e2e --detectOpenHandles", + "test:int": "jest --selectProjects api-integration --detectOpenHandles", "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:e2e": "jest --config ./test/jest-e2e.json" + "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand" }, "dependencies": { "@nestjs/common": "^11.0.1", @@ -85,15 +86,40 @@ "json", "ts" ], - "rootDir": "src", - "testRegex": ".*\\.spec\\.ts$", - "transform": { - "^.+\\.(t|j)s$": "ts-jest" - }, "collectCoverageFrom": [ "**/*.(t|j)s" ], "coverageDirectory": "../coverage", - "testEnvironment": "node" + "testEnvironment": "node", + "testTimeout": 50000, + "projects": [ + { + "displayName": "api-e2e", + "testRegex": ".*\\.e2e-spec\\.ts$", + "transform": { + "^.+\\.ts": "ts-jest" + }, + "maxWorkers": 1, + "rootDir": "test" + }, + { + "displayName": "api-unit", + "testRegex": ".*\\.spec\\.ts$", + "transform": { + "^.+\\.ts": "ts-jest" + }, + "maxWorkers": 1, + "rootDir": "src" + }, + { + "displayName": "api-integration", + "testRegex": ".*\\.int-spec\\.ts$", + "transform": { + "^.+\\.ts": "ts-jest" + }, + "maxWorkers": 1, + "rootDir": "test" + } + ] } -} +} \ No newline at end of file diff --git a/test/app.int-spec.ts b/test/app.int-spec.ts new file mode 100644 index 0000000..a763ad6 --- /dev/null +++ b/test/app.int-spec.ts @@ -0,0 +1,31 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { INestApplication } from '@nestjs/common'; +import * as request from 'supertest'; +import { App } from 'supertest/types'; +import { AppModule } from './../src/app.module'; +import { AppService } from '../src/app.service'; + +describe('AppController (integration)', () => { + let app: INestApplication; + let appService: AppService; + + beforeAll(async () => { + const moduleFixture: TestingModule = await Test.createTestingModule({ + imports: [AppModule], + }).compile(); + + app = moduleFixture.createNestApplication(); + await app.init(); + appService = moduleFixture.get(AppService); + }); + afterAll(async () => { + await app.close(); + }); + + it('shoud call appService.checkHealth', async () => { + const spyOnCheckHealth = jest.spyOn(appService, 'checkHealth'); + spyOnCheckHealth.mockImplementation(() => 'OK'); + await request(app.getHttpServer()).get('/health'); + expect(spyOnCheckHealth).toHaveBeenCalledTimes(1); + }); +}); diff --git a/test/jest-e2e.json b/test/jest-e2e.json deleted file mode 100644 index 2a29072..0000000 --- a/test/jest-e2e.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "moduleFileExtensions": ["js", "json", "ts"], - "rootDir": ".", - "testEnvironment": "node", - "testRegex": ".e2e-spec.ts$", - "transform": { - "^.+\\.js$": "ts-jest" - } -}