Skip to content

Bug: Reflection Issue with Dependencies using forwardRef (NestJS) #84

@omermorad

Description

@omermorad

Description

I discovered a bug related to the usage of the @Inject decorator with forwardRef in the class constructor (with NestJS). When circular dependencies exist due to the import statements, the reflection mechanism of reflect-metadata doesn't work correctly, resulting in undefined values for the forward-referenced dependency (dependency in this case).

The scenario is as follows:

class Main {
  public constructor(
    @Inject(forwardRef(() => Dependency)) private readonly dependency: Dependency;
  ) {}
}

When the reflect-metadata is unable to resolve the circular import, the dependencySix property ends up being assigned the value undefined, rather than properly reflecting the intended type DependencySix.

This issue disrupts Automock's ability to handle classes with circular dependencies that involve the use of forwardRef, leading to unexpected behavior and possible errors when trying to mock and test such classes.

Steps to Reproduce:

  1. Set up a class constructor with a dependency using @Inject and forwardRef, as shown in the description.
  2. Trigger the circular import scenario to create the reflection issue.
  3. Attempt to use Automock to mock the dependencies and perform unit tests on the class.

Expected Behavior:
Automock should correctly reflect the forwardRef dependency and provide the expected type DependencySix for the dependencySix property.

Current Behavior:
Due to the circular import causing a reflection issue, Automock assigns the dependencySix property the value undefined, resulting in incorrect behavior during mocking and testing.

Possible Solution:
To resolve this issue, modifications need to be made to the Automock logic. Specifically, handling the circular import scenario properly and ensuring that the forwardRef dependency is correctly reflected to avoid assigning undefined to the property.

Might be related to #67

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions