feat(core): update TestBed to recognize Standalone Components#45809
feat(core): update TestBed to recognize Standalone Components#45809AndrewKushnir wants to merge 1 commit intoangular:mainfrom
Conversation
889e4ce to
82d6ae5
Compare
packages/core/test/test_bed_spec.ts
Outdated
There was a problem hiding this comment.
@pkozlowski-opensource this is a use-case that might be tricky to support with Standalone Components (i.e. the TestBed.overrideProvider calls) without TestBed.configureTestingModule calls, since TestBed doesn't have enough information at the right time to make the necessary provider overrides. I think we can have a nice error message to let users know and suggest an alternative solution to use TestBed.overrideModule(ComponentDependenciesModule, {set: {providers: [ ... ]}}) instead, i.e. reference a type that declares that provider. So the test code will look like this:
// In this case the `TestBed.configureTestingModule ` is *not* required,
// TestBed knows where to find a class that declares that provider.
TestBed.overrideModule(
ComponentDependenciesModule,
{set: {providers: [ {provide: A, useValue: 'Overridden A' } ]}}
);
const fixture = TestBed.createComponent(MyStandaloneComp);
There was a problem hiding this comment.
The real question for me here is this: should we be able to override providers of sth that got imported into a standalone module? Or should we rather consider it as a "black box" and allow people to override "imports" as the whole - but not pick into individual imports?
TBD
There was a problem hiding this comment.
should we be able to override providers of sth that got imported into a standalone module?
I would say yes, since NgModules worked this way. You could override providers deep in dependencies. The override operation is an explicit piercing of any encapsulation.
There was a problem hiding this comment.
Yeah, in cases when standalone types are used as dependencies (imported somewhere in an NgModule), but a non-standalone component is used to create a testing fixture (via TestBed.createComponent) - provider overrides would work correctly after this PR. However there are still sharp edges with provider overrides when a standalone component is used to create a fixture - I hope to improve this in a followup PR.
82d6ae5 to
3773f4e
Compare
This commit updates an internal logic of the TestBed to recognize Standalone Components to be able to apply the necessary overrides correctly.
3773f4e to
5c9feea
Compare
packages/core/test/test_bed_spec.ts
Outdated
There was a problem hiding this comment.
should we be able to override providers of sth that got imported into a standalone module?
I would say yes, since NgModules worked this way. You could override providers deep in dependencies. The override operation is an explicit piercing of any encapsulation.
| const def = getComponentDef(moduleType); | ||
| const dependencies = maybeUnwrapFn(def.dependencies ?? []); | ||
| for (const dependency of dependencies) { | ||
| this.applyProviderOverridesToModule(dependency); |
There was a problem hiding this comment.
Nit: should we rename this function to applyProviderOverridesToScope or something? Since it's not specifically targreting NgModules?
We can definitely do this in a cleanup.
There was a problem hiding this comment.
Good idea, I will create a followup cleanup PR 👍
|
This PR was merged into the repository by commit 401dec4. |
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This commit updates an internal logic of the TestBed to recognize Standalone Components to be able to apply the necessary overrides correctly.
PR Type
What kind of change does this PR introduce?
Does this PR introduce a breaking change?