suites-dev/suites

bug: combining @Inject() with an interface throws an error

iddan opened this issue · 1 comments

iddan commented

The library throws an error when trying to mock a Nest.js service with configuration dependency

The code:

@Injectable()
export class EmailService {
  constructor(
    @InjectPinoLogger(EmailService.name)
    private readonly logger: PinoLogger,
    @Inject(emailConfig.KEY) private readonly config: ConfigType<typeof emailConfig> // <-- This one
  ) {

The error:

    TypeError: Cannot read properties of undefined (reading 'name')

      33 |
      34 |   beforeAll(() => {
    > 35 |     const { unit } = TestBed.create(EmailService).compile();
         |                              ^
      36 |
      37 |     emailService = unit;
      38 |   });

      at ../node_modules/@automock/jest/dist/lib/reflector.service.js:16:22
          at Array.forEach (<anonymous>)
      at ReflectorService.reflectDependencies (../node_modules/@automock/jest/dist/lib/reflector.service.js:14:15)
      at new TestBedResolver (../node_modules/@automock/jest/dist/lib/test-bed-resolver.js:13:44)
      at Function.create (../node_modules/@automock/jest/dist/lib/testbed-factory.js:15:16)
      at email/services/email.service.spec.ts:35:30

The lib code:

class ReflectorService {
    constructor(reflector) {
        this.reflector = reflector;
    }
    reflectDependencies(targetClass) {
        const classDependencies = new Map();
        const types = this.reflectParamTypes(targetClass);
        const tokens = this.reflectParamTokens(targetClass);
        const duplicates = ReflectorService.findDuplicates([...types]).map((typeOrToken) => typeof typeOrToken === 'string' ? typeOrToken : typeOrToken.name);
        types.forEach((type, index) => {
            if (type.name === 'Object' || duplicates.includes(type.name)) { // <-- throws here

Thanks for submitting this issue @iddan, I will take a look 👀