how to test redux-saga all effect using jest

て烟熏妆下的殇ゞ 提交于 2019-12-24 01:15:57

问题


function* mySaga() {
  const [customers, products] = yield all([
    call(fetchCustomers),
    call(fetchProducts)
  ])
}

I want to test all effect in jest but I get: Invalid attempt to destructure non-iterable instance

my code is:

    const generator = mySaga()
    expect(generator.next().value).toEqual(all([
      call(fetchCustomers),
      call(fetchProducts)
    ]))

回答1:


I want to test all effect in jest but I get

To deal with an error, it is necessary to understand at first how function generator generally works, irrespective of redux-saga library. Every yield operation performs interrupting generator instance, and point with yield keyword in input/output entry.

So, right value of yield becomes generator.next().value result, and generator.next(RESULT) becomes left value of yield keyword. Redux-saga effects does not perform any special work, just makes special actions objects (https://github.com/redux-saga/redux-saga/blob/master/src/internal/io.js )

So, to solve original task, just pass value to next() generator function, which will be destructured in const [customers, products] = yield statement.



来源:https://stackoverflow.com/questions/47401736/how-to-test-redux-saga-all-effect-using-jest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!