0

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({});
    });
1
  • Modified the question. Basically can we provide some delay in response.flush({}) not sure how to do. Commented yesterday

1 Answer 1

1

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({});
}));
3
  • amazing man, i was trying to add response.flush() in settimeout. Just FYI, its not allowing to flush post timeout. If tick is given beyond timeout defined in service. Commented 6 hours ago
  • @ArnabMukherjee but does the promise await timeout work? Commented 6 hours ago
  • i followed the async and tick. If ticks are covering the timeout , and we write response.flush() post that, its saying can't flush a cancelled one. Anyways skipping flush it perfectly working able to see time exception i am raising in service. Commented 5 hours ago

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.