How I test redux-saga

December 08, 2017 0 Comments

How I test redux-saga

 

 

I’m a fan of redux saga (although I’ve been keeping an eye on redux-observable too).

I particularly like having all async logic in one place, making it easily testable and having powerful async control flows available where necessary.

Because the saga effects only construct a special object that is consumed and executed by the middleware (aka they dont do much!), it is really easy to test your sagas with no mocking required.

Few points of note:

  • The mock result of the api (true or false) is injected into the subsequent next() call after the api call. So easy!
  • Use of nested test, importing cloneableGenerator and using “.clone()” to test different control flow branches

I have however recently been thinking about RxJs and possibly moving to use redux-observables. There are definite pros and cons and both seem great.

I personally like the straightforward way of writing many yield steps compared to chaining functions with weird names like flatMap in RxJs. But perhaps RxJs provides even more expressive async POWER! :)

Another slight downside of saga is Flow can’t check the arguments to my loginApi call as they are passed as separate arguments to the “call” effect.

Here is a great stack overflow comparison: https://stackoverflow.com/a/40027778/2956606

And some code snippets of saga vs observables to help make up your mind: https://hackmd.io/s/H1xLHUQ8e

If anyone has any comments of improving the way I’ve written things I’d love to know.

Thanks for reading.


Tag cloud