nestjs icon indicating copy to clipboard operation
nestjs copied to clipboard

ts-jest: Cannot create mock of a class which has private dependencies in constructor

Open vc-bhanudatsinh opened this issue 10 months ago • 2 comments

Error: Argument of type 'typeof PermissionGuard' is not assignable to parameter of type 'PartialFuncReturn<{ prototype: { readonly reflector: ...; readonly rolePermissionService: ...; canActivate: ...; }; }>'. Types of property 'prototype' are incompatible. Type 'PermissionGuard' is not assignable to type 'DeepPartial<{ readonly reflector: Reflector; readonly rolePermissionService: RolePermissionService; canActivate: (context: ExecutionContext) => Promise<...>; }>'. Property 'reflector' is private in type 'PermissionGuard' but not in type 'DeepPartial<{ readonly reflector: Reflector; readonly rolePermissionService: RolePermissionService; canActivate: (context: ExecutionContext) => Promise<...>; }>'

 @Injectable()
export class PermissionGuard implements CanActivate {
 constructor(
   private readonly reflector: Reflector,
   private readonly rolePermissionService: RolePermissionService
 ) {}
 }

image

vc-bhanudatsinh avatar Apr 18 '24 05:04 vc-bhanudatsinh

I am getting the same problem.

mtander avatar May 23 '24 20:05 mtander

did you try writing it like this?

.useValue(createMock<PermssionGuard>())

barraponto avatar Aug 27 '24 19:08 barraponto