redux-saga-test-plan icon indicating copy to clipboard operation
redux-saga-test-plan copied to clipboard

Using takeEvery from redux-saga/effects causes TypeError: Cannot read property 'next' of undefined

Open mattoni opened this issue 6 years ago • 5 comments

In the latest version of redux saga (^0.16.0) I was getting a console warning about how importing takeEvery from redux-saga was deprecated and that it needed to be from redux-saga/effects.

Doing so seems to have broken rstp, and now I get the following error:

console.error node_modules/redux-saga/lib/internal/utils.js:240
      uncaught at tokenSaga at tokenSaga
       at takeEvery
       TypeError: Cannot read property 'next' of undefinedat sagaWrapper ([...]redux-saga-test-plan/lib/expectSaga/sagaWrapper.js:54:34)

switching back to the root takeEvery import works without a problem. Also worth noting that the sagas work fine outside of the test scenario.

mattoni avatar Apr 04 '18 03:04 mattoni

Hi @mattoni.

The docs have a section on saga helpers. When you switch to redux-saga/effects for takeEvery, you'll need to update your tests to use takeEveryEffect. You also need to make sure you call next before takeEveryEffect like you have to do with other effects such as put.

jfairbank avatar Apr 04 '18 11:04 jfairbank

Thanks for the response, I must have missed that in the docs. Is there no way to test takeEvery with expectSaga? That's what I was using when the error occurred.

mattoni avatar Apr 04 '18 15:04 mattoni

Ah, sorry. I didn't pay enough attention to your error message to realize you were using expectSaga.

You don't need anything special for takeEvery. expectSaga runs your saga like redux-saga, so you can dispatch actions to the "inner" saga that takeEvery forks. See this example from the docs.

Does that help?

jfairbank avatar Apr 06 '18 21:04 jfairbank

@jfairbank When I was testing sagas with effect "fork", I have errors like:

  1. 'Cannot read property 'next' of undefined'
  2. 'Cannot read property 'apply' of undefined'

And I guess that all this errors exist, because 'redux-saga-test-plan' calls function in effect 'fork' incorrectly. I solved this problem with

await expectSaga(myExampleSaga) .provide({ fork: effect => effect.fn(effect.args[0]), }) .run();

        If I use this way, 'redux-saga-test-plan' calls function in effect 'fork' correctly. 
        
        If I don't want 'redux-saga-test-plan' calls function in effect 'fork', I use this second way (see below):
        
        `  await expectSaga(myExampleSaga)
        .provide({
            fork: (effect, next) => next,
        })`
        
        my package.json: 
        "redux-saga-test-plan": "4.0.0-rc.3",
        "redux-saga": "^1.1.3",
        
        Thank you for your wonderful library)
        

alisa-medvedeva avatar Mar 01 '22 19:03 alisa-medvedeva

@jfairbank Addition: if called function in effect 'fork' has one argument await expectSaga(myExampleSaga) .provide({ fork: effect => effect.fn(effect.args[0]), }) .run();

if called function in effect 'fork' has two arguments await expectSaga(myExampleSaga) .provide({ fork: effect => effect.fn(effect.args[0], effect.args[1]), }) .run();

etc

alisa-medvedeva avatar Mar 01 '22 22:03 alisa-medvedeva