I want to test a service which has a method which returns an observable, but I keep getting this error when running a expect inside subscribe
Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)
I tried to increase the timeout interval for Jasmine, but this didn't work. Here is my code:
user.service.ts:
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class UserService {
subject: Subject<string> = new Subject<string>();
constructor() { }
sendUserNotification(message: string): void {
this.subject.next(message);
}
getUserNotification(): Observable<string> {
return this.subject.asObservable();
}
}
user.service.spec.ts:
import { TestBed } from '@angular/core/testing';
import { UserService } from './user.service';
describe('UserService', () => {
let service: UserService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(UserService);
});
it('should be able to set and get the registered user', (done) => {
service.sendUserNotification('testNotification');
service.getUserNotification().subscribe((notification: string): void => {
expect(notification).toEqual('testNotification1'); // This is causing the error
done();
});
});
});
Please advise what could be wrong. Thanks!