Store: Unit testing documentation

Created on 12 May 2018  路  9Comments  路  Source: ngxs/store

I am having some issues with the example provided on unit test page. Does not work for me.

This was from the docs (does not work):

      it('it toggles feed', () => {
        store.dispatch(new FeedAnimals());
        store.selectOnce(state => state.zoo.feed).subscribe(feed => {
          expect(feed).toBe(true);
          // NOTE: i think at least need a "done()" here (and as param for this test function)
        });
      });

After some tweaking of my own, I got two versions working.

  1. This works for me using async/await and toPromise() as needed:
      it('it toggles feed with async/await', async () => {
        // await because dispatch may take a longer time.
        await store.dispatch(new FeedAnimals()).toPromise();
        const feed = store.selectSnapshot(state => state.zoo.feed)
        expect(feed).toBe(true);
      });
  1. A singlesubscribe() for an Observable stream and done() to complete the asynchronous test (I think piping from dispatch() is optional, should we bother?):
  it('it toggles feed without async/await', done => {
    store.dispatch(new FeedAnimals()).pipe(
      concatMap(() => store.selectOnce(state => state.zoo.feed))
    ).subscribe(
      feed => {
        expect(feed).toBe(true)
        done()
      }
    )
  })

I should also mention that my Angular project uses Jest for unit testing. The configuration was roughly based on this article.

Q: So, any recommendation on whether we should use "async/await with toPromise()" or a "subscribe() with done()" for these unit tests? Or you recommend some other ways?

Most helpful comment

Ok I understand better. Now. Maybe over the weekend I'll have time to throw in some unit testing recipes or something along those lines

All 9 comments

@deebloo - Can you help?

@amcdnl yes I can take a look at the docs

the done does matter in this case. It will depend on your actions. by default observables are actually synchronous so if your action is async you may have to add your listener BEFORE you dispatch your action

I think you are referring to the fact that Actions in ngxs can make state changes via sync (direct state chain, plain reducer) or async (middleware, side-effects). In the case of sync, then we can just check state after the dispatch. This is what the demo unit test code does.

However, for async, we have to do something like my example above (either approach works).

My question was to seek recommended/best practice for unit testing ngxs actions with side-effects.

I suggest update the docs with separate sections to describe testing sync actions and async actions (i am not sure if these are the right terms ) scenarios to make it clear to new users.

Yep, I'd agree with @kctang it'd be good to get a few more examples here, we've been having a little trouble here understanding how we should test these scenarios.
On the whole though we are finding the library easy to use, we've got two feature teams working with NGXS for the last few weeks, overall experience is very positive 馃槂

Ok I understand better. Now. Maybe over the weekend I'll have time to throw in some unit testing recipes or something along those lines

I added some more unit testing docs. Feel free to PR anything you find valuable.

Just to let you know, I also had this issue and updating jest to version 23 solved it.

I don't know if anyone has come across this issue, but I am receiving a SPEC HAS NO EXPECTATIONS should dispatch YOUR_ACTION action. The test is throwing up a false positive. I think these docs are incorrect.

Running Angular 7, Angular CLI 7, TypeScript 3, with:

"@ngxs/devtools-plugin": "^3.2.0", "@ngxs/form-plugin": "^3.2.0", "@ngxs/router-plugin": "^3.2.0", "@ngxs/storage-plugin": "^3.2.0", "@ngxs/store": "^3.2.0",

Was this page helpful?
0 / 5 - 0 ratings

Related issues

piernik picture piernik  路  5Comments

goodmite picture goodmite  路  5Comments

splincode picture splincode  路  4Comments

paulstelzer picture paulstelzer  路  5Comments

CheckMater picture CheckMater  路  5Comments