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
feat(core): Introduce TestBed.inject to replace TestBed.get #32200
Conversation
@@ -677,7 +677,7 @@ describe('AppComponent', () => { | |||
}); | |||
|
|||
it('should not be loaded/registered until necessary', () => { | |||
const loader: TestElementsLoader = fixture.debugElement.injector.get(ElementsLoader); | |||
const loader = fixture.debugElement.injector.get(ElementsLoader) as unknown as TestElementsLoader; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why this change is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per the "breaking changes" comment, I added AbstractType<T>
to the "typed version" of Injector#get
. Previously abstract classes would have returned any
, the untyped version has been deprecated since Angular 4.x
This caused the returned value here to be type ElementsLoader
which I thought I could cast directly to TestElementsLoader
but they are apparently not fully compatible since the compiler complained about it
This PR breaks a lot of things in |
It can be caused by:
The former can be fixed by using proper typings, the latter can be solved by not marking |
Discussed with Misko, I'll remove the deprecation in this PR and fix breakages. Once it's merged, then I'll migrate google3 code, and then send another PR deprecating TestBed.get |
TestBed.get is not type safe, fixing it would be a massive breaking change. The Angular team has proposed replacing it with TestBed.inject and deprecate TestBed.get. Deprecation from TestBed.get will come as a separate commit. Issue #26491 Fixes #29905 BREAKING CHANGE: Injector.get now accepts abstract classes to return type-safe values. Previous implementation returned `any` through the deprecated implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we change this so that #get behavior is unmodified while #inject projects a type-safe implementation?
I don't think that we can change the behavior of the #get api without causing lots of breakages.
@@ -295,7 +295,7 @@ describe('ViewContainerRef', () => { | |||
const changeDetector = ref.injector.get(ChangeDetectorRef); | |||
changeDetector.detectChanges(); | |||
expect(dynamicComp.doCheckCount).toEqual(1); | |||
expect(changeDetector.context).toEqual(dynamicComp); | |||
expect((changeDetector as any).context).toEqual(dynamicComp); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change tells me that this is a painful breaking change that will likely affect lots of code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this is Injector.get
, not TestBed
, the change here is that we're now taking abstract classes in consideration to return the right type instead of returning any
which is the deprecated behavior since Angular 4.x
I actually don't expect many breakages from Injector.get
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, this only broke 5-6 targets on google3, affecting about 10-15 files which I already fixed.
This is what this commit is doing, I discussed with Vikram (see doc linked at the bottom of the PR) and he had the concern that |
Can you do a global search and replace in the angular code base to switch all of the documentation example and tests to use the new |
I have written the PR for that - #32382 |
…32200) TestBed.get is not type safe, fixing it would be a massive breaking change. The Angular team has proposed replacing it with TestBed.inject and deprecate TestBed.get. Deprecation from TestBed.get will come as a separate commit. Issue angular#26491 Fixes angular#29905 BREAKING CHANGE: Injector.get now accepts abstract classes to return type-safe values. Previous implementation returned `any` through the deprecated implementation. PR Close angular#32200
…tBed.inject` Relates to angular/angular#32200
…32200) TestBed.get is not type safe, fixing it would be a massive breaking change. The Angular team has proposed replacing it with TestBed.inject and deprecate TestBed.get. Deprecation from TestBed.get will come as a separate commit. Issue angular#26491 Fixes angular#29905 BREAKING CHANGE: Injector.get now accepts abstract classes to return type-safe values. Previous implementation returned `any` through the deprecated implementation. PR Close angular#32200
This is cleanup/followup for PR angular#32200 PR Close angular#32382
From 9.0.0 use TestBed.inject See angular#32200 Fixes angular#26491 PR Close angular#32406
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. |
TestBed.get is not type safe, fixing it would be a massive breaking
change. The Angular team has proposed replacing it with TestBed.inject
and deprecate TestBed.get.
Fixes #26491
Fixes #29905
BREAKING CHANGE: Injector.get now accepts abstract classes to return
type-safe values. Previous implementation returned
any
through thedeprecated implementation.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: #26491, #29905
What is the new behavior?
Does this PR introduce a breaking change?
The breaking change affects a feature that has been deprecated since Angular 4.x
Injector#get()
has 2 signatures:InjectionToken<T>
and a class (Type<T>
)any
The updated Typed signature now also accepts abstract classes (
AbstractType<T>
) which may cause compilation issues to the deprecated use ofany
as it would need to be manually downcasted to the concrete type if needed.Other information
Internal doc - go/testbed.inject
Note: I did not update all uses of
TestBed.get
in the Angular codebase to keep this diff small.I will follow with another CL migrating all
TestBed.get
toTestBed.inject
if this PR goes through.