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): log errors instead of re-throwing them #29853

Closed
wants to merge 1 commit into from
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
45 changes: 45 additions & 0 deletions packages/core/test/test_bed_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,51 @@ describe('TestBed', () => {
TestBed.configureTestingModule({imports: [ProvidesErrorHandler, HelloWorldModule]});

expect(TestBed.get(ErrorHandler)).toEqual(jasmine.any(CustomErrorHandler));

});

it('should throw errors in CD', () => {
@Component({selector: 'my-comp', template: ''})
class MyComp {
name !: {hello: string};

ngOnInit() {
// this should throw because this.name is undefined
this.name.hello = 'hello';
}
}

TestBed.configureTestingModule({declarations: [MyComp]});

expect(() => {
const fixture = TestBed.createComponent(MyComp);
fixture.detectChanges();
}).toThrowError();
});

// TODO(FW-1245): properly fix issue where errors in listeners aren't thrown and don't cause
// tests to fail. This is an issue in both View Engine and Ivy, and may require a breaking
// change to completely fix (since simple re-throwing breaks handlers in ngrx, etc).
xit('should throw errors in listeners', () => {

@Component({selector: 'my-comp', template: '<button (click)="onClick()">Click me</button>'})
class MyComp {
name !: {hello: string};

onClick() {
// this should throw because this.name is undefined
this.name.hello = 'hello';
}
}

TestBed.configureTestingModule({declarations: [MyComp]});
const fixture = TestBed.createComponent(MyComp);
fixture.detectChanges();

expect(() => {
const button = fixture.nativeElement.querySelector('button');
button.click();
}).toThrowError();
});

onlyInIvy('TestBed should handle AOT pre-compiled Components')
Expand Down
14 changes: 2 additions & 12 deletions packages/core/testing/src/r3_test_bed_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ApplicationInitStatus, COMPILER_OPTIONS, Compiler, Component, Directive, ErrorHandler, ModuleWithComponentFactories, NgModule, NgModuleFactory, NgZone, Injector, Pipe, PlatformRef, Provider, Type, ɵcompileComponent as compileComponent, ɵcompileDirective as compileDirective, ɵcompileNgModuleDefs as compileNgModuleDefs, ɵcompilePipe as compilePipe, ɵgetInjectableDef as getInjectableDef, ɵNG_COMPONENT_DEF as NG_COMPONENT_DEF, ɵNG_DIRECTIVE_DEF as NG_DIRECTIVE_DEF, ɵNG_INJECTOR_DEF as NG_INJECTOR_DEF, ɵNG_MODULE_DEF as NG_MODULE_DEF, ɵNG_PIPE_DEF as NG_PIPE_DEF, ɵRender3ComponentFactory as ComponentFactory, ɵRender3NgModuleRef as NgModuleRef, ɵɵInjectableDef as InjectableDef, ɵNgModuleFactory as R3NgModuleFactory, ɵNgModuleTransitiveScopes as NgModuleTransitiveScopes, ɵNgModuleType as NgModuleType, ɵDirectiveDef as DirectiveDef, ɵpatchComponentDefWithScope as patchComponentDefWithScope, ɵtransitiveScopesFor as transitiveScopesFor,} from '@angular/core';
import {ApplicationInitStatus, COMPILER_OPTIONS, Compiler, Component, Directive, ModuleWithComponentFactories, NgModule, NgModuleFactory, NgZone, Injector, Pipe, PlatformRef, Provider, Type, ɵcompileComponent as compileComponent, ɵcompileDirective as compileDirective, ɵcompileNgModuleDefs as compileNgModuleDefs, ɵcompilePipe as compilePipe, ɵgetInjectableDef as getInjectableDef, ɵNG_COMPONENT_DEF as NG_COMPONENT_DEF, ɵNG_DIRECTIVE_DEF as NG_DIRECTIVE_DEF, ɵNG_INJECTOR_DEF as NG_INJECTOR_DEF, ɵNG_MODULE_DEF as NG_MODULE_DEF, ɵNG_PIPE_DEF as NG_PIPE_DEF, ɵRender3ComponentFactory as ComponentFactory, ɵRender3NgModuleRef as NgModuleRef, ɵɵInjectableDef as InjectableDef, ɵNgModuleFactory as R3NgModuleFactory, ɵNgModuleTransitiveScopes as NgModuleTransitiveScopes, ɵNgModuleType as NgModuleType, ɵDirectiveDef as DirectiveDef, ɵpatchComponentDefWithScope as patchComponentDefWithScope, ɵtransitiveScopesFor as transitiveScopesFor,} from '@angular/core';
import {ResourceLoader} from '@angular/compiler';

import {clearResolutionOfComponentResourcesQueue, restoreComponentResolutionQueue, resolveComponentResources, isComponentDefPendingResolution} from '../../src/metadata/resource_loading';
Expand Down Expand Up @@ -513,19 +513,14 @@ export class R3TestBedCompiler {
class RootScopeModule {
}

@NgModule({providers: [{provide: ErrorHandler, useClass: R3TestErrorHandler}]})
class R3ErrorHandlerModule {
}

const ngZone = new NgZone({enableLongStackTrace: true});
const providers: Provider[] = [
{provide: NgZone, useValue: ngZone},
{provide: Compiler, useFactory: () => new R3TestCompiler(this)},
...this.providers,
...this.providerOverrides,
];
const imports =
[RootScopeModule, this.additionalModuleTypes, R3ErrorHandlerModule, this.imports || []];
const imports = [RootScopeModule, this.additionalModuleTypes, this.imports || []];

// clang-format off
compileNgModuleDefs(this.testModuleType, {
Expand Down Expand Up @@ -633,11 +628,6 @@ function flatten<T>(values: any[], mapFn?: (value: T) => any): T[] {
return out;
}

/** Error handler used for tests. Rethrows errors rather than logging them out. */
class R3TestErrorHandler extends ErrorHandler {
handleError(error: any) { throw error; }
}

class R3TestCompiler implements Compiler {
constructor(private testBed: R3TestBedCompiler) {}

Expand Down