Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alexciesielski opened this issue Jun 27, 2023 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@alexciesielski
Copy link

alexciesielski commented Jun 27, 2023

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 alexciesielski added the bug Something isn't working label Jun 27, 2023
@alexciesielski
Copy link
Author

alexciesielski commented Jun 27, 2023

I just updated to latest ng-mocks 14.11.0 and I still get the error

@alexciesielski alexciesielski changed the title Bug: RangeError: Maximum call stack size exceeded when importing component using forwardRef in standalone component Bug: RangeError: Maximum call stack size exceeded when encountering circular reference in component imported using forwardRef Jun 28, 2023
@satanTime
Copy link
Member

Hi @alexciesielski,

thank you for reporting the issue.
It looks like an angular issue.

I tested on ng-mocks@14.11.0 and angular@15.2.10 and it works correctly.

Could you confirm it still fails on your side?
The test I'm using is:

import {CommonModule} from '@angular/common';
import {Component, forwardRef,} from '@angular/core';
import {MockBuilder, MockRender, ngMocks} from 'ng-mocks';

@Component({
  selector: 'y42-child',
  standalone: true,
  imports: [
    CommonModule,
    forwardRef(() => ParentComponent),
  ],
  template: 'ChildComponent'
})
export class ChildComponent {}

@Component({
  selector: 'y42-parent',
  standalone: true,
  imports: [
    CommonModule,
    ChildComponent,
  ],
  template: 'ParentComponent',
})
export class ParentComponent {}

describe('my sandbox', () => {
  describe('ChildComponent', () => {
    beforeEach(() => MockBuilder(ChildComponent));

    it('should render ChildComponent', () => {
      const fixture = MockRender(ChildComponent);

      // Asserting it got the right paramId.
      expect(ngMocks.formatText(fixture)).toEqual('ChildComponent');
    });
  });

  describe('ParentComponent',() => {
    beforeEach(() => MockBuilder(ParentComponent));

    it('should render ParentComponent', () => {
      const fixture = MockRender(ParentComponent);

      // Asserting it got the right paramId.
      expect(ngMocks.formatText(fixture)).toEqual('ParentComponent');
    });
  });
});

the result:

> ng test --ts-config ./tsconfig.json --progress=false

15 11 2023 14:37:43.200:INFO [karma-server]: Karma v6.4.2 server started at http://localhost:9877/
15 11 2023 14:37:43.202:INFO [launcher]: Launching browsers ChromeCi with concurrency unlimited
15 11 2023 14:37:43.205:INFO [launcher]: Starting browser ChromeHeadless
15 11 2023 14:37:47.090:INFO [Chrome Headless 101.0.4950.0 (Mac OS 10.15.7)]: Connected on socket 9TlMSNuu1kSnwDp7AAAB with id 90245406
TOTAL: 4 SUCCESS

> jest --config jest.config.ts

jest-haste-map: Haste module naming collision: a15
  The following files share their name; please adjust your hasteImpl:
    * <rootDir>/package.json
    * <rootDir>/dist/lib/package.json

 PASS  src/app/app.component.spec.ts (5.507 s)
 PASS  src/issue/test.spec.ts

Test Suites: 2 passed, 2 total
Tests:       4 passed, 4 total
Snapshots:   0 total
Time:        7.466 s
Ran all test suites.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants