how to test redux-saga all effect using jest

2019-07-13 18:13发布

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条回答
孤傲高冷的网名
2楼-- · 2019-07-13 18:26

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.

查看更多
登录 后发表回答