Tutorial hero
Lesson icon

Observer Spy

Originally published November 10, 2022 Time 1 mins

I’ve been doing a lot of videos on reactive coding with RxJS lately, and something that has some up a lot is that people find testing RxJS code difficult.

At least for my approach to testing, I haven’t found this to be the case, and I think it is entirely due to the the amazing observer-spy library from Shai Reznik - this is the one third party library I always use for testing.

The idea is reasonably straightforward, if you need to test something related to an observable in your test you can subscribe to it like this:

observable in your test you can subscribe

and then you can do whatever you need to do in your test:

methods like getLastValue() on the observer spy to check values

and as you can see above, you can use methods like getLastValue() on the observer spy to check values that have been emitted on the spy. There are additional methods too like:

  • getFirstValue()
  • getLastValue()
  • getValues()
  • receivedError()

I’m actually going to have a full video on this out next week, so keep an eye out if you are interested!

Learn to build modern Angular apps with my course