-
-
Notifications
You must be signed in to change notification settings - Fork 71
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: A module that is explicitly marked to be kept should be kept even if it's imported in another module that is mocked #2647
Comments
Hi @DmitryEfimenko, it works exactly as you expect, even if However , it might be present under a rebuilt module which equal Could you describe your real use-case where you are facing an issue? |
hey, thanks for getting back to me so quickly, as usual! Have you looked at the repro link that I posted? I'm going to create another repro trying to show a real Angular unit test use-case where I stumbled on this issue. I'll post a link to that shortly |
This is how it works: https://codesandbox.io/s/red-cdn-vhbeyp?file=/src/tests/ng-mocks-change/reactive-forms.spec.ts Because |
Ah, that makes sense. For now, you can try to rely on modules instead of explicitly mocking / keeping things. beforeEach(() => MockBuilder(ComponentToTest, ItsModuleWIthDependenciesToMock)); it should respect the place of definition. And I'll add a fix that such Components from Services are present in both places: declarations and providers. |
That would be awesome! Thanks for the advice |
just to have latest info in this issue, here's the latest and the minimum reproduction of the error that I could come up with: You've asked me to see if I can help with the reproduction "on your side". Could you please clarify what you mean by that? |
I meant to provide a repo with the issue, so I could work on it on my side to find and fix the bug. So, basically, exactly what you did :) Thanks for the link. I'll take a look next days where the problem is. |
So, Thanks again. That should be enough for debugging. |
And removal of Anyway, I should work properly with both definitions, it will be fixed. |
Hi @DmitryEfimenko, I also wanted to ask you about https://twitter.com/dmitryaefimenko/status/1529985415247081473, could you create a feature request and put there your thoughts about which syntax you would like to see or the problem to solve? |
Sure thing. TLDR: Details: <mat-date-range-input [rangePicker]="dateRangePicker">
<input [formControl]="startDate.control" matStartDate />
<input [formControl]="endDate.control" matEndDate />
<app-ms-to-date-input
#startDate
[(ngModel)]="vm.startDate"
name="startDate"
></app-ms-to-date-input>
<app-ms-to-date-input
#endDate
[(ngModel)]="vm.endDate"
name="endDate"
></app-ms-to-date-input>
</mat-date-range-input>
<mat-date-range-picker #dateRangePicker></mat-date-range-picker> You see, the As for the tweet, I'm on it. |
So, I've taken a look deeper. This behavior is expected. The problem here is that To make it work the module should be exported: beforeEach(() => MockBuilder(ComponentUnderTest)
.mock(NgxErrorsModule)
.keep(FormsModule, {export: true})
.keep(ReactiveFormsModule, {export: true})); or a shorter version beforeEach(() => MockBuilder(
[ComponentUnderTest, FormsModule, ReactiveFormsModule], // things to keep and export
NgxErrorsModule, // things to mock
)); I need to think if it's worth to export kept things on mock declarations by default. There is a partially related FR already: #725. |
Just a note. The initial expectation is that beforeEach(() => MockBuilder(ComponentUnderTest, TheModuleOfComponentUnderTest)
.keep(FormsModule)
.keep(ReactiveFormsModule)
); It's generating: @NgModule({
imports: [FormsModule, ReactiveFormsModule, MockModule(NgxErrorsModule)],
declarations: [ComponentUnderTest],
})
class MockOfTheModuleOfComponentUnderTest {} whereas your setup generates: @NgModule({
imports: [FormsModule, MockModule(NgxErrorsModule)],
declarations: [ComponentUnderTest],
})
class MockOfMockBuilder {} Is there a reason why you don't want to provide modules in |
A possible solution: if |
ahh, this explains a lot. I think I was not using the the signature where I'd pass modules to mock in the MockBuilder because I didn't quite understand the signature of the MockBuilder. After you mentioned provided the code sample (below) it all cleared up in my mind and I will totally use this signature going forward: MockBuilder(
[ComponentUnderTest, FormsModule, ReactiveFormsModule], // things to keep and export
[NgxErrorsModule], // things to mock
) I think it would be helpful to others if this particular example (with the arrays) is placed in the docs. Now that that's out of the way, I have to mention that I don't quite understand why we would not apply |
…#2647 [ ] throw if a dependency wasn't found [ ] update docs about form testing [ ] update migration docs about the breaking change BREAKING CHANGE: MockBuilder with 2 params marks all chain calls as dependency BREAKING CHANGE: MockBuilder with 0-1 params marks all chain calls as export
…#2647 - [ ] throw if a dependency wasn't found - [ ] update docs about form testing - [ ] update migration docs about the breaking change BREAKING CHANGE: MockBuilder with 2 params marks all chain calls as dependency BREAKING CHANGE: MockBuilder with 0-1 params marks all chain calls as export
…#2647 - [ ] throw if a dependency wasn't found - [ ] update docs about form testing - [ ] update migration docs about the breaking change BREAKING CHANGE: MockBuilder with 2 params marks all chain calls as dependency BREAKING CHANGE: MockBuilder with 0-1 params marks all chain calls as export
…2647 BREAKING CHANGE: MockBuilder with 2 params marks all chain calls as dependency BREAKING CHANGE: MockBuilder with 0-1 params marks all chain calls as export
…2647 BREAKING CHANGE: MockBuilder with 2 params marks all chain calls as dependency BREAKING CHANGE: MockBuilder with 0-1 params marks all chain calls as export
…2647 BREAKING CHANGE: MockBuilder with 2 params marks all chain calls as dependency BREAKING CHANGE: MockBuilder with 0-1 params marks all chain calls as export Please read: https://ng-mocks.sudo.eu/migrations#from-13-to-14
…2647 BREAKING CHANGE: MockBuilder with 2 params marks all chain calls as dependency BREAKING CHANGE: MockBuilder with 0-1 params marks all chain calls as export Please read: https://ng-mocks.sudo.eu/migrations#from-13-to-14
…2647 BREAKING CHANGE: MockBuilder with 2 params marks all chain calls as dependency BREAKING CHANGE: MockBuilder with 0-1 params marks all chain calls as export Please read: https://ng-mocks.sudo.eu/migrations#from-13-to-14
…2647 BREAKING CHANGE: MockBuilder with 2 params marks all chain calls as dependency BREAKING CHANGE: MockBuilder with 0-1 params marks all chain calls as export Please read: https://ng-mocks.sudo.eu/migrations#from-13-to-14
feat(MockBuilder): default params as dependency or export #2647
Awesome! I'll verify on my end once it's published to npm. Could you please comment here once that happens so that I don't miss this exciting event? |
Sure. There will be a comment about the related release. I'm waiting for primeng's support of a14, and then I can release the new version. |
v14.0.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. |
Description of the bug
Let's say that my component under test uses some other components that come from 3rd party library. These components are made available through a module that the 3rd party library also provides. Under the hood, the 3rd party library module is using
ReactiveFormsModule
.Now, lets say that my component also does some forms stuff, so I need to use
ReactiveFormsModule
as well. Therefore I'd want to mock all the components that are coming from the 3rd party library, but keepReactiveFormsModule
.I'd think that I should achieve this behavior via the following:
This however does not seem to work. The
ReactiveFormsModule
is not found in thedeps.imports
. Please see the repro linkA repo with an example of the bug
Link: https://codesandbox.io/s/nostalgic-moore-7sezz5?file=/src/test.spec.ts
Expected vs actual behavior
The
ReactiveFormsModule
should be included in thedeps.imports
.The text was updated successfully, but these errors were encountered: