fix(core): add stacktrace in log when error during cleanup component in TestBed (#22162)
PR Close #22162
This commit is contained in:
parent
b75cf3f70b
commit
16d1700a8e
|
@ -9,7 +9,7 @@
|
|||
import {ResourceLoader} from '@angular/compiler';
|
||||
import {CompileMetadataResolver} from '@angular/compiler/src/metadata_resolver';
|
||||
import {MockResourceLoader} from '@angular/compiler/testing/src/resource_loader_mock';
|
||||
import {Component, Directive, Injectable, NgModule, Pipe, Type} from '@angular/core';
|
||||
import {Component, Directive, Injectable, NgModule, OnDestroy, Pipe, Type} from '@angular/core';
|
||||
import {TestBed, async, getTestBed} from '@angular/core/testing';
|
||||
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
||||
|
||||
|
@ -56,6 +56,10 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
|
|||
constructor(service: SomeService) {}
|
||||
}
|
||||
|
||||
@Component({template: ''})
|
||||
class TestCompErrorOnDestroy implements OnDestroy {
|
||||
ngOnDestroy() {}
|
||||
}
|
||||
|
||||
function resetTestEnvironmentWithSummaries(summaries?: () => any[]) {
|
||||
const {platform, ngModule} = getTestBed();
|
||||
|
@ -207,6 +211,31 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
|
|||
.toThrowError('SomeModule was AOT compiled, so its metadata cannot be changed.');
|
||||
});
|
||||
|
||||
it('should return stack trace and component data on resetTestingModule when error is thrown',
|
||||
() => {
|
||||
resetTestEnvironmentWithSummaries();
|
||||
|
||||
const fixture = TestBed.configureTestingModule({declarations: [TestCompErrorOnDestroy]})
|
||||
.createComponent<TestCompErrorOnDestroy>(TestCompErrorOnDestroy);
|
||||
|
||||
const expectedError = 'Error from ngOnDestroy';
|
||||
|
||||
const component: TestCompErrorOnDestroy = fixture.componentInstance;
|
||||
|
||||
spyOn(console, 'error');
|
||||
spyOn(component, 'ngOnDestroy').and.throwError(expectedError);
|
||||
|
||||
const expectedObject = {
|
||||
stacktrace: new Error(expectedError),
|
||||
component,
|
||||
};
|
||||
|
||||
TestBed.resetTestingModule();
|
||||
|
||||
expect(console.error)
|
||||
.toHaveBeenCalledWith('Error during cleanup of component', expectedObject);
|
||||
});
|
||||
|
||||
it('should allow to add summaries via configureTestingModule', () => {
|
||||
resetTestEnvironmentWithSummaries();
|
||||
|
||||
|
|
|
@ -287,7 +287,10 @@ export class TestBed implements Injector {
|
|||
try {
|
||||
fixture.destroy();
|
||||
} catch (e) {
|
||||
console.error('Error during cleanup of component', fixture.componentInstance);
|
||||
console.error('Error during cleanup of component', {
|
||||
component: fixture.componentInstance,
|
||||
stacktrace: e,
|
||||
});
|
||||
}
|
||||
});
|
||||
this._activeFixtures = [];
|
||||
|
|
Loading…
Reference in New Issue