I'm working on increasing a test coverage on an Angular project. I came across something like this in the code:

view plain print about
1observable.pipe(
2 retry(1),
3 catchError(error =>
{ return throwError(error)})
4)

The code was in an Angular HTTP Interceptor, that was doing some relative complex error handling.

The test was something like this:

view plain print about
1observable.subscribe((result) => {
2 },
3 (error) => {
4 expect(error).toBe(someValue);
5 }
6 observer.error(someValue);

The observable was mocked in the test, and it was running assertions inside the error handler.

Although, the test appeared to work when running on the command line, I noticed an error in IntelliJ that no assertions existed in the test. Why is that?

I had to look in a bit deeper. It turns out the test is not taking into account the retry values, and therefore the second step in the pipe never executes.

The solution is to just check twice.

The final test is something like this:

view plain print about
1observable.subscribe((result) => {
2 },
3 (error) => {
4 expect(error).toBe(someValue);
5 }
6 for(let x = 0; x <= 1; x++) {
7 observer.error(someValue);
8 }

Now the error() will be executed on the observer more than once, thus allowing the catchError() to be executed, thereby letting the subscribe functions run, and checking the assertion.

I suck at picking these things up in code reviews without testing the code.