Trying to validate timeout from db queries, not able to achieve it via HttpTestingController.
Not sure how to design the UT. Where to add the timeout in this setup.
it('test timeout', () => {
service.getData('2025-10-12', '2025-10-16').subscribe(
{
next: data => {
expect(data).not.toBeNull();
},
error: error => {
expect(error).toBeNull(); // handling all missing keys
}
}
);
// mocking endpoint - binding
const response = httpController.expectOne({
method: 'GET',
url: RestServerSA.RELEASES_OVERVIEW_RANGE
});
// mocking response from endpoint
response.flush({});
});
You can introduce a delay by using promises
. Where we use setTimeout
inside the promise to resolve
after a certain period.
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Usage will be like below:
it('test timeout', async () => {
service.getData('2025-10-12', '2025-10-16').subscribe(
{
next: data => {
expect(data).not.toBeNull();
},
error: error => {
expect(error).toBeNull(); // handling all missing keys
}
}
);
// mocking endpoint - binding
const response = httpController.expectOne({
method: 'GET',
url: RestServerSA.RELEASES_OVERVIEW_RANGE
});
await delay(5000); // delay for 5 seconds
// or
// await new Promise(resolve => setTimeout(resolve, 5000));
// mocking response from endpoint
response.flush({});
});
You can also try a combination of fakeAsync
and tick
, but I do not favor this, since it already similar to flush
.
it('test timeout', fakeAsync(() => {
service.getData('2025-10-12', '2025-10-16').subscribe(
{
next: data => {
expect(data).not.toBeNull();
},
error: error => {
expect(error).toBeNull(); // handling all missing keys
}
}
);
// mocking endpoint - binding
const response = httpController.expectOne({
method: 'GET',
url: RestServerSA.RELEASES_OVERVIEW_RANGE
});
tick(5000); // delay for 5 seconds
// mocking response from endpoint
response.flush({});
}));