Home How to test redux-thunk middleware async functions?
Reply: 0

How to test redux-thunk middleware async functions?

user1394 Published in July 21, 2018, 9:50 am

I'm trying to test my asyn thunk middleware function using mocha, chai and sinon (my first time!).

Please consider my files:


export const fetchCurrentUser = () => {
   return (dispatch) => {
      setTimeout(dispatch, 100);


it('Should work', () => {
   const dispatch = sinon.spy();
   const action = fetchCurrentUser();



I did not yet implement the fetchCurrentUser function - just assumed it will take some "server" time and then it will call 'dispatch()'.

The spec fails, due to the async flow. If I add a setTimeout of 101 ms before the expect - it passes.

My code will use some DB API that returns promise, so the async function will eventually look like:

return (dispatch) => {
   return dbAPI.fetchUser().then(dispatch(....));

So I tried to require dbAPI and create a sinon.stub().returns(Promise.resolve()) inside the test and it didn't work as well (I thought that since the stub returns a resolved promise - the async function will act like a synchronous function).

Any ideas how should I test async functions like that?

Thank, Amit.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.337192 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO