Skip to content

Commit 5e209cb

Browse files
crisbetopkozlowski-opensource
authored andcommitted
feat(core): remove TestBed.get (#60414)
`TestBed.get` isn't type safe and has been deprecated for several years now. These changes remove it from the public API and a follow-up change will add an automated migration to `TestBed.inject`. BREAKING CHANGE: * `TestBed.get` has been removed. Use `TestBed.inject` instead. PR Close #60414
1 parent b461e06 commit 5e209cb

File tree

15 files changed

+42
-54
lines changed

15 files changed

+42
-54
lines changed

goldens/public-api/core/testing/index.api.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ export interface TestBed {
124124
// (undocumented)
125125
execute(tokens: any[], fn: Function, context?: any): any;
126126
flushEffects(): void;
127-
// @deprecated (undocumented)
128-
get(token: any, notFoundValue?: any): any;
129127
initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, options?: TestEnvironmentOptions): void;
130128
// (undocumented)
131129
inject<T>(token: ProviderToken<T>, notFoundValue: undefined, options: InjectOptions & {

packages/common/http/test/module_spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,15 @@ class ReentrantInterceptor implements HttpInterceptor {
7979
describe('HttpClientModule', () => {
8080
let injector: Injector;
8181
beforeEach(() => {
82-
injector = TestBed.configureTestingModule({
82+
TestBed.configureTestingModule({
8383
imports: [HttpClientTestingModule],
8484
providers: [
8585
{provide: HTTP_INTERCEPTORS, useClass: InterceptorA, multi: true},
8686
{provide: HTTP_INTERCEPTORS, useClass: InterceptorB, multi: true},
8787
{provide: HTTP_INTERCEPTORS, useClass: InterceptorC, multi: true},
8888
],
8989
});
90+
injector = TestBed.inject(Injector);
9091
});
9192
it('initializes HttpClient properly', (done) => {
9293
injector
@@ -132,10 +133,11 @@ describe('HttpClientModule', () => {
132133
});
133134
it('allows interceptors to inject HttpClient', (done) => {
134135
TestBed.resetTestingModule();
135-
injector = TestBed.configureTestingModule({
136+
TestBed.configureTestingModule({
136137
imports: [HttpClientTestingModule],
137138
providers: [{provide: HTTP_INTERCEPTORS, useClass: ReentrantInterceptor, multi: true}],
138139
});
140+
injector = TestBed.inject(Injector);
139141
injector
140142
.get(HttpClient)
141143
.get('/test')

packages/core/test/acceptance/di_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2873,7 +2873,7 @@ describe('di', () => {
28732873
constructor(public injector: Injector) {}
28742874
}
28752875

2876-
const testBedInjector: Injector = TestBed.get(Injector);
2876+
const testBedInjector = TestBed.inject(Injector);
28772877
const childInjector = Injector.create({providers: [], parent: testBedInjector});
28782878

28792879
const anyService = childInjector.get(AnyService);

packages/core/test/application_ref_integration_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('ApplicationRef bootstrap', () => {
6969
expect(helloWorldComponent.log).toEqual(['OnInit', 'DoCheck', 'DoCheck']);
7070

7171
// Cleanup TestabilityRegistry
72-
const registry: TestabilityRegistry = getTestBed().get(TestabilityRegistry);
72+
const registry = getTestBed().inject(TestabilityRegistry);
7373
registry.unregisterAllApplications();
7474
}),
7575
);

packages/core/test/application_ref_spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import {
1717
Component,
1818
EnvironmentInjector,
1919
InjectionToken,
20+
Injector,
2021
LOCALE_ID,
2122
NgModule,
2223
NgZone,
2324
PlatformRef,
25+
ProviderToken,
2426
provideZoneChangeDetection,
2527
RendererFactory2,
2628
TemplateRef,
@@ -134,7 +136,7 @@ describe('bootstrap', () => {
134136

135137
createRootEl();
136138
const modFactory = compiler.compileModuleSync(SomeModule);
137-
const module = modFactory.create(TestBed);
139+
const module = modFactory.create(TestBed.inject(Injector));
138140
const cmpFactory = module.componentFactoryResolver.resolveComponentFactory(SomeComponent);
139141
const component = app.bootstrap(cmpFactory);
140142

@@ -162,7 +164,7 @@ describe('bootstrap', () => {
162164

163165
createRootEl('custom-selector');
164166
const modFactory = compiler.compileModuleSync(SomeModule);
165-
const module = modFactory.create(TestBed);
167+
const module = modFactory.create(TestBed.inject(Injector));
166168
const cmpFactory = module.componentFactoryResolver.resolveComponentFactory(SomeComponent);
167169
const component = app.bootstrap(cmpFactory, 'custom-selector');
168170

packages/core/test/linker/integration_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,7 @@ describe('integration tests', function () {
15781578
});
15791579
const template = '<div><div *someImpvp="ctxBoolProp">hello</div></div>';
15801580
TestBed.overrideComponent(MyComp, {set: {template}});
1581-
const anchorElement = getTestBed().get(ANCHOR_ELEMENT);
1581+
const anchorElement = getTestBed().inject(ANCHOR_ELEMENT);
15821582
const fixture = TestBed.createComponent(MyComp);
15831583

15841584
fixture.detectChanges();

packages/core/test/linker/security_integration_spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe('security integration tests', function () {
9696
const template = `<a [href]="ctxProp">Link Title</a>`;
9797
TestBed.overrideComponent(SecuredComponent, {set: {template}});
9898
const fixture = TestBed.createComponent(SecuredComponent);
99-
const sanitizer: DomSanitizer = getTestBed().get(DomSanitizer);
99+
const sanitizer = getTestBed().inject(DomSanitizer);
100100

101101
const e = fixture.debugElement.children[0].nativeElement;
102102
const ci = fixture.componentInstance;
@@ -110,7 +110,7 @@ describe('security integration tests', function () {
110110
const template = `<a [href]="ctxProp">Link Title</a>`;
111111
TestBed.overrideComponent(SecuredComponent, {set: {template}});
112112
const fixture = TestBed.createComponent(SecuredComponent);
113-
const sanitizer: DomSanitizer = getTestBed().get(DomSanitizer);
113+
const sanitizer = getTestBed().inject(DomSanitizer);
114114

115115
const trusted = sanitizer.bypassSecurityTrustScript('javascript:alert(1)');
116116
const ci = fixture.componentInstance;
@@ -122,7 +122,7 @@ describe('security integration tests', function () {
122122
const template = `<a href="/foo/{{ctxProp}}">Link Title</a>`;
123123
TestBed.overrideComponent(SecuredComponent, {set: {template}});
124124
const fixture = TestBed.createComponent(SecuredComponent);
125-
const sanitizer: DomSanitizer = getTestBed().get(DomSanitizer);
125+
const sanitizer: DomSanitizer = getTestBed().inject(DomSanitizer);
126126

127127
const e = fixture.debugElement.children[0].nativeElement;
128128
const trusted = sanitizer.bypassSecurityTrustUrl('bar/baz');

packages/core/test/render3/providers_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ describe('providers', () => {
10951095

10961096
const environmentInjector = createEnvironmentInjector(
10971097
[{provide: String, useValue: 'From module injector'}],
1098-
TestBed.get(EnvironmentInjector),
1098+
TestBed.inject(EnvironmentInjector),
10991099
);
11001100

11011101
hostComponent!.vcref.createComponent(EmbeddedComponent, {

packages/core/testing/src/test_bed.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ export interface TestBed {
115115
): T | null;
116116
inject<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T;
117117

118-
/** @deprecated from v9.0.0 use TestBed.inject */
119-
get(token: any, notFoundValue?: any): any;
120-
121118
/**
122119
* Runs the given function in the `EnvironmentInjector` context of `TestBed`.
123120
*
@@ -364,17 +361,6 @@ export class TestBedImpl implements TestBed {
364361
return TestBedImpl.INSTANCE.inject(token, notFoundValue, options);
365362
}
366363

367-
/** @deprecated from v9.0.0 use TestBed.inject */
368-
static get(token: any, notFoundValue?: any): any;
369-
/** @deprecated from v9.0.0 use TestBed.inject */
370-
static get(
371-
token: any,
372-
notFoundValue: any = Injector.THROW_IF_NOT_FOUND,
373-
options?: InjectOptions,
374-
): any {
375-
return TestBedImpl.INSTANCE.inject(token, notFoundValue, options);
376-
}
377-
378364
/**
379365
* Runs the given function in the `EnvironmentInjector` context of `TestBed`.
380366
*
@@ -575,13 +561,6 @@ export class TestBedImpl implements TestBed {
575561
: result;
576562
}
577563

578-
/** @deprecated from v9.0.0 use TestBed.inject */
579-
get(token: any, notFoundValue?: any): any;
580-
/** @deprecated from v9.0.0 use TestBed.inject */
581-
get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND, options?: InjectOptions): any {
582-
return this.inject(token, notFoundValue, options);
583-
}
584-
585564
runInInjectionContext<T>(fn: () => T): T {
586565
return runInInjectionContext(this.inject(EnvironmentInjector), fn);
587566
}

packages/misc/angular-in-memory-web-api/test/http-client-backend-service_spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('HttpClient Backend Service', () => {
4949
],
5050
});
5151

52-
http = TestBed.get(HttpClient);
52+
http = TestBed.inject(HttpClient);
5353
});
5454

5555
it('can get heroes', waitForAsync(() => {
@@ -62,7 +62,7 @@ describe('HttpClient Backend Service', () => {
6262
}));
6363

6464
it('GET should be a "cold" observable', waitForAsync(() => {
65-
const httpBackend = TestBed.get(HttpBackend);
65+
const httpBackend = TestBed.inject<any>(HttpBackend);
6666
const spy = spyOn(httpBackend, 'collectionHandler').and.callThrough();
6767
const get$ = http.get<Hero[]>('api/heroes');
6868

@@ -88,7 +88,7 @@ describe('HttpClient Backend Service', () => {
8888
}));
8989

9090
it('Should only initialize the db once', waitForAsync(() => {
91-
const httpBackend = TestBed.get(HttpBackend);
91+
const httpBackend = TestBed.inject<any>(HttpBackend);
9292
const spy = spyOn(httpBackend, 'resetDb').and.callThrough();
9393

9494
// Simultaneous backend.handler calls
@@ -252,7 +252,7 @@ describe('HttpClient Backend Service', () => {
252252
],
253253
});
254254

255-
http = TestBed.get(HttpClient);
255+
http = TestBed.inject(HttpClient);
256256
});
257257

258258
it('can get heroes', waitForAsync(() => {
@@ -377,7 +377,7 @@ describe('HttpClient Backend Service', () => {
377377
let heroService: HeroService;
378378

379379
beforeEach(() => {
380-
heroService = TestBed.get(HeroService);
380+
heroService = TestBed.inject(HeroService);
381381
});
382382

383383
it('can get heroes', waitForAsync(() => {
@@ -499,9 +499,9 @@ describe('HttpClient Backend Service', () => {
499499
],
500500
});
501501

502-
http = TestBed.get(HttpClient);
503-
httpBackend = TestBed.get(HttpBackend);
504-
interceptors = TestBed.get(HTTP_INTERCEPTORS);
502+
http = TestBed.inject(HttpClient);
503+
httpBackend = TestBed.inject<any>(HttpBackend);
504+
interceptors = TestBed.inject<any>(HTTP_INTERCEPTORS);
505505
});
506506

507507
// sanity test
@@ -560,8 +560,8 @@ describe('HttpClient Backend Service', () => {
560560
],
561561
});
562562

563-
http = TestBed.get(HttpClient);
564-
httpBackend = TestBed.get(HttpBackend);
563+
http = TestBed.inject(HttpClient);
564+
httpBackend = TestBed.inject<any>(HttpBackend);
565565
createPassThruBackend = spyOn(<any>httpBackend, 'createPassThruBackend').and.callThrough();
566566
});
567567

@@ -623,7 +623,7 @@ describe('HttpClient Backend Service', () => {
623623
],
624624
});
625625

626-
http = TestBed.get(HttpClient);
626+
http = TestBed.inject(HttpClient);
627627
});
628628

629629
it('can get heroes (encapsulated)', waitForAsync(() => {

0 commit comments

Comments
 (0)