From 7fd0428aae3a7d94bfc8fee764ac24f5fe3fbb41 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Wed, 29 Sep 2021 17:30:24 +0200 Subject: [PATCH] fix(core): don't rethrow errors if test teardown has been disabled (#43635) Fixes that the current logic was set up so that error rethrowing was enabled even when teardown is disabled. PR Close #43635 --- packages/core/test/test_bed_spec.ts | 25 ++++++++++++++++++++++++ packages/core/testing/src/r3_test_bed.ts | 7 ++++--- packages/core/testing/src/test_bed.ts | 7 ++++--- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/core/test/test_bed_spec.ts b/packages/core/test/test_bed_spec.ts index d531a24b85c..49c72c226cb 100644 --- a/packages/core/test/test_bed_spec.ts +++ b/packages/core/test/test_bed_spec.ts @@ -12,6 +12,7 @@ import {By} from '@angular/platform-browser'; import {expect} from '@angular/platform-browser/testing/src/matchers'; import {onlyInIvy} from '@angular/private/testing'; import {TestBedRender3} from '../testing/src/r3_test_bed'; +import {TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT} from '../testing/src/test_bed_common'; const NAME = new InjectionToken('name'); @@ -1528,4 +1529,28 @@ describe('TestBed module teardown', () => { TestBed.resetTestingModule(); expect(fixtureDocument.body.contains(fixture.nativeElement)).toBe(false); }); + + it('should rethrow errors based on the default teardown behavior', () => { + expect(TestBed.shouldRethrowTeardownErrors()).toBe(TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT); + }); + + it('should rethrow errors if the option is omitted and test teardown is enabled', () => { + TestBed.configureTestingModule({teardown: {destroyAfterEach: true}}); + expect(TestBed.shouldRethrowTeardownErrors()).toBe(true); + }); + + it('should not rethrow errors if the option is omitted and test teardown is disabled', () => { + TestBed.configureTestingModule({teardown: {destroyAfterEach: false}}); + expect(TestBed.shouldRethrowTeardownErrors()).toBe(false); + }); + + it('should rethrow errors if the option is enabled, but teardown is disabled', () => { + TestBed.configureTestingModule({teardown: {destroyAfterEach: false, rethrowErrors: true}}); + expect(TestBed.shouldRethrowTeardownErrors()).toBe(true); + }); + + it('should not rethrow errors if the option is disabled, but teardown is enabled', () => { + TestBed.configureTestingModule({teardown: {destroyAfterEach: true, rethrowErrors: false}}); + expect(TestBed.shouldRethrowTeardownErrors()).toBe(false); + }); }); diff --git a/packages/core/testing/src/r3_test_bed.ts b/packages/core/testing/src/r3_test_bed.ts index ec469c2b1fd..25347e39be7 100644 --- a/packages/core/testing/src/r3_test_bed.ts +++ b/packages/core/testing/src/r3_test_bed.ts @@ -469,17 +469,18 @@ export class TestBedRender3 implements TestBed { } } - private shouldRethrowTeardownErrors() { + shouldRethrowTeardownErrors() { const instanceOptions = this._instanceTeardownOptions; const environmentOptions = TestBedRender3._environmentTeardownOptions; // If the new teardown behavior hasn't been configured, preserve the old behavior. if (!instanceOptions && !environmentOptions) { - return false; + return TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT; } // Otherwise use the configured behavior or default to rethrowing. - return instanceOptions?.rethrowErrors ?? environmentOptions?.rethrowErrors ?? true; + return instanceOptions?.rethrowErrors ?? environmentOptions?.rethrowErrors ?? + this.shouldTearDownTestingModule(); } shouldTearDownTestingModule(): boolean { diff --git a/packages/core/testing/src/test_bed.ts b/packages/core/testing/src/test_bed.ts index af228342b27..06d45690aca 100644 --- a/packages/core/testing/src/test_bed.ts +++ b/packages/core/testing/src/test_bed.ts @@ -694,17 +694,18 @@ export class TestBedViewEngine implements TestBed { } } - private shouldRethrowTeardownErrors() { + shouldRethrowTeardownErrors() { const instanceOptions = this._instanceTeardownOptions; const environmentOptions = TestBedViewEngine._environmentTeardownOptions; // If the new teardown behavior hasn't been configured, preserve the old behavior. if (!instanceOptions && !environmentOptions) { - return false; + return TEARDOWN_TESTING_MODULE_ON_DESTROY_DEFAULT; } // Otherwise use the configured behavior or default to rethrowing. - return instanceOptions?.rethrowErrors ?? environmentOptions?.rethrowErrors ?? true; + return instanceOptions?.rethrowErrors ?? environmentOptions?.rethrowErrors ?? + this.shouldTearDownTestingModule(); } shouldTearDownTestingModule(): boolean {