From 21245887e6482218b39b42af2cdccde2fe77b96a Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Fri, 6 Sep 2019 09:26:17 -0700 Subject: [PATCH] fix(ivy): unable to override ComponentFactoryResolver provider in tests (#32512) PR Close #32512 --- packages/core/src/di/r3_injector.ts | 7 ++++--- .../platform-browser/test/testing_public_spec.ts | 12 +++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/core/src/di/r3_injector.ts b/packages/core/src/di/r3_injector.ts index c92f3b739a..df5be04609 100644 --- a/packages/core/src/di/r3_injector.ts +++ b/packages/core/src/di/r3_injector.ts @@ -116,15 +116,16 @@ export class R3Injector { constructor( def: InjectorType, additionalProviders: StaticProvider[]|null, readonly parent: Injector, source: string|null = null) { - // Start off by creating Records for every provider declared in every InjectorType - // included transitively in `def`. const dedupStack: InjectorType[] = []; - deepForEach([def], injectorDef => this.processInjectorType(injectorDef, [], dedupStack)); + // Start off by creating Records for every provider declared in every InjectorType + // included transitively in additional providers then do the same for `def`. This order is + // important because `def` may include providers that override ones in additionalProviders. additionalProviders && deepForEach( additionalProviders, provider => this.processProvider( provider, def, additionalProviders)); + deepForEach([def], injectorDef => this.processInjectorType(injectorDef, [], dedupStack)); // Make sure the INJECTOR token provides this injector. this.records.set(INJECTOR, makeRecord(undefined, this)); diff --git a/packages/platform-browser/test/testing_public_spec.ts b/packages/platform-browser/test/testing_public_spec.ts index f9b7ed6fe8..ef0765a8f5 100644 --- a/packages/platform-browser/test/testing_public_spec.ts +++ b/packages/platform-browser/test/testing_public_spec.ts @@ -7,7 +7,7 @@ */ import {CompilerConfig, ResourceLoader} from '@angular/compiler'; -import {CUSTOM_ELEMENTS_SCHEMA, Compiler, Component, Directive, Inject, Injectable, InjectionToken, Injector, Input, NgModule, Optional, Pipe, SkipSelf, ɵstringify as stringify} from '@angular/core'; +import {CUSTOM_ELEMENTS_SCHEMA, Compiler, Component, ComponentFactoryResolver, Directive, Inject, Injectable, InjectionToken, Injector, Input, NgModule, Optional, Pipe, SkipSelf, ɵstringify as stringify} from '@angular/core'; import {TestBed, async, fakeAsync, getTestBed, inject, tick, withModule} from '@angular/core/testing'; import {expect} from '@angular/platform-browser/testing/src/matchers'; import {ivyEnabled, modifiedInIvy, obsoleteInIvy, onlyInIvy} from '@angular/private/testing'; @@ -418,6 +418,16 @@ const bTok = new InjectionToken('b'); }); describe('overriding providers', () => { + + describe('in core', () => { + it('ComponentFactoryResolver', () => { + const componentFactoryMock = + jasmine.createSpyObj('componentFactory', ['resolveComponentFactory']); + TestBed.overrideProvider(ComponentFactoryResolver, {useValue: componentFactoryMock}); + expect(TestBed.get(ComponentFactoryResolver)).toEqual(componentFactoryMock); + }); + }); + describe('in NgModules', () => { it('should support useValue', () => {