-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
Comments
To test standalone components, try setting up your test cases using
Here is another real life example;
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 :) |
@GipHub123 thanks for the explanation, because I'm also encountering this issue. If I understand correctly, 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. |
@screambeard Yes I totally agree with you 👍 I was trying to say to that mocking makes usually more sense.
|
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.
After a lot of try and error I figured out how to solve this.
Workaround:
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?The text was updated successfully, but these errors were encountered: