While there are numerous resources available online for testing a service that sends an API request, I am facing challenges in testing a service that:
- invokes another service that sends an HTTP request and then returns the response to the original service
- sends an HTTP request based on the above scenario
Below is a snippet of my code where I'm encountering difficulties:
beforeEach((done) => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
HttpClientTestingModule,
JwtModule.forRoot({
jwtOptionsProvider: {
provide: JWT_OPTIONS,
useFactory: jwtOptionsFactory,
deps: []
}
}),
],
providers: [
ConfigService,
AuthenticationService,
UserService,
JobService
]
});
let injector = getTestBed();
sut = injector.get(JobService);
httpMock = injector.get(HttpTestingController);
});
describe('Appointment Management', () =>{
it('should set an appointment', () => {
expect(sut).toBeDefined();
const dummyResponse = 'dsfsdf';
sut
.setAppointmentDate('temp', new Date(2017, 12, 31))
.subscribe(result => {
expect(result).toEqual(dummyResponse)
})
const req = httpMock.expectOne('http://localhost:32307/appointments');
expect(req.request.method).toBe('POST');
req.flush(dummyResponse);
});
})
To provide further context, the job service is the main focus of this test, with dependencies on configService and userService.
The user service relies on configService and authentication service, while router and jwt service are essential for authentication service and user service.
All services also require the use of HttpClient.
THE ERROR MESSAGE I AM ENCOUNTERING:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Even though there should be no actual external calls made during testing, I am puzzled as to why it times out. I attempted stubbing out the dependency calls, but it resulted in the same error, similar to:
const userRequest = httpMock.expectOne('http://localhost:32307/user');
userRequest.flush('temp');