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

TakeLastest effect test error. #195

Open EYHN opened 6 years ago

EYHN commented 6 years ago

saga.js

export default function* language() {
  yield takeLatest(CHANGE_LOCALE, changeLocale);
  yield put(changeLocaleAction(navigator.language));
}

test.js

describe('language Saga', () => {
  it('should start task to watch for CHANGE_LOCALE action', () => {

    testSaga(language).takeLatest(CHANGE_LOCALE, changeLocale);
  });
});

error message:

 FAIL  src/containers/LanguageProvider/test/saga.test.ts
  ● language Saga › should start task to watch for CHANGE_LOCALE action

    TypeError: Cannot read property 'pattern' of undefined

       7 |   it('should start task to watch for CHANGE_LOCALE action', () => {
       8 |
    >  9 |     testSaga(language).takeLatest(CHANGE_LOCALE, changeLocale);
      10 |   });
      11 |
      12 |   // it('should put CHANGE_LOCALE action with navigator.language', () => {

      at validateTakeHelper (node_modules/redux-saga-test-plan/lib/testSaga/validateTakeHelper.js:42:50)
      at Object.takeHelperProgresser [as takeLatest] (node_modules/redux-saga-test-plan/lib/testSaga/index.js:315:59)
      at Object.it (src/containers/LanguageProvider/test/saga.test.ts:9:24)
samuelcastro commented 6 years ago

Hi @EYHN , the best way to test it would be with expectSaga like:

it('test', () => {
  const changeLocaleAction = {
      type: CHANGE_LOCALE,
    };

    return expectSaga(yourSagaFn)
      .put(changeLocaleAction(navigator.language))
      .put(whateverYouWantToTest)
      .dispatch(changeLocaleAction)
      .run();
  });
EYHN commented 6 years ago

I don't know what happened. Maybe some error messages will be helpful.

jfairbank commented 6 years ago

Hi, @EYHN.

I'd recommend using expectSaga like @samuelcastro suggested.

If you want to use testSaga, then make sure you're using the correct assertion method.

Are you using the takeLatest effect creator (not the helper)? That is, are you importing takeLatest like this?

import { takeLatest } from 'redux-saga/effects'

If so, then you need to use takeLatestEffect in your test after calling next(), which is documented here.

describe('language Saga', () => {
  it('should start task to watch for CHANGE_LOCALE action', () => {
    testSaga(language)
      .next()
      .takeLatest(CHANGE_LOCALE, changeLocale);
  });
});

Hope that helps.

alexander-elgin commented 6 years ago

Any update? I have the same error

jmahesh123 commented 5 years ago

Same issue: SagaTestError: Assertion 2 failed: actual takeLatest did not take a pattern

manelpb commented 5 years ago

any update?