I've been doing some testing against a complex array of nested observables. The Observable chain is something like this:

view plain print about
1observable.pipe(
2 withLatestFrom(observable2),
3 switchMap(([data1, data2]) =>
of([data1.get('myID'), data] as [string, string, Data])),
4 switchMap(([myID, data]) => {
5 if(myID) {
6 return [myID, data]
7 } else {
8 angularRouter.navigate('somewhereElse');
9 throwError('item discarded');
10 }
11 },
12 tap(([myID, data]) => {
13 // do other stuff
14 });
15)

Lots going on here. First, we have a withLatestFrom() which grabs the lastest results from a second observable. It passes that and the original resolved data into the switch Map. The switch map does a check. If we have an ID we process onto the tap. If we don't we redirect and throw an error.

This is more complex in my main app, believe it or not. This all worked fine in our app w/o issue; the final tap was never executed when an error occurs.

But, once I started setting things up to test in Angular against this code, the final tap would execute regardless of whether or not the error was thrown. Based on my understanding and all documentation I can find this is not supposed to be the case.

It took some trial and experience to figure out a solution. I changed from the throwError() rxjs command to using JS's throw:

This worked in both in my main app and in my tests without causing errors.