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

Testing forked sagas throws: Cannot read property 'name' of undefined #185

Open KasperOlesen opened 6 years ago

KasperOlesen commented 6 years ago

I am trying to test my forked sagas and followed your documentation on it, but the test fails and throws an error: Cannot read property 'name' of undefined. Even though none of my stubs/mocks have a property called 'name', and nor should they.

Instead of writing the whole issue again, you can read about it here: https://stackoverflow.com/questions/49427575/testing-forked-redux-sagas-with-redux-saga-test-plan

jfairbank commented 6 years ago

Hi @KasperOlesen.

In your first testing attempt, you don't seem to be using expectSaga or dispatch correctly. Since applyMenu accepts a menuList argument, you need to provide it in your test when you call expectSaga. The very first example with expectSaga in the docs shows how to do this.

For dispatch, you need to provide an action as an argument, not two arguments like you have in your test. The docs also show how to use dispatch properly.

In your case, I think you need a test like this:

it('Sets a new menu', () => {
  return expectSaga(applyMenu, menuList)
    .put(setMenu(menuList))
    .dispatch(setMenuRequest(menuList))
    .run();
});

See if that stops the error. Otherwise, I will need to see more of your code. For example, what does setMenu look like?


That being said, you CAN test this through takeEvery as well. In your second attempt, you're conflating testSaga with expectSaga. The next, takeEvery, and takeLatest methods don't exist on the expectSaga API, only testSaga, hence the error you're encountering. Instead, you can test like this.

it('Sets a new menu', () => {
  return expectSaga(menuSaga) // just test the top level saga, nothing else changes below
    .put(setMenu(menuList))
    .dispatch(setMenuRequest(menuList))
    .run();
});

Please see if that helps and please ensure you've carefully read through the documentation. I've organized it between unit testing with testSaga and integration testing with expectSaga, so you should only need what's under integration testing if you're using expectSaga. I'm sorry if there are still gaps in the documentation or if something is confusing. I work on this with what little free time I have at the moment.

KasperOlesen commented 6 years ago

Thank you for your response, I will give your examples a try

ataschz commented 4 years ago

Hi, I have the same problem, any solution @KasperOlesen ?

eliaslecomte commented 3 years ago

I had the same exception when I was incorrectly importing my saga.

emmlopez commented 3 years ago

thank you @eliaslecomte , I had same error message , but then I checked if I was importing my saga, and I forgot to export it before function testsaga() after export function testSaga()