title: Testing actions reliably
body: <p>I'm using xstate to implement a login flow.
  I have a machine where the initialState invokes a Promise, and if it's rejected it will redirect to a state that has an <code>entry</code> action. I would like to test that the action is called at the right time properly.</p>
  <p><strong>machine.ts</strong></p>
  <pre><code>{
    id: 'entrance',
    initial: States.FETCHING_SESSION,
    states: {
      [States.FETCHING_SESSION]: {
        invoke: {
          src: 'fetchSession',
          onError: {
            target: States.LOGGED_OUT,
          }
        }
      },
      [States.LOGGED_OUT]: {
        entry: ['navigateToLogin']
        type: 'final'
      },
    }
  }
  </code></pre>
  <p><strong>machine.spec.ts</strong></p>
  <pre><code>    const mockFetchSession = jest.fn()
        .mockRejectedValueOnce({ error: new Error('401 unauthorized') })
      const mockNavigateToLogin = jest.fn()
  
      const service = interpret(entranceMachine.withConfig({
        services: {
          fetchSession: mockFetchSession
        },
        actions: {
          navigateToLogin: mockNavigateToLogin
        }
      }))
  
      it('Goes to login page on fail', (done) =&gt; {
        service.onTransition((state) =&gt; {
          expect(state.matches(States.LOGGED_OUT))
          expect(mockNavigateToLogin).toHaveBeenCalled() // &lt;- this test case always fails
          done()
        })
        service.start()
      })
  </code></pre>
  <p>I managed to make it work in kinda of an ugly way by wrapping expect around a setTimout.</p>
  <pre><code>setTimeout(() =&gt; expect(mockNavigateToLogin).toHaveBeenCalled(), 100)
  </code></pre>
  <p>I wonder if there is a better option?
  Thanks</p>