how to test redux-saga all effect using jest

Viewed 2404
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)
    ]))
2 Answers

It's just an issue happen on old redux-saga, your code will work on the latest version (v1.1.3 at the moment).

I created a testing snippet for that, the test is working perfectly with redux-saga version 1.1.3. And we can reproduce this error if we downgrade the version to 0.16.2.

So an version upgrade can fix this error. Hope it helps.

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.

Related