This is my code in the service.ts file
private isMessageShown = new BehaviorSubject({ visibility: false});
showMessage() {
const visibilityDetails = {
show: true,
};
this.isMessageShown.next(visibilityDetails);
setTimeout(this.hideMessage.bind(this), 3000);
}
hideMessage() {
this.isMessageShown.next({ visibility: false});
}
get messageVisibilityInfo() {
return this.isMessageShown.asObservable();
}
My service.spec.ts code related to showMessage assertion
it('should get message status', done => {
const result = service.messageVisibilityInfo;
service.showMessage();
result.subscribe(res => {
expect(res.visibility).toBeTruthy();
done();
});
});
The test case is working fine as expected but I am having this error in the console
Uncaught Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out
I have been trying to solve this error for a few days, any help would be appreciated
If you are implementing a service then you must have a UI component associated with it so that it render toast(i hope). Try removing the the timer and implement it such way
ngOnInit(): void {
this.toastObservable.currentToastVisibility.subscribe((data) => {
// Do what every logic or work you want
if (data.visibility) {
setTimeout(() => {
this.toastObservable.hideToast();
}, 3000);
}
});
}
--spec.ts
it('should call ngOnInit', fakeAsync(() => {
component.ngOnInit();
spyOn(toaster, 'hideToast');
tick(3000);
expect(toaster.hideToast).toHaveBeenCalled();
}));