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
This commit is contained in:
Kristiyan Kostadinov
2021-09-29 17:30:24 +02:00
committed by Dylan Hunn
parent bdc6ff465a
commit 7fd0428aae
3 changed files with 33 additions and 6 deletions
+25
View File
@@ -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<string>('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);
});
});
+4 -3
View File
@@ -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 {
+4 -3
View File
@@ -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 {