golevelup / nestjs

A collection of badass modules and utilities to help you level up your NestJS applications 🚀
MIT License
2.26k stars 259 forks source link

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

Closed vc-bhanudatsinh closed 1 week ago

vc-bhanudatsinh commented 5 months ago

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

mtander commented 4 months ago

I am getting the same problem.

barraponto commented 1 month ago

did you try writing it like this?

.useValue(createMock<PermssionGuard>())