jfairbank / redux-saga-test-plan

Test Redux Saga with an easy plan.
http://redux-saga-test-plan.jeremyfairbank.com
MIT License
1.25k stars 127 forks source link

How to test race with while true? #352

Open StevenWonder opened 4 years ago

StevenWonder commented 4 years ago

I created a saga very similar to the one found in the redux-saga docs https://redux-saga.js.org/docs/advanced/RacingEffects.html

    function* backgroundTask() {
        while (true) { 
            yield delay(1000)
            yield put({ type: 'poke' })
        }
    }

    function* watchStartBackgroundTask() {
        while (true) {
            yield take('START_BACKGROUND_TASK')
            yield race({
                task: call(backgroundTask),
                cancel: take('CANCEL_TASK')
            })
        }
    }

I'm trying to test this saga, but it's stuck in an infinite loop. Here's my test

    it('tests a race', () => {
        return expectSaga(watchStartBackgroundTask)
            .dispatch({type: 'START_BACKGROUND_TASK'})
            .dispatch({type: 'CANCEL_TASK'})
            .provide([
                [delay(1000), undefined]
            ])
            .put.actionType('poke')
            .silentRun()
    })

I expect the race to end with the action CANCEL_TASK, but this test just hangs.

If I remove while true around the backgroundTask then it works, but that's not what I want 🤷 . Without using a provide for the race, what strategy can I use to test this pattern?

Ideally, I want the backgroundTask to run 3-4 times and then dispatch CANCEL_TASK

Versions

redux-saga: 1.1.3 redux-saga-test-plan: 4.0.0-rc.3