From d814540027e4b0b8346709ad5d120f4e85218ac8 Mon Sep 17 00:00:00 2001 From: Matheus Dias Date: Wed, 15 May 2024 19:00:03 -0300 Subject: [PATCH 01/10] test: unit tests for user service class --- src/users/users.service.spec.ts | 61 ++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/src/users/users.service.spec.ts b/src/users/users.service.spec.ts index 62815ba6..fce9d13b 100644 --- a/src/users/users.service.spec.ts +++ b/src/users/users.service.spec.ts @@ -1,18 +1,69 @@ import { Test, TestingModule } from '@nestjs/testing'; import { UsersService } from './users.service'; +import { PrismaService } from '../../src/prisma/prisma.service'; +import { UpdateUserSchema } from './types'; describe('UsersService', () => { - let service: UsersService; + let userService: UsersService; + + const mockPrismaService = { + user: { + create: jest.fn(), + update: jest.fn() + } + } beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [UsersService], - }).compile(); + providers: [ + UsersService, + PrismaService, + ], + }) + .overrideProvider(PrismaService) + .useValue(mockPrismaService) + .compile(); - service = module.get(UsersService); + userService = module.get(UsersService); }); it('should be defined', () => { - expect(service).toBeDefined(); + expect(userService).toBeDefined(); }); + + describe('store function', () => { + const userPayload = { + name: 'matheus', lastName: 'silva', phone:'44999998311' + } + + it(`deve chamar a funcao store 1 vez`, async () => { + userService.store(userPayload) + expect(mockPrismaService.user.create).toHaveBeenCalledTimes(1) + }); + + it(`deve chamar a funcao store com os parametros`, async () => { + userService.store(userPayload) + const data = { + ...userPayload, + password: userPayload.phone, + login: userPayload.phone, + createdAt: new Date().toISOString(), + } + expect(mockPrismaService.user.create).toHaveBeenCalledWith({ data }) + }); + }); + + describe('update function', () => { + const id = 'user_id_test'; + const body = { + name: 'Matheus', + lastName: 'Silva', + phone:'44999998311', + }; + + it(`deve chamar a funcao store 1 vez`, async () => { + userService.update(id, body) + expect(mockPrismaService.user.update).toHaveBeenCalledTimes(1) + }); + }) }); From 29b994140d5808b4d11fda6de8bf68a1258472ce Mon Sep 17 00:00:00 2001 From: Matheus Dias Date: Fri, 17 May 2024 15:27:32 -0300 Subject: [PATCH 02/10] test: controller store function tests --- src/users/users.controller.spec.ts | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/users/users.controller.spec.ts b/src/users/users.controller.spec.ts index 3e27c395..c97a63cd 100644 --- a/src/users/users.controller.spec.ts +++ b/src/users/users.controller.spec.ts @@ -1,18 +1,62 @@ import { Test, TestingModule } from '@nestjs/testing'; import { UsersController } from './users.controller'; +import { UsersService } from './users.service'; describe('UsersController', () => { let controller: UsersController; + let userService: UsersService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [UsersController], + providers: [ + { + provide: UsersService, + useValue: { + store: jest.fn(), + create: jest.fn() + } + } + ] }).compile(); controller = module.get(UsersController); + userService = module.get(UsersService) }); it('should be defined', () => { expect(controller).toBeDefined(); + expect(userService).toBeDefined(); }); + + describe('store function', () => { + const body = { + name: "Matheus", + lastName: "Silva", + phone: "44999998311", + } + + it('checks if the function has been called 1 time', async () => { + await controller.store(body) + expect(userService.store).toHaveBeenCalledTimes(1) + }); + + it('checks the return of the function', async () => { + const expectedResult = { + message: "Successfully created user", + respData: undefined, + statusCode: 201, + } + const result = await controller.store(body) + expect(expectedResult.statusCode).toEqual(201) + expect(expectedResult.message).toEqual("Successfully created user") + expect(expectedResult).toEqual(result) + + }); + + it('checks if the function fails', async () => { + jest.spyOn(userService, 'store').mockRejectedValueOnce(new Error()); + expect(controller.store(body)).rejects.toThrow() + }) + }) }); From 824d24b905827f2ec236a24f876e803cfcd4e60e Mon Sep 17 00:00:00 2001 From: Matheus Dias Date: Fri, 17 May 2024 15:27:56 -0300 Subject: [PATCH 03/10] refactor: removing module alias --- src/users/users.controller.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 5d4a0dc2..c0c11280 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -11,10 +11,10 @@ import { } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; -import { UserGuard } from '@/guards/user.guard'; +import { UserGuard } from '../guards/user.guard'; import { ServerResponse } from '../utils'; import { UsersService } from './users.service'; -import { AdminGuard } from '@/guards/admin.guard'; +import { AdminGuard } from '../guards/admin.guard'; @ApiTags('Usuários') @Controller('users') From 4e2ff75978fc860cc6b41a6941c78d684c8dda6c Mon Sep 17 00:00:00 2001 From: Matheus Dias Date: Fri, 17 May 2024 15:29:58 -0300 Subject: [PATCH 04/10] refactor: test text description --- src/users/users.service.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/users/users.service.spec.ts b/src/users/users.service.spec.ts index fce9d13b..32484a33 100644 --- a/src/users/users.service.spec.ts +++ b/src/users/users.service.spec.ts @@ -36,12 +36,12 @@ describe('UsersService', () => { name: 'matheus', lastName: 'silva', phone:'44999998311' } - it(`deve chamar a funcao store 1 vez`, async () => { + it('shold call the store function 1 time', async () => { userService.store(userPayload) expect(mockPrismaService.user.create).toHaveBeenCalledTimes(1) }); - it(`deve chamar a funcao store com os parametros`, async () => { + it('shold call the store function with the parameters', async () => { userService.store(userPayload) const data = { ...userPayload, @@ -61,7 +61,7 @@ describe('UsersService', () => { phone:'44999998311', }; - it(`deve chamar a funcao store 1 vez`, async () => { + it('shuld call the store function 1 time', async () => { userService.update(id, body) expect(mockPrismaService.user.update).toHaveBeenCalledTimes(1) }); From 4a52349dd2330a2609bf1a32b15bb52ca281e76c Mon Sep 17 00:00:00 2001 From: Matheus Dias Date: Fri, 17 May 2024 16:05:38 -0300 Subject: [PATCH 05/10] test: update function and self update --- src/users/users.controller.spec.ts | 68 ++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/src/users/users.controller.spec.ts b/src/users/users.controller.spec.ts index c97a63cd..6a770b77 100644 --- a/src/users/users.controller.spec.ts +++ b/src/users/users.controller.spec.ts @@ -14,7 +14,7 @@ describe('UsersController', () => { provide: UsersService, useValue: { store: jest.fn(), - create: jest.fn() + update: jest.fn() } } ] @@ -48,8 +48,6 @@ describe('UsersController', () => { statusCode: 201, } const result = await controller.store(body) - expect(expectedResult.statusCode).toEqual(201) - expect(expectedResult.message).toEqual("Successfully created user") expect(expectedResult).toEqual(result) }); @@ -58,5 +56,67 @@ describe('UsersController', () => { jest.spyOn(userService, 'store').mockRejectedValueOnce(new Error()); expect(controller.store(body)).rejects.toThrow() }) - }) + }); + + describe('update function', () => { + const body = { + name: "Matheus", + lastName: "Silva", + phone: "44999998311", + } + const id = 'test_user_id' + + it('checks if the function has been called 1 time', async () => { + await controller.update(body, id); + expect(userService.update).toHaveBeenCalledTimes(1); + }); + + it('checks the return of the function', async () => { + const expectedResult = { + message: "Successfully updated user", + respData: undefined, + statusCode: 201, + } + const result = await controller.update(body, id); + expect(expectedResult).toEqual(result) + + }); + + it('checks if the function fails', async () => { + jest.spyOn(userService, 'update').mockRejectedValueOnce(new Error()); + expect(controller.update(body, id)).rejects.toThrow() + }); + }); + + describe('selfUpdate function', () => { + const body = { + name: "Matheus", + lastName: "Silva", + phone: "44999998311", + } + const req = { + user: 'test_user_id' + } + + it('checks if the function has been called 1 time', async () => { + await controller.selfUpdate(body, req); + expect(userService.update).toHaveBeenCalledTimes(1); + }); + + it('checks the return of the function', async () => { + const expectedResult = { + message: "Successfully updated", + respData: undefined, + statusCode: 201, + } + const result = await controller.selfUpdate(body, req); + expect(expectedResult).toEqual(result) + + }); + + it('checks if the function fails', async () => { + jest.spyOn(userService, 'update').mockRejectedValueOnce(new Error()); + expect(controller.selfUpdate(body, req)).rejects.toThrow() + }); + }); }); From f6e5bb104fc3586b0f7fa8560e652c0c8b303e10 Mon Sep 17 00:00:00 2001 From: Matheus Dias Date: Fri, 17 May 2024 16:12:18 -0300 Subject: [PATCH 06/10] lint: run lint code --- src/users/users.controller.spec.ts | 75 ++++++++++++++---------------- src/users/users.service.spec.ts | 52 ++++++++++----------- 2 files changed, 61 insertions(+), 66 deletions(-) diff --git a/src/users/users.controller.spec.ts b/src/users/users.controller.spec.ts index 6a770b77..710aa2cb 100644 --- a/src/users/users.controller.spec.ts +++ b/src/users/users.controller.spec.ts @@ -14,14 +14,14 @@ describe('UsersController', () => { provide: UsersService, useValue: { store: jest.fn(), - update: jest.fn() - } - } - ] + update: jest.fn(), + }, + }, + ], }).compile(); controller = module.get(UsersController); - userService = module.get(UsersService) + userService = module.get(UsersService); }); it('should be defined', () => { @@ -31,40 +31,39 @@ describe('UsersController', () => { describe('store function', () => { const body = { - name: "Matheus", - lastName: "Silva", - phone: "44999998311", - } + name: 'Matheus', + lastName: 'Silva', + phone: '44999998311', + }; it('checks if the function has been called 1 time', async () => { - await controller.store(body) - expect(userService.store).toHaveBeenCalledTimes(1) + await controller.store(body); + expect(userService.store).toHaveBeenCalledTimes(1); }); it('checks the return of the function', async () => { const expectedResult = { - message: "Successfully created user", + message: 'Successfully created user', respData: undefined, statusCode: 201, - } - const result = await controller.store(body) - expect(expectedResult).toEqual(result) - + }; + const result = await controller.store(body); + expect(expectedResult).toEqual(result); }); it('checks if the function fails', async () => { jest.spyOn(userService, 'store').mockRejectedValueOnce(new Error()); - expect(controller.store(body)).rejects.toThrow() - }) + expect(controller.store(body)).rejects.toThrow(); + }); }); describe('update function', () => { const body = { - name: "Matheus", - lastName: "Silva", - phone: "44999998311", - } - const id = 'test_user_id' + name: 'Matheus', + lastName: 'Silva', + phone: '44999998311', + }; + const id = 'test_user_id'; it('checks if the function has been called 1 time', async () => { await controller.update(body, id); @@ -73,30 +72,29 @@ describe('UsersController', () => { it('checks the return of the function', async () => { const expectedResult = { - message: "Successfully updated user", + message: 'Successfully updated user', respData: undefined, statusCode: 201, - } + }; const result = await controller.update(body, id); - expect(expectedResult).toEqual(result) - + expect(expectedResult).toEqual(result); }); it('checks if the function fails', async () => { jest.spyOn(userService, 'update').mockRejectedValueOnce(new Error()); - expect(controller.update(body, id)).rejects.toThrow() + expect(controller.update(body, id)).rejects.toThrow(); }); }); describe('selfUpdate function', () => { const body = { - name: "Matheus", - lastName: "Silva", - phone: "44999998311", - } + name: 'Matheus', + lastName: 'Silva', + phone: '44999998311', + }; const req = { - user: 'test_user_id' - } + user: 'test_user_id', + }; it('checks if the function has been called 1 time', async () => { await controller.selfUpdate(body, req); @@ -105,18 +103,17 @@ describe('UsersController', () => { it('checks the return of the function', async () => { const expectedResult = { - message: "Successfully updated", + message: 'Successfully updated', respData: undefined, statusCode: 201, - } + }; const result = await controller.selfUpdate(body, req); - expect(expectedResult).toEqual(result) - + expect(expectedResult).toEqual(result); }); it('checks if the function fails', async () => { jest.spyOn(userService, 'update').mockRejectedValueOnce(new Error()); - expect(controller.selfUpdate(body, req)).rejects.toThrow() + expect(controller.selfUpdate(body, req)).rejects.toThrow(); }); }); }); diff --git a/src/users/users.service.spec.ts b/src/users/users.service.spec.ts index 32484a33..3ba0228a 100644 --- a/src/users/users.service.spec.ts +++ b/src/users/users.service.spec.ts @@ -1,7 +1,6 @@ import { Test, TestingModule } from '@nestjs/testing'; import { UsersService } from './users.service'; import { PrismaService } from '../../src/prisma/prisma.service'; -import { UpdateUserSchema } from './types'; describe('UsersService', () => { let userService: UsersService; @@ -9,20 +8,17 @@ describe('UsersService', () => { const mockPrismaService = { user: { create: jest.fn(), - update: jest.fn() - } - } + update: jest.fn(), + }, + }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [ - UsersService, - PrismaService, - ], + providers: [UsersService, PrismaService], }) - .overrideProvider(PrismaService) - .useValue(mockPrismaService) - .compile(); + .overrideProvider(PrismaService) + .useValue(mockPrismaService) + .compile(); userService = module.get(UsersService); }); @@ -33,37 +29,39 @@ describe('UsersService', () => { describe('store function', () => { const userPayload = { - name: 'matheus', lastName: 'silva', phone:'44999998311' - } + name: 'matheus', + lastName: 'silva', + phone: '44999998311', + }; it('shold call the store function 1 time', async () => { - userService.store(userPayload) - expect(mockPrismaService.user.create).toHaveBeenCalledTimes(1) + userService.store(userPayload); + expect(mockPrismaService.user.create).toHaveBeenCalledTimes(1); }); it('shold call the store function with the parameters', async () => { - userService.store(userPayload) + userService.store(userPayload); const data = { ...userPayload, password: userPayload.phone, login: userPayload.phone, createdAt: new Date().toISOString(), - } - expect(mockPrismaService.user.create).toHaveBeenCalledWith({ data }) + }; + expect(mockPrismaService.user.create).toHaveBeenCalledWith({ data }); }); }); describe('update function', () => { - const id = 'user_id_test'; - const body = { - name: 'Matheus', - lastName: 'Silva', - phone:'44999998311', - }; + const id = 'user_id_test'; + const body = { + name: 'Matheus', + lastName: 'Silva', + phone: '44999998311', + }; it('shuld call the store function 1 time', async () => { - userService.update(id, body) - expect(mockPrismaService.user.update).toHaveBeenCalledTimes(1) + userService.update(id, body); + expect(mockPrismaService.user.update).toHaveBeenCalledTimes(1); }); - }) + }); }); From ea86e0b4e903531c101a5aa4fcd8bc8a90645f2d Mon Sep 17 00:00:00 2001 From: Matheus Dias Date: Fri, 17 May 2024 16:36:36 -0300 Subject: [PATCH 07/10] feat: jest module alias configuration --- package.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 3212f2fd..11d1abfe 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,13 @@ "**/*.(t|j)s" ], "coverageDirectory": "../coverage", - "testEnvironment": "node" + "testEnvironment": "node", + "moduleNameMapper": { + "^@/decorators/(.*)$": "/decorators/$1", + "^@/interceptors/(.*)$": "/interceptors/$1", + "^@/middlewares/(.*)$": "/middlewares/$1", + "^@/utils/(.*)$": "/utils/$1", + "^@/guards/(.*)$": "/guards/$1" + } } } From c744c643cec14ddae48419dc4b2930be3fd52f12 Mon Sep 17 00:00:00 2001 From: Matheus Dias Date: Fri, 17 May 2024 16:38:04 -0300 Subject: [PATCH 08/10] refactor: add module alias --- src/users/users.controller.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index c0c11280..5d4a0dc2 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -11,10 +11,10 @@ import { } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; -import { UserGuard } from '../guards/user.guard'; +import { UserGuard } from '@/guards/user.guard'; import { ServerResponse } from '../utils'; import { UsersService } from './users.service'; -import { AdminGuard } from '../guards/admin.guard'; +import { AdminGuard } from '@/guards/admin.guard'; @ApiTags('Usuários') @Controller('users') From 288368bc0bb33744c3ae6a9ad487711def6086d6 Mon Sep 17 00:00:00 2001 From: Matheus Dias Date: Fri, 17 May 2024 18:47:41 -0300 Subject: [PATCH 09/10] fix: moving configuration to jest config file --- jest.config.ts | 5 +++++ package.json | 24 ------------------------ 2 files changed, 5 insertions(+), 24 deletions(-) diff --git a/jest.config.ts b/jest.config.ts index 6bc3f4c4..57c51433 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -13,6 +13,11 @@ const config: Config = { '^src/(.*)$': '/$1', '^@/(.*)$': '/$1', '^test/(.*)$': '/../$1', + "^@/decorators/(.*)$": "/decorators/$1", + "^@/interceptors/(.*)$": "/interceptors/$1", + "^@/middlewares/(.*)$": "/middlewares/$1", + "^@/utils/(.*)$": "/utils/$1", + "^@/guards/(.*)$": "/guards/$1" }, testEnvironment: 'node', }; diff --git a/package.json b/package.json index 8bf4057b..70a6153f 100644 --- a/package.json +++ b/package.json @@ -65,29 +65,5 @@ "ts-node": "^10.9.1", "tsconfig-paths": "^4.2.0", "typescript": "^5.1.3" - }, - "jest": { - "moduleFileExtensions": [ - "js", - "json", - "ts" - ], - "rootDir": "src", - "testRegex": ".*\\.spec\\.ts$", - "transform": { - "^.+\\.(t|j)s$": "ts-jest" - }, - "collectCoverageFrom": [ - "**/*.(t|j)s" - ], - "coverageDirectory": "../coverage", - "testEnvironment": "node", - "moduleNameMapper": { - "^@/decorators/(.*)$": "/decorators/$1", - "^@/interceptors/(.*)$": "/interceptors/$1", - "^@/middlewares/(.*)$": "/middlewares/$1", - "^@/utils/(.*)$": "/utils/$1", - "^@/guards/(.*)$": "/guards/$1" - } } } From aef4cf0017fbd746459295b6e3f8eff884143410 Mon Sep 17 00:00:00 2001 From: Matheus Dias Date: Mon, 20 May 2024 17:29:30 -0300 Subject: [PATCH 10/10] test: refactor controller unit tests for integration tests --- src/users/users.controller.spec.ts | 171 +++++++++++++++-------------- 1 file changed, 89 insertions(+), 82 deletions(-) diff --git a/src/users/users.controller.spec.ts b/src/users/users.controller.spec.ts index 710aa2cb..1d91347e 100644 --- a/src/users/users.controller.spec.ts +++ b/src/users/users.controller.spec.ts @@ -1,119 +1,126 @@ +/* eslint-disable jest/no-conditional-expect */ import { Test, TestingModule } from '@nestjs/testing'; import { UsersController } from './users.controller'; import { UsersService } from './users.service'; +import { ServerResponse } from '../utils'; +import { PrismaService } from 'src/prisma/prisma.service'; describe('UsersController', () => { let controller: UsersController; - let userService: UsersService; + let service: UsersService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [UsersController], - providers: [ - { - provide: UsersService, - useValue: { - store: jest.fn(), - update: jest.fn(), - }, - }, - ], + providers: [UsersService, PrismaService], }).compile(); controller = module.get(UsersController); - userService = module.get(UsersService); + service = module.get(UsersService); }); - it('should be defined', () => { - expect(controller).toBeDefined(); - expect(userService).toBeDefined(); - }); + describe('store', () => { + it('should create a new user', async () => { + const userData = { + name: 'Test', + lastName: 'User', + phone: '123456789', + }; + const response = new ServerResponse(201, 'Successfully created user'); - describe('store function', () => { - const body = { - name: 'Matheus', - lastName: 'Silva', - phone: '44999998311', - }; + jest.spyOn(service, 'store').mockResolvedValueOnce(undefined); - it('checks if the function has been called 1 time', async () => { - await controller.store(body); - expect(userService.store).toHaveBeenCalledTimes(1); + expect(await controller.store(userData)).toEqual(response); }); - it('checks the return of the function', async () => { - const expectedResult = { - message: 'Successfully created user', - respData: undefined, - statusCode: 201, + it('should throw error if creation fails', async () => { + const body = { + name: 'Updated', + lastName: 'User', + phone: '987654321', }; - const result = await controller.store(body); - expect(expectedResult).toEqual(result); - }); - - it('checks if the function fails', async () => { - jest.spyOn(userService, 'store').mockRejectedValueOnce(new Error()); - expect(controller.store(body)).rejects.toThrow(); + jest + .spyOn(service, 'store') + .mockRejectedValueOnce(new Error('Store failed')); + try { + await controller.store(body); + throw new Error('Expected update function to throw an error'); + } catch (error: any) { + expect(error.response).toEqual('Error'); + expect(error.status).toEqual(400); + } }); }); - describe('update function', () => { - const body = { - name: 'Matheus', - lastName: 'Silva', - phone: '44999998311', - }; - const id = 'test_user_id'; + describe('update', () => { + it('should update an existing user', async () => { + const userId = '1'; + const userData = { + name: 'Updated', + lastName: 'User', + phone: '987654321', + }; + const response = new ServerResponse(201, 'Successfully updated user'); - it('checks if the function has been called 1 time', async () => { - await controller.update(body, id); - expect(userService.update).toHaveBeenCalledTimes(1); - }); + jest.spyOn(service, 'update').mockResolvedValueOnce(undefined); - it('checks the return of the function', async () => { - const expectedResult = { - message: 'Successfully updated user', - respData: undefined, - statusCode: 201, - }; - const result = await controller.update(body, id); - expect(expectedResult).toEqual(result); + expect(await controller.update(userData, userId)).toEqual(response); }); - it('checks if the function fails', async () => { - jest.spyOn(userService, 'update').mockRejectedValueOnce(new Error()); - expect(controller.update(body, id)).rejects.toThrow(); + it('should throw error if update fails', async () => { + const id = '1'; + const userData = { + name: 'Updated', + lastName: 'User', + phone: '987654321', + }; + jest + .spyOn(service, 'update') + .mockRejectedValueOnce(new Error('Update failed')); + try { + await controller.update(userData, id); + throw new Error('Expected update function to throw an error'); + } catch (error: any) { + expect(error.response).toEqual('Error'); + expect(error.status).toEqual(400); + } }); }); - describe('selfUpdate function', () => { - const body = { - name: 'Matheus', - lastName: 'Silva', - phone: '44999998311', - }; - const req = { - user: 'test_user_id', - }; + describe('selfUpdate', () => { + it('should update the own user', async () => { + const userId = '1'; + const userData = { + name: 'Updated', + lastName: 'User', + phone: '987654321', + }; + const req = { user: { userId } }; + const response = new ServerResponse(201, 'Successfully updated'); + + jest.spyOn(service, 'update').mockResolvedValueOnce(undefined); - it('checks if the function has been called 1 time', async () => { - await controller.selfUpdate(body, req); - expect(userService.update).toHaveBeenCalledTimes(1); + expect(await controller.selfUpdate(userData, req)).toEqual(response); }); - it('checks the return of the function', async () => { - const expectedResult = { - message: 'Successfully updated', - respData: undefined, - statusCode: 201, + it('should throw error if update fails', async () => { + const userId = '1'; + const userData = { + name: 'Updated', + lastName: 'User', + phone: '987654321', }; - const result = await controller.selfUpdate(body, req); - expect(expectedResult).toEqual(result); - }); - - it('checks if the function fails', async () => { - jest.spyOn(userService, 'update').mockRejectedValueOnce(new Error()); - expect(controller.selfUpdate(body, req)).rejects.toThrow(); + const req = { user: { userId } }; + jest + .spyOn(service, 'update') + .mockRejectedValueOnce(new Error('Update failed')); + try { + await controller.selfUpdate(userData, req); + throw new Error('Expected update function to throw an error'); + } catch (error: any) { + expect(error.response).toEqual('Error'); + expect(error.status).toEqual(400); + } }); }); });