Encountering this error message during testing:
ERROR: 'Error during cleanup of component'
The issue stems from the following code snippet :
ngOnDestroy(){
methodCallToMock()
}
To address this, I need to mock the methodCallToMock() function within the same component, forcing it to return false and perform no further actions.
This is my test setup :
describe('ChatWindowComponent', () => {
let component: ChatWindowComponent;
let fixture: ComponentFixture<ChatWindowComponent>;
let spy: any;
const MockMessageService = {
getMessages: (i: string) => Observable.of([]),
};
const MockSocketIoService = {
onTyping: () => Observable.of({}),
onStoppedTyping: () => Observable.of({}),
onNewMessage: () => Observable.of({}),
onReadConfirmation: () => Observable.of({}),
onReceptConfirmation: () => Observable.of({}),
};
const MockUserService = {
getCurrentUserFromLocalStorage: () => '',
};
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MaterializeModule, FormsModule],
declarations: [ChatWindowComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [Logger,
TrackingCommunicationService,
{provide: SocketioService, useValue: MockSocketIoService},
{provide: MessageService, useValue: MockMessageService},
{provide: UserService, useValue: MockUserService}],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ChatWindowComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
afterEach(() => {
component = null;
});
it('should create', () => {
spy = spyOn(component, 'methodCallToMock').and.returnValue(false);
expect(component).toBeTruthy();
});
});
I've tried various approaches like the following :
it('should create', () => {
spy = spyOn(component, 'methodCallToMock').and.returnValue(false);
expect(component).toBeTruthy();
});
Despite these attempts, the error persists. How can I troubleshoot and resolve this issue?