Search code examples
angularunit-testingjasminestorengxs

NGXS: Testing Asynchronous Actions: Automatic subscription doesn't work


from official website NGXS

enter image description here

  1. I write unit tests for NGXS store(Jasmine + Karma, angular 2)
  2. In NGXS store in Asynchronous Actions(it's like Effect in NGRX) we don't subscribe inside - we just return observable. And it's fine, because the automatics subscription works.
  3. BUT when I launch my tests, the automatic subscription on Asynchronous Actions(EFFECTS) - just doesn't work. As the result - test failed.
  4. To sum up - I can't test Asynchronous Actions NGXS
  5. What I have already tried : 1) I wrote like in the official website NGXS(in the picture) 2)I tried it(done()) construction, 3) I tried actions$.subscribe() construction.
  6. It works when I make manually - .subscribe() in the Action - but it's problem, because it have to work without this
  7. This is link to official page: https://www.ngxs.io/recipes/unit-testing (topic : Testing Asynchronous Actions) Please, help :)

Solution

  • The example from the official page works correctly. If you stub some function from an action, which returns Observable, try embedding the delay inside your stub function. For example,

    stubFunc: () => { return timer(0).pipe( ... ); }