diff --git a/packages/core/test/test_bed_spec.ts b/packages/core/test/test_bed_spec.ts index 51c7ea7905..ccbc5b40c2 100644 --- a/packages/core/test/test_bed_spec.ts +++ b/packages/core/test/test_bed_spec.ts @@ -6,7 +6,7 @@ * 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 {By} from '@angular/platform-browser'; import {expect} from '@angular/platform-browser/testing/src/matchers'; @@ -256,6 +256,19 @@ describe('TestBed', () => { 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') .describe('resetting ng defs', () => { it('should restore ng defs to their initial states', () => { diff --git a/packages/core/testing/src/r3_test_bed.ts b/packages/core/testing/src/r3_test_bed.ts index 4689b0f8cb..740629b87b 100644 --- a/packages/core/testing/src/r3_test_bed.ts +++ b/packages/core/testing/src/r3_test_bed.ts @@ -630,17 +630,22 @@ export class TestBedRender3 implements Injector, TestBed { class RootScopeModule { } + @NgModule({providers: [{provide: ErrorHandler, useClass: R3TestErrorHandler}]}) + class R3ErrorHandlerModule { + } + const ngZone = new NgZone({enableLongStackTrace: true}); const providers = [ {provide: NgZone, useValue: ngZone}, {provide: Compiler, useFactory: () => new R3TestCompiler(this)}, - {provide: ErrorHandler, useClass: R3TestErrorHandler}, ...this._providers, ...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 imports = [RootScopeModule, this.ngModule, this._imports]; const schemas = this._schemas; @NgModule({providers, declarations, imports, schemas, jit: true})