fix(ivy): TestBed overriding custom ErrorHandler (#29482)

Fixes TestBed's default ErrorHandler overriding the one provided by the consumer via an `import`.

This PR resolves FW-1193.

PR Close #29482
This commit is contained in:
Kristiyan Kostadinov 2019-03-22 22:18:09 +01:00 committed by Miško Hevery
parent b5295ad277
commit bef5043a5a
2 changed files with 21 additions and 3 deletions

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {Component, Directive, Inject, InjectionToken, NgModule, Optional, Pipe} from '@angular/core'; import {Component, Directive, ErrorHandler, Inject, InjectionToken, NgModule, Optional, Pipe} from '@angular/core';
import {TestBed, getTestBed} from '@angular/core/testing/src/test_bed'; import {TestBed, getTestBed} from '@angular/core/testing/src/test_bed';
import {By} from '@angular/platform-browser'; import {By} from '@angular/platform-browser';
import {expect} from '@angular/platform-browser/testing/src/matchers'; import {expect} from '@angular/platform-browser/testing/src/matchers';
@ -256,6 +256,19 @@ describe('TestBed', () => {
TestBed.createComponent(ComponentWithInlineTemplate); TestBed.createComponent(ComponentWithInlineTemplate);
}); });
it('should be able to override the ErrorHandler via an import', () => {
class CustomErrorHandler {}
@NgModule({providers: [{provide: ErrorHandler, useClass: CustomErrorHandler}]})
class ProvidesErrorHandler {
}
getTestBed().resetTestingModule();
TestBed.configureTestingModule({imports: [ProvidesErrorHandler, HelloWorldModule]});
expect(TestBed.get(ErrorHandler)).toEqual(jasmine.any(CustomErrorHandler));
});
onlyInIvy('patched ng defs should be removed after resetting TestingModule') onlyInIvy('patched ng defs should be removed after resetting TestingModule')
.describe('resetting ng defs', () => { .describe('resetting ng defs', () => {
it('should restore ng defs to their initial states', () => { it('should restore ng defs to their initial states', () => {

View File

@ -630,17 +630,22 @@ export class TestBedRender3 implements Injector, TestBed {
class RootScopeModule { class RootScopeModule {
} }
@NgModule({providers: [{provide: ErrorHandler, useClass: R3TestErrorHandler}]})
class R3ErrorHandlerModule {
}
const ngZone = new NgZone({enableLongStackTrace: true}); const ngZone = new NgZone({enableLongStackTrace: true});
const providers = [ const providers = [
{provide: NgZone, useValue: ngZone}, {provide: NgZone, useValue: ngZone},
{provide: Compiler, useFactory: () => new R3TestCompiler(this)}, {provide: Compiler, useFactory: () => new R3TestCompiler(this)},
{provide: ErrorHandler, useClass: R3TestErrorHandler},
...this._providers, ...this._providers,
...this._providerOverrides, ...this._providerOverrides,
]; ];
// We need to provide the `R3ErrorHandlerModule` after the consumer's NgModule so that we can
// override the default ErrorHandler, if the consumer didn't pass in a custom one.
const imports = [RootScopeModule, this.ngModule, R3ErrorHandlerModule, this._imports];
const declarations = this._declarations; const declarations = this._declarations;
const imports = [RootScopeModule, this.ngModule, this._imports];
const schemas = this._schemas; const schemas = this._schemas;
@NgModule({providers, declarations, imports, schemas, jit: true}) @NgModule({providers, declarations, imports, schemas, jit: true})