I've being trying to increase the test's coverage of a project. Looking into the coverage report, I saw that retryWhen method is not being covered and since then I've been struggling.
Here's the function I'm trying to test:
launchWebHook<T>(
webhookUrl: string,
webhookRequest: IWebHookShape<T>,
signedToken: string,
): Observable<IWebHookAxiosResponse> {
const requestOptions = {
headers: {}, // not empty in src code
};
return this.httpService.post(webhookUrl, webhookRequest, requestOptions).pipe(
retryWhen(errors =>
errors.pipe(
delay(900000),
take(3),
scan((acc, source, index) => {
if (index) {
throw source;
}
}),
),
),
);
}
And here is the test I have:
const webhookUrl = 'https://webhook.com';
const webhookRequest = {} as IWebHookShape<string>;
const signedToken = 'signed';
const response = {} as AxiosResponse<any>;
(httpService.post as jest.Mock).mockResolvedValue(new Observable<AxiosResponse<any>>());
const mockPipe = jest.spyOn(Observable.prototype, 'pipe');
const retryWhen = mockPipe.mock.calls[0][1];
webhookService.launchWebHook(webhookUrl, webhookRequest, signedToken);
expect(retryWhen).toHaveBeenCalled();
I tried using a similar implementation of this answer, but couldn't make it work.
I'm also trying to use mockFn.mock.calls
based on this answer