From ce3232cd9efb220d6bf596029440d3c57ddcd15b Mon Sep 17 00:00:00 2001 From: SebastienCozeDev Date: Thu, 7 May 2026 18:28:11 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20unit=20tests=20for=20AppContr?= =?UTF-8?q?oller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 6 ++++++ src/app.controller.spec.ts | 43 ++++++++++++++++++++++++++++++++++++-- src/app.service.ts | 2 +- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 1b2ba27..476758a 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,12 @@ generate-migrations: run-migrations: $(DOCKER_COMPOSE_CMD) exec api-dev sh -c "npm run migration:run" +test: + $(DOCKER_COMPOSE_CMD) exec api-dev sh -c "npm run test" + +cov: + $(DOCKER_COMPOSE_CMD) exec api-dev sh -c "npm run test:cov" + build: ## Build the Docker images ifndef PROFILE @$(MAKE) list-profiles diff --git a/src/app.controller.spec.ts b/src/app.controller.spec.ts index d22f389..9453de3 100644 --- a/src/app.controller.spec.ts +++ b/src/app.controller.spec.ts @@ -5,18 +5,57 @@ import { AppService } from './app.service'; describe('AppController', () => { let appController: AppController; + const mockAppService = { + getHello: jest.fn().mockReturnValue('Hello World!'), + getStatus: jest.fn().mockReturnValue({ online: true }), + getOpenSourceLink: jest.fn().mockReturnValue({ + visibility: 'Public', + ownership: '@SebastienCozeDev', + link: 'https://github.com/SebastienCozeDev/task-flow-api', + }), + }; + beforeEach(async () => { const app: TestingModule = await Test.createTestingModule({ controllers: [AppController], - providers: [AppService], + providers: [ + { + provide: AppService, + useValue: mockAppService, + }, + ], }).compile(); appController = app.get(AppController); + jest.clearAllMocks(); + }); + + it('should be defined', () => { + expect(appController).toBeDefined(); }); - describe('root', () => { + describe('getHello', () => { it('should return "Hello World!"', () => { expect(appController.getHello()).toBe('Hello World!'); + expect(mockAppService.getHello).toHaveBeenCalledTimes(1); + }); + }); + + describe('getStatus', () => { + it('should return the application status', () => { + expect(appController.getStatus()).toEqual({ online: true }); + expect(mockAppService.getStatus).toHaveBeenCalledTimes(1); + }); + }); + + describe('getOpenSourceLink', () => { + it('should return the repository information', () => { + expect(appController.getOpenSourceLink()).toEqual({ + visibility: 'Public', + ownership: '@SebastienCozeDev', + link: 'https://github.com/SebastienCozeDev/task-flow-api', + }); + expect(mockAppService.getOpenSourceLink).toHaveBeenCalledTimes(1); }); }); }); diff --git a/src/app.service.ts b/src/app.service.ts index ea51130..6e73a9c 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -14,7 +14,7 @@ export class AppService { getOpenSourceLink(): { visibility: string, ownership: string, link: string } { return { - visibility: "Private", + visibility: "Public", ownership: "@SebastienCozeDev", link: "https://github.com/SebastienCozeDev/task-flow-api", }