Unit testing con rxjs

Featured image

Unit testing & rxjs operators

Well, we’re back!! happy 2021! better late than never… and yes, we start this year with a little post about what we love to write about: unit testinggggggggggggg. At this point we love to daleeeeee what we know! enter image description here

I’m going to continue a little bit more giving examples about Jasmine, but soon I’ll be looking to migrate to Jest, so stay tuned for that!

Well yyyy let’s get down to business…as always, the idea is to keep making small posts that we like to read, concrete, short, simple, so let’s go with that! Today I want to point to the tests with certain rxjs operators such as concatMap and debunk some myths

concatMap

This operator according to the definition “*concatMap does not subscribe to the next observable until the previous completes*”, i.e. it concatenates * concatMap with concatMap. that is, it sequentially concatenates our subscriptions, assuming that if the first or any of them fails or is empty, it will not continue the sequence and will stop.

Let’s see an example of this, in our component we can have concatenated calls, conceptually speaking it would be something like:

this.service
  .method()
  .pipe(
    concatMap(() => {
      return this.service2.method();
    })
  )
  .pipe(
    concatMap(() => {
      return this.service2.method2();
    })
  )
  .pipe(
    concatMap(() => {
      return this.service3.method();
    })
  )
  .subscribe(
    () => {
      console.log('josha!!!');
    },
    (error) => {
      console.log('upsis');
    }
  )

It doesn’t matter if we want to concatenate methods from the same service or from different services or blah blah.

What we do care is that, for when we do the unit test of such a case, we know what we can do:

  1. It is not necessary to run the test in an asynchronous zone as you might think (using fakeAsync or async), or applying marble testing, then you can test in a synchronous and simple way.

  2. It is important that we understand the definition, “will not continue if any of the subscriptions fail”. **If in our tests we have double tests (using stubbed services) and/or Spies, we have to take into account that the methods of our services that are involved in this concatenation, MUST RETURN SOMETHING.

enter image description here

In this case we must be careful because if any of these methods does not return an observable with something like: what sequence? there is no sequence.

To see it better with the previous example it would be that in our mock, stub or spy we have in the return of those methods something like

return of('holis')

or even if it is:

return of({})

because if we only return a bare observable enter image description here

return of()

to the ovenoooo to the ovenooooooooooo!!! enter image description here

well, it’s not that bad either, but it won’t work, those concatMaps will remain in the eternal glow of a mind without memory, that is to say, in the eternal limbo.

enter image description here

So well, that’s all friends, let’s code!