Skip to content

Commit 5a46f94

Browse files
karajasonaden
authored andcommittedMay 21, 2019
fix(core): remove deprecated TestBed.deprecatedOverrideProvider API (#30576)
BREAKING CHANGE In PR #19558, we fixed a bug in `TestBed.overrideProvider` where eager providers were not being instantiated correctly. However, it turned out that since this bug had been around for quite a bit, many apps were relying on the broken behavior where the providers would not be instantiated. To assist in the transition, the `TestBed.deprecatedOverrideProvider` method was temporarily introduced to mimic the old behavior so that apps would have a longer time period to migrate their code. 2 years and 3 versions later, it is time to remove the temporary method. This commit removes `TestBed.deprecatedOverrideProvider` altogether. Any usages of `TestBed.deprecatedOverrideProvider` should be replaced with `TestBed.overrideProvider`. This may mean that providers that were not created before will now be instantiated, which could mean that your tests need to provide more mocks or stubs for the dependencies of the newly instantiated providers. PR Close #30576
1 parent 5716605 commit 5a46f94

File tree

6 files changed

+12
-169
lines changed

6 files changed

+12
-169
lines changed
 

‎aio/content/guide/deprecations.md

+2-7
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,6 @@ Tip: In the [API reference section](api) of this doc site, deprecated APIs are i
6060
| [`RootRenderer`](api/core/RootRenderer) | [`RendererFactory2`](api/core/RendererFactory2) | v4 | none |
6161
| [`ViewEncapsulation.Native`](api/core/ViewEncapsulation#Native) | [`ViewEncapsulation.ShadowDom`](api/core/ViewEncapsulation#ShadowDom) | v6 | Use the native encapsulation mechanism of the renderer. See [view.ts](https://github.com/angular/angular/blob/3e992e18ebf51d6036818f26c3d77b52d3ec48eb/packages/core/src/metadata/view.ts#L32).
6262

63-
#### @angular/core/testing
64-
65-
| API | Replacement | Deprecation announced | Notes |
66-
| --- | ----------- | --------------------- | ----- |
67-
| [`TestBed.deprecatedOverrideProvider()`](api/core/testing/TestBed#deprecatedoverrideprovider) | [`TestBed.overrideProvider()`] (api/core/testing/TestBed#overrideprovider) | v7 | none |
68-
| [`TestBedStatic.deprecatedOverrideProvider()`](api/core/testing/TestBedStatic#deprecatedoverrideprovider) | [`TestBedStatic.overrideProvider()`](api/core/testing/TestBedStatic#overrideprovider) | v5 | none |
69-
7063

7164
#### @angular/forms
7265

@@ -253,6 +246,8 @@ The following APIs have been removed starting with version 8.0.0:
253246
| [`@angular/http`](https://v7.angular.io/api/http) | All exports | [`@angular/common/http`](https://v7.angular.io/api/common/http) | See [below](#http). |
254247
[`@angular/http/testing`](https://v7.angular.io/api/http/testing) | All exports | [`@angular/common/http/testing`](https://v7.angular.io/api/common/http/testing) | See [below](#http). |
255248
| `@angular/platform-browser` | [`DOCUMENT`](https://v7.angular.io/api/platform-browser/DOCUMENT) | [`DOCUMENT` in `@angular/common`](https://v7.angular.io/api/common/DOCUMENT) | Updating to version 8 with [`ng update`](cli/update) changes this automatically. |
249+
| `@angular/core/testing` | [`TestBed.deprecatedOverrideProvider()`](https://v7.angular.io/api/core/testing/TestBed#deprecatedoverrideprovider) | [`TestBed.overrideProvider()`] (api/core/testing/TestBed#overrideprovider) | none |
250+
| `@angular/core/testing` | [`TestBedStatic.deprecatedOverrideProvider()`](https://v7.angular.io/api/core/testing/TestBedStatic#deprecatedoverrideprovider) | [`TestBedStatic.overrideProvider()`](api/core/testing/TestBedStatic#overrideprovider) | none |
256251

257252

258253

‎packages/core/testing/src/r3_test_bed.ts

-43
Original file line numberDiff line numberDiff line change
@@ -150,25 +150,6 @@ export class TestBedRender3 implements Injector, TestBed {
150150
return TestBedRender3 as any as TestBedStatic;
151151
}
152152

153-
/**
154-
* Overwrites all providers for the given token with the given provider definition.
155-
*
156-
* @deprecated as it makes all NgModules lazy. Introduced only for migrating off of it.
157-
*/
158-
static deprecatedOverrideProvider(token: any, provider: {
159-
useFactory: Function,
160-
deps: any[],
161-
}): void;
162-
static deprecatedOverrideProvider(token: any, provider: {useValue: any;}): void;
163-
static deprecatedOverrideProvider(token: any, provider: {
164-
useFactory?: Function,
165-
useValue?: any,
166-
deps?: any[],
167-
}): TestBedStatic {
168-
_getTestBedRender3().deprecatedOverrideProvider(token, provider as any);
169-
return TestBedRender3 as any as TestBedStatic;
170-
}
171-
172153
static get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
173154
/**
174155
* @deprecated from v8.0.0 use Type<T> or InjectionToken<T>
@@ -317,30 +298,6 @@ export class TestBedRender3 implements Injector, TestBed {
317298
this.compiler.overrideProvider(token, provider);
318299
}
319300

320-
/**
321-
* Overwrites all providers for the given token with the given provider definition.
322-
*
323-
* @deprecated as it makes all NgModules lazy. Introduced only for migrating off of it.
324-
*/
325-
deprecatedOverrideProvider(token: any, provider: {
326-
useFactory: Function,
327-
deps: any[],
328-
}): void;
329-
deprecatedOverrideProvider(token: any, provider: {useValue: any;}): void;
330-
deprecatedOverrideProvider(
331-
token: any, provider: {useFactory?: Function, useValue?: any, deps?: any[]}): void {
332-
// HACK: This is NOT the correct implementation for deprecatedOverrideProvider.
333-
// To implement it in a backward compatible way, we would need to record some state
334-
// so we know to prevent eager instantiation of NgModules. However, we don't plan
335-
// to implement this at all since the API is deprecated and scheduled for removal
336-
// in V8. This hack is here temporarily for Ivy testing until we transition apps
337-
// inside Google to the overrideProvider API. At that point, we will be able to
338-
// remove this method entirely. In the meantime, we can use overrideProvider to
339-
// test apps with Ivy that don't care about eager instantiation. This fixes 85%
340-
// of cases in our blueprint.
341-
this.overrideProvider(token, provider as any);
342-
}
343-
344301
createComponent<T>(type: Type<T>): ComponentFixture<T> {
345302
const testComponentRenderer: TestComponentRenderer = this.get(TestComponentRenderer);
346303
const rootElId = `root-ng-internal-isolated-${_nextRootElementId++}`;

‎packages/core/testing/src/test_bed.ts

-48
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,6 @@ export interface TestBed {
8383
overrideProvider(token: any, provider: {useFactory?: Function, useValue?: any, deps?: any[]}):
8484
void;
8585

86-
/**
87-
* Overwrites all providers for the given token with the given provider definition.
88-
*
89-
* @deprecated as it makes all NgModules lazy. Introduced only for migrating off of it.
90-
*/
91-
deprecatedOverrideProvider(token: any, provider: {
92-
useFactory: Function,
93-
deps: any[],
94-
}): void;
95-
deprecatedOverrideProvider(token: any, provider: {useValue: any;}): void;
96-
deprecatedOverrideProvider(
97-
token: any, provider: {useFactory?: Function, useValue?: any, deps?: any[]}): void;
98-
99-
10086
overrideTemplateUsingTestingModule(component: Type<any>, template: string): void;
10187

10288
createComponent<T>(component: Type<T>): ComponentFixture<T>;
@@ -224,25 +210,6 @@ export class TestBedViewEngine implements Injector, TestBed {
224210
return TestBedViewEngine as any as TestBedStatic;
225211
}
226212

227-
/**
228-
* Overwrites all providers for the given token with the given provider definition.
229-
*
230-
* @deprecated as it makes all NgModules lazy. Introduced only for migrating off of it.
231-
*/
232-
static deprecatedOverrideProvider(token: any, provider: {
233-
useFactory: Function,
234-
deps: any[],
235-
}): void;
236-
static deprecatedOverrideProvider(token: any, provider: {useValue: any;}): void;
237-
static deprecatedOverrideProvider(token: any, provider: {
238-
useFactory?: Function,
239-
useValue?: any,
240-
deps?: any[],
241-
}): TestBedStatic {
242-
_getTestBedViewEngine().deprecatedOverrideProvider(token, provider as any);
243-
return TestBedViewEngine as any as TestBedStatic;
244-
}
245-
246213
static get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
247214
/**
248215
* @deprecated from v8.0.0 use Type<T> or InjectionToken<T>
@@ -537,21 +504,6 @@ export class TestBedViewEngine implements Injector, TestBed {
537504
this.overrideProviderImpl(token, provider);
538505
}
539506

540-
/**
541-
* Overwrites all providers for the given token with the given provider definition.
542-
*
543-
* @deprecated as it makes all NgModules lazy. Introduced only for migrating off of it.
544-
*/
545-
deprecatedOverrideProvider(token: any, provider: {
546-
useFactory: Function,
547-
deps: any[],
548-
}): void;
549-
deprecatedOverrideProvider(token: any, provider: {useValue: any;}): void;
550-
deprecatedOverrideProvider(
551-
token: any, provider: {useFactory?: Function, useValue?: any, deps?: any[]}): void {
552-
this.overrideProviderImpl(token, provider, /* deprecated */ true);
553-
}
554-
555507
private overrideProviderImpl(
556508
token: any, provider: {
557509
useFactory?: Function,

‎packages/core/testing/src/test_bed_common.ts

-16
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,6 @@ export interface TestBedStatic {
114114
deps?: any[],
115115
}): TestBedStatic;
116116

117-
/**
118-
* Overwrites all providers for the given token with the given provider definition.
119-
*
120-
* @deprecated as it makes all NgModules lazy. Introduced only for migrating off of it.
121-
*/
122-
deprecatedOverrideProvider(token: any, provider: {
123-
useFactory: Function,
124-
deps: any[],
125-
}): void;
126-
deprecatedOverrideProvider(token: any, provider: {useValue: any;}): void;
127-
deprecatedOverrideProvider(token: any, provider: {
128-
useFactory?: Function,
129-
useValue?: any,
130-
deps?: any[],
131-
}): TestBedStatic;
132-
133117
get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
134118
/**
135119
* @deprecated from v8.0.0 use Type<T> or InjectionToken<T>

‎packages/platform-browser/test/testing_public_spec.ts

-21
Original file line numberDiff line numberDiff line change
@@ -494,27 +494,6 @@ class CompWithUrlTemplate {
494494
expect(someModule).toBeAnInstanceOf(SomeModule);
495495
});
496496

497-
obsoleteInIvy(`deprecated method, won't be reimplemented for Render3`)
498-
.it('should keep imported NgModules lazy with deprecatedOverrideProvider', () => {
499-
let someModule: SomeModule|undefined;
500-
501-
@NgModule()
502-
class SomeModule {
503-
constructor() { someModule = this; }
504-
}
505-
506-
TestBed.configureTestingModule({
507-
providers: [
508-
{provide: 'a', useValue: 'aValue'},
509-
],
510-
imports: [SomeModule]
511-
});
512-
TestBed.deprecatedOverrideProvider('a', {useValue: 'mockValue'});
513-
514-
expect(TestBed.get('a')).toBe('mockValue');
515-
expect(someModule).toBeUndefined();
516-
});
517-
518497
describe('injecting eager providers into an eager overwritten provider', () => {
519498
@NgModule({
520499
providers: [

‎tools/public_api_guard/core/testing.d.ts

+10-34
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,6 @@ export interface TestBed {
5757
}): void;
5858
configureTestingModule(moduleDef: TestModuleMetadata): void;
5959
createComponent<T>(component: Type<T>): ComponentFixture<T>;
60-
deprecatedOverrideProvider(token: any, provider: {
61-
useFactory?: Function;
62-
useValue?: any;
63-
deps?: any[];
64-
}): void;
65-
deprecatedOverrideProvider(token: any, provider: {
66-
useValue: any;
67-
}): void;
68-
/** @deprecated */ deprecatedOverrideProvider(token: any, provider: {
69-
useFactory: Function;
70-
deps: any[];
71-
}): void;
7260
execute(tokens: any[], fn: Function, context?: any): any;
7361
get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
7462
/** @deprecated */ get(token: any, notFoundValue?: any): any;
@@ -78,16 +66,16 @@ export interface TestBed {
7866
overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void;
7967
overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void;
8068
overrideProvider(token: any, provider: {
81-
useFactory?: Function;
82-
useValue?: any;
83-
deps?: any[];
69+
useFactory: Function;
70+
deps: any[];
8471
}): void;
8572
overrideProvider(token: any, provider: {
8673
useValue: any;
8774
}): void;
8875
overrideProvider(token: any, provider: {
89-
useFactory: Function;
90-
deps: any[];
76+
useFactory?: Function;
77+
useValue?: any;
78+
deps?: any[];
9179
}): void;
9280
overrideTemplateUsingTestingModule(component: Type<any>, template: string): void;
9381
resetTestEnvironment(): void;
@@ -105,25 +93,17 @@ export interface TestBedStatic {
10593
}): TestBedStatic;
10694
configureTestingModule(moduleDef: TestModuleMetadata): TestBedStatic;
10795
createComponent<T>(component: Type<T>): ComponentFixture<T>;
108-
deprecatedOverrideProvider(token: any, provider: {
109-
useFactory?: Function;
110-
useValue?: any;
111-
deps?: any[];
112-
}): TestBedStatic;
113-
deprecatedOverrideProvider(token: any, provider: {
114-
useValue: any;
115-
}): void;
116-
/** @deprecated */ deprecatedOverrideProvider(token: any, provider: {
117-
useFactory: Function;
118-
deps: any[];
119-
}): void;
120-
/** @deprecated */ get(token: any, notFoundValue?: any): any;
12196
get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
97+
/** @deprecated */ get(token: any, notFoundValue?: any): any;
12298
initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): TestBed;
12399
overrideComponent(component: Type<any>, override: MetadataOverride<Component>): TestBedStatic;
124100
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): TestBedStatic;
125101
overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): TestBedStatic;
126102
overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): TestBedStatic;
103+
overrideProvider(token: any, provider: {
104+
useFactory: Function;
105+
deps: any[];
106+
}): TestBedStatic;
127107
overrideProvider(token: any, provider: {
128108
useValue: any;
129109
}): TestBedStatic;
@@ -132,10 +112,6 @@ export interface TestBedStatic {
132112
useValue?: any;
133113
deps?: any[];
134114
}): TestBedStatic;
135-
overrideProvider(token: any, provider: {
136-
useFactory: Function;
137-
deps: any[];
138-
}): TestBedStatic;
139115
overrideTemplate(component: Type<any>, template: string): TestBedStatic;
140116
overrideTemplateUsingTestingModule(component: Type<any>, template: string): TestBedStatic;
141117
resetTestEnvironment(): void;

0 commit comments

Comments
 (0)
Please sign in to comment.