question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to write high level test for saga? (Subscribe to end of side-effect handling)

See original GitHub issue

Current examples of testing sagas are pretty low level. It gives really fine grained control over exactly what a saga does.

https://redux-saga.github.io/redux-saga/docs/advanced/Testing.html

However, I’d like to be able to test my redux setup on a much higher level. Basically how an application would actually consume it

  1. Create a store with some initial state
  2. Dispatch actions to it, expecting sagas to do any async work / side effects
  3. Let reducers transform state in response to actions dispatched directly / by sagas
  4. Wait until the handling of the actions has finished
  5. Use selectors to get the final state and confirm it matches expectations

However, I don’t know how to accomplish step 4 with redux saga.

With thunk this is super easy.

function incrementAsync() {
  return dispatch => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        // Yay! Can invoke sync or async actions with `dispatch`
        dispatch(increment());
        resolve()
      }, 1000);
    });
  };
}

// Then in test setup
describe('redux store', () => {
  it('increments async by 1', async () => {
    const store = configureStore({counter: 1})
    await store.dispatch(incrementAsync(1))
    const counter = selectCounter(store.getState())
    expect(counter).toBe(2)
  })
})

However, it’s not clear to me how to dispatch an action and then wait until after redux-saga has finished handling side-effects for it. Any ideas?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
yelouaficommented, Jul 11, 2016

You can also run the worker Saga directly

describe('redux saga', () => {
  it('increments async by 1', async () => {
    const store = configureStore({counter: 1})

    await sagaMiddleware.run(incrementAsyncSaga(1)).done
    const counter = selectCounter(store.getState())
    expect(counter).toBe(2)

    await sagaMiddleware.run(incrementAsyncSaga(1)).done
    const counter = selectCounter(store.getState())
    expect(counter).toBe(3)
  })
})
1reaction
tonyxiaocommented, Jul 10, 2016

Read a bit more docs, maybe something like this?

it('increments counter via saga', async () => {

  const promise1 = sagaMiddleware.run(rootSaga).done
  store.dispatch(incrementAsyncViaSaga(1))
  store.dispatch(END)
  await promise1
  expect(counter).toBe(2)

  const promise2 = sagaMiddleware.run(rootSaga).done
  store.dispatch(incrementAsyncViaSaga(1))
  store.dispatch(END)
  await promise2
  expect(counter).toBe(3)
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Managing side-effects with redux-saga - YouTube
Practical examples of how redux- saga can help you reduce application complexity by moving side-effects management to a dedicated place.
Read more >
Testing Redux-Sagas with a plan.
Here you will run the saga to the end, mocking select and call effects and asserting the put effects you are interested in....
Read more >
Testing Sagas
There are two main ways to test Sagas: testing the saga generator function step-by-step or running the full saga and asserting the side...
Read more >
The best way to test Redux Sagas
If we reorder the side effects, we need to fix all of our expect(gen.next(foo).value) assertions, to make sure we're passing the right return ......
Read more >
Saga Middleware for Redux to Handle Side Effects
When developing front-ends, handling asynchronous behavior is always bit of a challenge. Yassine Elouafi's redux-saga provides one solution ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found