Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ivy): TestBed overriding custom ErrorHandler #29482

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/core/test/test_bed_spec.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/core/testing/src/r3_test_bed.ts
Expand Up @@ -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})
Expand Down