1- /* eslint-disable @typescript-eslint/no-misused-promises -- Jest handles async test functions */
2- /* eslint-disable @typescript-eslint/no-unnecessary-condition -- Test assertions need optional chains */
31import { UnauthorizedException , BadRequestException } from '@nestjs/common' ;
42import { JwtService } from '@nestjs/jwt' ;
53import { Test , type TestingModule } from '@nestjs/testing' ;
@@ -16,7 +14,14 @@ import { UsersService } from '../users/users.service';
1614
1715// Mock bcrypt
1816jest . mock ( 'bcrypt' ) ;
19- const mockedBcrypt = bcrypt as jest . Mocked < typeof bcrypt > ;
17+ const mockedBcrypt = {
18+ compare : bcrypt . compare as jest . MockedFunction <
19+ ( data : string | Buffer , encrypted : string ) => Promise < boolean >
20+ > ,
21+ hash : bcrypt . hash as jest . MockedFunction <
22+ ( data : string | Buffer , saltOrRounds : string | number ) => Promise < string >
23+ > ,
24+ } ;
2025
2126describe ( 'AuthService' , ( ) => {
2227 let service : AuthService ;
@@ -119,13 +124,13 @@ describe('AuthService', () => {
119124 describe ( 'validateUser' , ( ) => {
120125 it ( 'should return user without password when credentials are valid' , async ( ) => {
121126 usersService . findByEmail . mockResolvedValue ( mockUser as User ) ;
122- mockedBcrypt . compare . mockImplementation ( ( ) => Promise . resolve ( true ) ) ;
127+ mockedBcrypt . compare . mockResolvedValue ( true ) ;
123128
124129 const result = await service . validateUser ( 'test@example.com' , 'password' ) ;
125130
126131 expect ( result ) . toBeDefined ( ) ;
127132 expect ( result ?. email ) . toBe ( 'test@example.com' ) ;
128- expect ( ( result as Record < string , unknown > ) ? .password ) . toBeUndefined ( ) ;
133+ expect ( ( result as Record < string , unknown > ) . password ) . toBeUndefined ( ) ;
129134 } ) ;
130135
131136 it ( 'should return null when user does not exist' , async ( ) => {
@@ -152,7 +157,7 @@ describe('AuthService', () => {
152157
153158 it ( 'should return null when password is incorrect' , async ( ) => {
154159 usersService . findByEmail . mockResolvedValue ( mockUser as User ) ;
155- mockedBcrypt . compare . mockImplementation ( ( ) => Promise . resolve ( false ) ) ;
160+ ( mockedBcrypt . compare as jest . Mock ) . mockResolvedValue ( false ) ;
156161
157162 const result = await service . validateUser (
158163 'test@example.com' ,
@@ -166,7 +171,7 @@ describe('AuthService', () => {
166171 describe ( 'login' , ( ) => {
167172 it ( 'should return access token and user for valid credentials' , async ( ) => {
168173 usersService . findByEmail . mockResolvedValue ( mockUser as User ) ;
169- mockedBcrypt . compare . mockImplementation ( ( ) => Promise . resolve ( true ) ) ;
174+ mockedBcrypt . compare . mockResolvedValue ( true ) ;
170175
171176 const result = await service . login ( {
172177 email : 'test@example.com' ,
@@ -192,7 +197,7 @@ describe('AuthService', () => {
192197
193198 it ( 'should throw UnauthorizedException for unverified email' , async ( ) => {
194199 usersService . findByEmail . mockResolvedValue ( mockUnverifiedUser as User ) ;
195- mockedBcrypt . compare . mockImplementation ( ( ) => Promise . resolve ( true ) ) ;
200+ mockedBcrypt . compare . mockResolvedValue ( true ) ;
196201
197202 await expect (
198203 service . login ( {
@@ -589,7 +594,7 @@ describe('AuthService', () => {
589594 usersService . hasPassword . mockResolvedValue ( true ) ;
590595 usersService . findOne . mockResolvedValue ( mockUser as User ) ;
591596 usersService . findByEmail . mockResolvedValue ( mockUser as User ) ;
592- mockedBcrypt . compare . mockImplementation ( ( ) => Promise . resolve ( true ) ) ;
597+ mockedBcrypt . compare . mockResolvedValue ( true ) ;
593598
594599 const result = await service . deleteAccount (
595600 'user-1' ,
@@ -622,7 +627,7 @@ describe('AuthService', () => {
622627 usersService . hasPassword . mockResolvedValue ( true ) ;
623628 usersService . findOne . mockResolvedValue ( mockUser as User ) ;
624629 usersService . findByEmail . mockResolvedValue ( mockUser as User ) ;
625- mockedBcrypt . compare . mockImplementation ( ( ) => Promise . resolve ( false ) ) ;
630+ mockedBcrypt . compare . mockResolvedValue ( false ) ;
626631
627632 await expect (
628633 service . deleteAccount ( 'user-1' , 'wrongpassword' , 'DELETE' ) ,
0 commit comments