redux-saga-test-plan
redux-saga-test-plan copied to clipboard
TakeLastest effect test error.
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)
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();
});
I don't know what happened. Maybe some error messages will be helpful.
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.
Any update? I have the same error
Same issue: SagaTestError: Assertion 2 failed: actual takeLatest did not take a pattern
any update?