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: MockComponent (Standalone) mocks all components not just the ones specified #8649

Open
Kogs opened this issue Apr 3, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@Kogs
Copy link

Kogs commented Apr 3, 2024

Description of the bug

Using MockComponent to mock one dependent child Component all other child Components are mocked as well.

An example of the bug

If you have total 3 components (All Components are Standalone):
MyComp
MyComp1
MyComp2
And you want to Test MyComp but mock MyComp1 and keep the real MyComp2.
The following example will result in all Components (MyComp1 & MyComp2) to get Mocked for some reason.

<app-my-comp1></app-my-comp1>
<app-my-comp2></app-my-comp2>
describe('MyComp', () => {
 beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [
         MockComponent(MyComp1)
      ],
      imports: [
          MyComp
      ]
    }).compileComponents();
   ....
});

After a lot of try and error I figured out how to solve this.
Workaround:

await TestBed.configureTestingModule({
  declarations: [
    MockComponent(MyComp1),
    MyComp2
  ],
  imports: [
      MyComp
  ]
}).compileComponents();

By adding the MyComp2 to the declarations everything works as expected.
I also tried to move declarations into the imports section like it's suggested for Standalone components, but this didn't make any difference.

Angular: 17.1.2 (Standalone Components)
NgMocks: 14.12.1

Expected vs actual behavior

I would expect that only the specified Components that I put into MockComponent are replaced with Mock variants not everything else?

@Kogs Kogs added the bug Something isn't working label Apr 3, 2024
@GipHub123
Copy link

@Kogs

To test standalone components, try setting up your test cases using MockBuilder;
Check example: https://github.com/help-me-mom/ng-mocks/blob/master/examples/TestStandaloneComponent/test.spec.ts


beforeEach(async () => {

  return MockBuilder(MyComp)
    .keep(MyComp1)
    .mock(MyComp2);
});

Here is another real life example;

beforeEach(async () => {

  return MockBuilder(ButtonActionComponent)
    .keep(MatButtonModule)
    .keep(MatIconModule)
    .keep(MatTooltipModule)
    .keep(TranslateModule)
    .mock(ComponentX);

});

You want to usually isolate test case meaning that better practice is to mock all components that are not necessary for your test case. In another words use should probably mock MyComp2 as well :)

@screambeard
Copy link

@GipHub123 thanks for the explanation, because I'm also encountering this issue. If I understand correctly, MockComponent and MockComponents mocks all components in the target component independently of the arguments for those functions?

Your comment about that you should probably mock MyComp2 as well, can be true, but is largely dependent of the test your are writing. Also using the MockBuilder forces you to rewrite the tests to work with the MockBuilder.

It would make more sense that MockComponent and MockComponents only mocks the actual components provided. So curious to hear from the maintainer whether this is a bug or not or what is the best way to work around this is using MockBuilder is not an option.

@GipHub123
Copy link

GipHub123 commented Apr 30, 2024

Your comment about that you should probably mock MyComp2 as well, can be true, but is largely dependent of the test your are writing. Also using the MockBuilder forces you to rewrite the tests to work with the MockBuilder.

@screambeard Yes I totally agree with you 👍 I was trying to say to that mocking makes usually more sense.

Also using the MockBuilder forces you to rewrite the tests to work with the MockBuilder.
Yes. Rewriting tests setup can be haunting task. Been there 😄

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

4 participants