Prefer declared component instead of same component on imported module
See original GitHub issueHi! First of all, thanks for your hard work simplificating my tests and I hope this becomes the de-facto library for Angular tests (as its name almost suggests it)✌
When we provide a service that’s already contained in one of the imorted modules, Angular Testing Library gives preference to the one on the providers array. 👍
import { MockProvider} from 'ng-mocks';
await render(ComponentToTest, {
imports: [ModuleContainingServiceAbc],
providers: [MockProvider(ServiceAbc)] // Succeeds to use this one
});
I expected the same to happen on the declarations array, but this is not happening.
Is this the implemented behavior? I’m not sure if I may be doing something wrong.
import { MockComponent } from 'ng-mocks';
await render(ComponentToTest, {
imports: [ModuleContainingComponentAbc],
declarations: [MockComponent(ComponentAbc)] // Fails to use this one
});
On my specific case, ComponentAbc is not directly declared and exported by ModuleContainingComponentAbc, but by another module that’s imported by ModuleContainingComponentAbc.
If you need details on how ng-mocks creates the mocks: https://ng-mocks.sudo.eu/api/MockProvider https://ng-mocks.sudo.eu/api/MockComponent
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)

Top Related StackOverflow Question
Hi. Sorry for the delay. The example below works using
ngMocks.guts. Thanks @satanTime 👍@timdeschryver not being a fan of shallow rendering, what do you normally do instead? On my case the parent component has references to N child components, and these child components calls web-services on
ngOnInit, so I wanted to avoid having to create mocks for each of these services. Thanks for the support!Hi @marcioggs,
in theory “exclude” option of
ng-mocksshould help you here:There is an article about
spectator, however, the same should be true forangular-testing-library: https://ng-mocks.sudo.eu/extra/with-3rd-party.