'Promise getting rejected instead of resolved

I have a function like this :-

    export async function waitForCallStatus(
callService: CallService,
call: Call,
status: CallStatus,
delay = 300,
retries = 5,
currentTry = 0,
) :Promise<any> {
if (currentTry === retries) {
    throw TfApiError.badRequest('Max retries reached');
}
await Bluebird.delay(delay);
const callId = get(call, '_id');
const pbxCallId = get(call, '_pbxCallId')
const query = {
    _id: callId,
    pbxCallId: pbxCallId,
};
const updatedCall = await callService.findCall(query);
const updatedCallStatus = get(updatedCall, 'status');
if (updatedCallStatus === status) {
    return call;
}
const nextAttempt = currentTry + 1;
return waitForCallStatus(callService, updatedCall, status, delay, retries, nextAttempt);

}

So I am writing a test case for it in jest where it checks again if in first attempt it doesn't match the status-

it('Should check again if in first attempt fetched call status doesnt match the expected status', async () => {
    const status = 'Held';
    (callService.findCall as jest.Mock).mockResolvedValueOnce([currentCall]);
    expect(currentCall.status).not.toEqual(status);
    (callService.findCall as jest.Mock).mockResolvedValueOnce([holdCall]);
    expect(holdCall.status).toEqual(status);
    await expect(waitForCallStatus(callService, currentCall, CallStatus.Held)).resolves.toHaveReturnedWith(holdCall);
});

But every time I am getting the error

Received promise rejected instead of resolved Rejected to value: [INVALID_REQUEST: Max retries reached]

Where am I doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source