ts-mockito icon indicating copy to clipboard operation
ts-mockito copied to clipboard

How to mock catchError

Open ascdi opened this issue 2 years ago • 1 comments

i am trying to test a path that leads into an rxjs catchError function.

Example Code:

    return next.handle(request).pipe(
      catchError((error: HttpErrorResponse) => {
        console.log(`test`);

Now i am Mocking the next Part using mock & instance and tried both thenThrow and thenReturn(throwError( in Order to throw an error that could be catched by catchError. But i never reach the console.log statement.

Example:

    when(mockHttpHandler.handle(anything())).thenReturn(
      throwError(
        new HttpErrorResponse({
          error: 'some cryptic error called 93457tz9w345hzg9w24phg9ow4jh',
          url: testUrl,
          status: 500,
        })
      )
    );

How can i reach that path, mocking my next.handle?

ascdi avatar Jun 16 '22 11:06 ascdi