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: Mocking standalone components may cause warnings #6928

Closed
clusterberries opened this issue Oct 9, 2023 · 6 comments · Fixed by #7440
Closed

Bug: Mocking standalone components may cause warnings #6928

clusterberries opened this issue Oct 9, 2023 · 6 comments · Fixed by #7440
Assignees
Labels
bug Something isn't working released v14.12.0

Comments

@clusterberries
Copy link

Description of the bug

In my app, I have started using standalone components and found a bug related to it. Note that this issue does not occur with non-standalone components.

Steps:

  1. There is a SharedModule with some components used across the application.
  2. There is a standalone component StandaloneComponent that uses components from SharedModule.
  3. MyComponent (not standalone) uses StandaloneComponent and also some components from SharedModule.
  4. Create tests for MyComponent and mock the dependencies using MockComponent like the following example:
    TestBed.configureTestingModule({
      imports: [MockComponent(StandaloneComponent)],
      declarations: [MyComponent, MockComponent(SharedComponent2)],
    }).compileComponents();

Result: After running this test there is a warning in the console NG0304: 'app-shared2' is not a known element (used in the 'MyComponent' component template)

Workaround: mock the entire SharedModule instead of mocking separate components. However, this approach may cause performance issues if SharedModule contains a large number of components.

An example of the bug

Link: https://stackblitz.com/edit/github-pyxbdq?devToolsHeight=33&file=src%2Ftest.spec.ts

Expected vs actual behavior

Actual result: a warning in the console NG0304: 'app-shared2' is not a known element (used in the 'MyComponent' component template).
Expected result: The warning should not appear, since the SharedComponent2 is properly mocked.

Thank you for checking this out!

@clusterberries clusterberries added the bug Something isn't working label Oct 9, 2023
@satanTime
Copy link
Member

Hi @clusterberries,

thanks for highlighting the issue. Working on its fix.

@satanTime
Copy link
Member

The fix is here. However, even without ng-mocks it throws:

Error: Type SharedComponent2 is part of the declarations of 2 modules: DynamicTestModule and SharedModule!
Please consider moving SharedComponent2 to a higher module that imports DynamicTestModule and SharedModule.
You can also create a new NgModule that exports and includes SharedComponent2 then import that NgModule in DynamicTestModule and SharedModule.

ng-mocks will process declarations in the same way and will throw the same error now:

Error: Type MockOfSharedComponent2 is part of the declarations of 2 modules: DynamicTestModule and MockOfSharedModule!
Please consider moving MockOfSharedComponent2 to a higher module that imports DynamicTestModule and MockOfSharedModule.
You can also create a new NgModule that exports and includes MockOfSharedComponent2 then import that NgModule in DynamicTestModule and MockOfSharedModule.

@satanTime
Copy link
Member

satanTime commented Nov 15, 2023

The right way is to import a mock of SharedModule.

@satanTime
Copy link
Member

So, I found a way to fix that in a flexible way. In case of ng-mocks, TestBed will receive SharedModule in imports instead of SharedComponent2 in declarations.

satanTime added a commit that referenced this issue Nov 18, 2023
fix(core): respecting the order of declaration in TestBed #6928
@satanTime
Copy link
Member

v14.12.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems.

@clusterberries
Copy link
Author

Hi @satanTime, thank you! It works excellently now

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

Successfully merging a pull request may close this issue.

2 participants