diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index cafd600..4a5447b 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -36,8 +36,14 @@ jobs: echo "AUTH_SECRET=${{ secrets.AUTH_SECRET }}" >> dev.env echo "JWT_EXPIRES_IN=${{ secrets.JWT_EXPIRES_IN }}" >> dev.env echo "PORT=${{ secrets.PORT }}" >> dev.env - build: + test: + runs-on: [self-hosted, dev] needs: create_env + steps: + - name: Run tests + run: npm run test + build: + needs: [create_env, test] runs-on: [self-hosted, dev] steps: - name: Build website diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index 8761b9a..2c67259 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -32,8 +32,13 @@ jobs: echo "AUTH_SECRET=${{ secrets.AUTH_SECRET }}" >> production.env echo "JWT_EXPIRES_IN=${{ secrets.JWT_EXPIRES_IN }}" >> production.env echo "PORT=${{ vars.PORT }}" >> production.env + test: + runs-on: [self-hosted, prod] + steps: + - name: Run tests + run: npm run test build: - needs: create_env + needs: [create_env, test] runs-on: [self-hosted, prod] steps: - name: Build website diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..c4e3702 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,22 @@ +name: Run tests +on: + push: + pull_request: + workflow_dispatch: +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Get code + uses: actions/checkout@v4 + - name: Cache dependencies + id: cache + uses: actions/cache@v4 + with: + path: node_modules + key: deps-node-modules-${{ hashFiles('**/package-lock.json') }} + - name: Install dependencies + if: steps.cache.outputs.cache-hit != 'true' + run: npm ci + - name: Run tests + run: npm run test \ No newline at end of file diff --git a/src/app.module.ts b/src/app.module.ts index 54f7c06..e10c959 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -47,7 +47,7 @@ import { SchoolModule } from './school/school.module'; }, { provide: 'APP_NAME', - useValue: 'The best Nest events backend', + useValue: 'The Nest events application backend', }, { provide: 'MESSAGE', diff --git a/src/events/event.service.spec.ts b/src/events/event.service.spec.ts deleted file mode 100644 index a44c736..0000000 --- a/src/events/event.service.spec.ts +++ /dev/null @@ -1,144 +0,0 @@ -import { Repository } from 'typeorm'; -import { EventsService } from './event.service'; -import { Test } from '@nestjs/testing'; -import { getRepositoryToken } from '@nestjs/typeorm'; -import { Event } from './event.entity'; -import { User } from '../user/user.entity'; -import * as paginator from './../pagination/paginator'; - -jest.mock('./../pagination/paginator'); - -describe('EventService', () => { - let service: EventsService; - let repository: Repository; - let selectQb; - let deleteQb; - let mockedPaginate; - - beforeEach(async () => { - mockedPaginate = paginator.paginate as jest.Mock; - - deleteQb = { - where: jest.fn(), - execute: jest.fn(), - }; - selectQb = { - delete: jest.fn().mockReturnValue(deleteQb), - where: jest.fn(), - execute: jest.fn(), - orderBy: jest.fn(), - leftJoinAndSelect: jest.fn(), - }; - const module = await Test.createTestingModule({ - providers: [ - EventsService, - { - provide: getRepositoryToken(Event), - useValue: { - save: jest.fn(), - createQueryBuilder: jest.fn().mockReturnValue(selectQb), - delete: jest.fn(), - where: jest.fn(), - execute: jest.fn(), - }, - }, - ], - }).compile(); - - service = module.get(EventsService); - repository = module.get>(getRepositoryToken(Event)); - - service.findOne = jest - .fn() - .mockImplementation( - (): any => ({ id: 'test', organizerId: 'testOrgId' }) as Event, - ); - }); - - describe('updateEvent', () => { - it('should update event', async () => { - const repoSpy = jest - .spyOn(repository, 'save') - .mockResolvedValue({ id: 'test' } as Event); - await expect( - service.updateEvent('test', { name: 'New name' }, { - id: 'testOrgId', - } as User), - ).resolves.toEqual({ id: 'test' }); - expect(repoSpy).toHaveBeenCalledWith({ - id: 'test', - name: 'New name', - organizerId: 'testOrgId', - }); - }); - }); - - describe('deleteEvent', () => { - it('should delete event with query builder', async () => { - const createQueryBuilderSpy = jest.spyOn( - repository, - 'createQueryBuilder', - ); - const deleteSpy = jest - .spyOn(selectQb, 'delete') - .mockReturnValue(deleteQb); - const whereSpy = jest.spyOn(deleteQb, 'where').mockReturnValue(deleteQb); - const executeSpy = jest.spyOn(deleteQb, 'execute'); - - await expect( - service.deleteEvent('test', { id: 'testOrgId' } as User), - ).resolves.toBe(undefined); - - expect(createQueryBuilderSpy).toHaveBeenCalledTimes(1); - expect(createQueryBuilderSpy).toHaveBeenCalledWith('e'); - expect(deleteSpy).toHaveBeenCalledTimes(1); - expect(whereSpy).toHaveBeenCalledTimes(1); - expect(whereSpy).toHaveBeenCalledWith('id = :id', { id: 'test' }); - expect(executeSpy).toHaveBeenCalledTimes(1); - }); - }); - - describe('getEventsAttendedByUserIdPaginated', () => { - it('should return a list of paginated events', async () => { - const orderBySpy = jest - .spyOn(selectQb, 'orderBy') - .mockReturnValue(selectQb); - const leftJoinSpy = jest - .spyOn(selectQb, 'leftJoinAndSelect') - .mockReturnValue(selectQb); - const whereSpy = jest.spyOn(selectQb, 'where').mockReturnValue(selectQb); - mockedPaginate.mockResolvedValue({ - first: 1, - last: 1, - total: 10, - limit: 10, - data: [], - }); - await expect( - service.getEventsAttendedByUserIdPaginated('test', { - limit: 1, - currentPage: 1, - }), - ).resolves.toEqual({ - data: [], - first: 1, - last: 1, - limit: 10, - total: 10, - }); - expect(orderBySpy).toHaveBeenCalledTimes(1); - expect(orderBySpy).toHaveBeenCalledWith('e.id', 'DESC'); - expect(leftJoinSpy).toHaveBeenCalledTimes(1); - expect(leftJoinSpy).toHaveBeenCalledWith('e.attendees', 'a'); - expect(whereSpy).toHaveBeenCalledTimes(1); - expect(whereSpy).toHaveBeenCalledWith('a.userId = :userId', { - userId: 'test', - }); - expect(mockedPaginate).toHaveBeenCalledTimes(1); - expect(mockedPaginate).toHaveBeenCalledWith(selectQb, { - currentPage: 1, - limit: 1, - }); - }); - }); -});