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

Expect saga to cancel forked task? #341

Closed carljohnson93 closed 4 years ago

carljohnson93 commented 4 years ago

How do I write assertion for saga if it cancels forked task? I tried but it throws that there's no cancel method.

describe('timerSaga', () => {
  it('watches stop and cancels timer task', () => {
    return expectSaga(timerSaga)
      .cancel(timer)
      .dispatch({ type: 'STOP' })
      .run()
  }
}
carljohnson93 commented 4 years ago

I've figured out that it's impossible to observe canceled task because of lack of direct control on next value of generator, when using expectSaga. Better solution that I've found is to use race, it works same for my purpose, less verbose, automatically cancels loser task, and it can be tested with expectSaga.

  describe('timerSaga', () => {

     it('should watch start and race stop action and timer task', () => {
        return expectSaga(timerSaga)
          .race([
            call(timer),
            take('STOP')
          ])

          .dispatch(start())
          .run()
     })

})