Skip to content

Commit

Permalink
fixup! fix(core): Errors during ApplicationRef.tick should be rethrow…
Browse files Browse the repository at this point in the history
…n in tests with ZoneJS
  • Loading branch information
atscott committed Jul 31, 2024
1 parent d37f8b0 commit 57123fe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/core/testing/src/application_error_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {ErrorHandler, inject, NgZone, Injectable, InjectionToken} from '@angular/core';

export const RETHROW_APPLICATION_ERRORS = new InjectionToken<boolean>('rethrow application errors');
export const RETHROW_APPLICATION_ERRORS_DEFAULT = true;

@Injectable()
export class TestBedApplicationErrorHandler {
Expand Down
44 changes: 19 additions & 25 deletions packages/core/testing/src/test_bed_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ import {
} from './resolvers';
import {DEFER_BLOCK_DEFAULT_BEHAVIOR, TestModuleMetadata} from './test_bed_common';
import {
RETHROW_APPLICATION_ERRORS,
RETHROW_APPLICATION_ERRORS_DEFAULT,
TestBedApplicationErrorHandler,
} from './application_error_handler';

Expand Down Expand Up @@ -190,6 +190,7 @@ export class TestBedCompiler {
private testModuleRef: NgModuleRef<any> | null = null;

private deferBlockBehavior = DEFER_BLOCK_DEFAULT_BEHAVIOR;
private rethrowApplicationTickErrors = RETHROW_APPLICATION_ERRORS_DEFAULT;

constructor(
private platform: PlatformRef,
Expand Down Expand Up @@ -226,18 +227,14 @@ export class TestBedCompiler {
if (moduleDef.providers !== undefined) {
this.providers.push(...moduleDef.providers);
}
if (moduleDef._rethrowApplicationTickErrors !== undefined) {
this.providers.push({
provide: RETHROW_APPLICATION_ERRORS,
useValue: moduleDef._rethrowApplicationTickErrors,
});
}

if (moduleDef.schemas !== undefined) {
this.schemas.push(...moduleDef.schemas);
}

this.deferBlockBehavior = moduleDef.deferBlockBehavior ?? DEFER_BLOCK_DEFAULT_BEHAVIOR;
this.rethrowApplicationTickErrors =
moduleDef._rethrowApplicationTickErrors ?? RETHROW_APPLICATION_ERRORS_DEFAULT;
}

overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void {
Expand Down Expand Up @@ -946,31 +943,28 @@ export class TestBedCompiler {
...this.rootProviderOverrides,
internalProvideZoneChangeDetection({}),
TestBedApplicationErrorHandler,
{
provide: INTERNAL_APPLICATION_ERROR_HANDLER,
useFactory: () => {
const rethrowApplicationErrorsDisabled =
inject(RETHROW_APPLICATION_ERRORS, {optional: true}) === false;
if (!rethrowApplicationErrorsDisabled) {
const handler = inject(TestBedApplicationErrorHandler);
return (e: unknown) => {
handler.handleError(e);
};
} else {
const userErrorHandler = inject(ErrorHandler);
const ngZone = inject(NgZone);
return (e: unknown) =>
ngZone.runOutsideAngular(() => userErrorHandler.handleError(e));
}
},
},
{provide: ChangeDetectionScheduler, useExisting: ChangeDetectionSchedulerImpl},
],
});

const providers = [
{provide: Compiler, useFactory: () => new R3TestCompiler(this)},
{provide: DEFER_BLOCK_CONFIG, useValue: {behavior: this.deferBlockBehavior}},
{
provide: INTERNAL_APPLICATION_ERROR_HANDLER,
useFactory: () => {
if (this.rethrowApplicationTickErrors) {
const handler = inject(TestBedApplicationErrorHandler);
return (e: unknown) => {
handler.handleError(e);
};
} else {
const userErrorHandler = inject(ErrorHandler);
const ngZone = inject(NgZone);
return (e: unknown) => ngZone.runOutsideAngular(() => userErrorHandler.handleError(e));
}
},
},
...this.providers,
...this.providerOverrides,
];
Expand Down

0 comments on commit 57123fe

Please sign in to comment.