Help Needed: How can I test a rxjs ThrowError without causing Jasmine to time out?
In my project, I am working on testing a service that returns either a completed Observable or an error. The service code is as follows:
import { Observable, of, throwError } from 'rxjs';
export class MyService {
foo(shouldError: boolean): Observable<any> {
if (shouldError) {
return throwError('');
} else {
return of();
}
}
}
For testing purposes, I have written the following test cases:
describe('MyService', () => {
let service: MyService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(MyService);
});
it('handles observable', (done) => {
const shouldError = false;
service.foo(shouldError).subscribe(
(_) => done(),
(_) => done.fail()
);
});
it('handles error', (done) => {
const shouldError = true;
service.foo(shouldError).subscribe(
(_) => done.fail(),
(_) => done()
);
});
}
However, running these tests causes Jasmine to timeout and display the following error message:
Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)
I am stuck at this point and would appreciate any guidance on how to resolve this issue.