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

TakeLastest effect test error.

Open EYHN opened this issue 7 years ago • 6 comments

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)

EYHN avatar May 18 '18 03:05 EYHN

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();
  });

samuelcastro avatar May 23 '18 17:05 samuelcastro

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

EYHN avatar May 28 '18 06:05 EYHN

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.

jfairbank avatar May 28 '18 19:05 jfairbank

Any update? I have the same error

alexander-elgin avatar Aug 20 '18 07:08 alexander-elgin

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

jmahesh123 avatar Mar 15 '19 11:03 jmahesh123

any update?

manelpb avatar Apr 01 '19 15:04 manelpb