Closed yjaaidi closed 2 years ago
@yjaaidi can you explain "why" this is necessary? I'm trying to understand the use-case.
In my example I have an OvveridesComponent
which sets a provider in it's Decorator and then mounting does NOT require any providers for it work.
However, in a case where the component is provided a Service from it's module it would fail in this case because it is expecting to receive the provider from the TestBed Module as well. Am I missing something?
Typically, when testing the OverridesComponent
, you might want to override the provider and pass a test double that behaves differently, causing errors or simply returning a static value etc...
mount(OverridesComponent, {
providers: [
{
provide: MyService
useValue: {
tick$: of(42), // or throwError(new Error('💥')) or a Subject so we can emit new values and see how the component behaves
} as MyService
}
]
})
However, in a case where the component is provided a Service from its module it would fail in this case because it is expecting to receive the provider from the TestBed Module as well. Am I missing something?
No, because the test double provided at the component level will shadow the module service and take precedence which is what we want.
Does this make any sense?
When users set
providers
using themount
function, these providers should not be set at theTestBed
module level but at the component level using theTestBed.overrideComponent
function, somewhat like thisTestBed.overrideComponent(componentType, {add: {providers}})
function.It should be
add
and notset
because the last added provider will overwrite the previous ones if it's the same injection token or class.