ng-mocks icon indicating copy to clipboard operation
ng-mocks copied to clipboard

Bug: `RangeError: Maximum call stack size exceeded` when encountering circular reference in component imported using `forwardRef`

Open alexciesielski opened this issue 1 year ago • 2 comments

Description of the bug

I am trying to write a test for a recursive standalone component, which imports a parent component, which imports this component, using forwardRef(() => ParentComponent) in the imports: [] array of the Component decorator declaration, like below

@Component({
  selector: 'y42-parent',
  standalone: true,
  imports: [
    CommonModule,
    ChildComponent,
  ],
})
export class ParentComponent {}
@Component({
  selector: 'y42-child',
  standalone: true,
  imports: [
    CommonModule,
    forwardRef(() => ParentComponent),
  ],
})
export class ChildComponent {}
beforeEach(() => MockBuilder(ChildComponent));
RangeError: Maximum call stack size exceeded

at queueTypesFromModulesArrayRecur (../../node_modules/@angular/core/fesm2015/testing.mjs:23577:49)
at ../../node_modules/@angular/core/fesm2015/testing.mjs:23608:29
    at Array.forEach (<anonymous>)
at queueTypesFromModulesArrayRecur (../../node_modules/@angular/core/fesm2015/testing.mjs:23602:34)
at ../../node_modules/@angular/core/fesm2015/testing.mjs:23608:29
    at Array.forEach (<anonymous>)
at queueTypesFromModulesArrayRecur (../../node_modules/@angular/core/fesm2015/testing.mjs:23602:34)
at ../../node_modules/@angular/core/fesm2015/testing.mjs:23608:29
    at Array.forEach (<anonymous>)
at queueTypesFromModulesArrayRecur (../../node_modules/@angular/core/fesm2015/testing.mjs:23602:34)
at ../../node_modules/@angular/core/fesm2015/testing.mjs:23608:29
    at Array.forEach (<anonymous>)

If I comment out the import of the forwardRef the test correctly instantiates the component so it's definitely because of that. I tried every possible way using .mock, .replace, .keep - but everything results in the above error.

I'm on ng-mocks 14.7.1 and angular 15.2.0

alexciesielski avatar Jun 27 '23 15:06 alexciesielski