question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strange behavior when testing sagas after `call()`.

See original GitHub issue

Hi,

I’m trying to test some simple sagas following the examples in this repository but the tests are giving me strange results. First of all, the code:

sagas/index.js

export function* loadApp() {
  yield put(showLoading());
  const { users, expenses, categories } = yield call(fetchAll);
  yield put(receiveUsers(users));
  yield put(receiveExpenses(expenses));
  yield put(receiveCategories(categories));
  yield put(finishLoading());
}

test/sagas/index.js

describe.skip('#loadApp()', function () {
  const loadAppGenerator = loadApp();
  const users = [{ id: 1 }, { id: 2 }, { id: 3 }];
  const expenses = [{ id: 1 }, { id: 2 }, { id: 3 }];
  const categories = [{ id: 1 }, { id: 2 }, { id: 3 }];

  it('should start yielding SHOW_LOADING action', function () {
    const expected = put(ApplicationActions.showLoading());
    const result = loadAppGenerator.next().value;
    expect(result).to.deep.equal(expected);
  });
  it('should yield a call to api.fetchAll()', function () {
    const expected = call(fetchAll);
    const result = loadAppGenerator.next().value;
    expect(result).to.deep.equal(expected);
  });
  it('should yield action RECEIVE_USERS', function () {
    const expected = put(UsersActions.receiveUsers(users));
    const result = loadAppGenerator.next(users).value;
    expect(result).to.deep.equal(expected);
  });
  it('should yield action RECEIVE_EXPENSES', function () {
    const expected = put(ExpensesActions.receiveExpenses(expenses));
    const result = loadAppGenerator.next(expenses).value;
    expect(result).to.deep.equal(expected);
  });
  it('should yield action RECEIVE_CATEGORIES', function () {
    const expected = put(CategoriesActions.receiveCategories(categories));
    const result = loadAppGenerator.next(categories).value;
    expect(result).to.deep.equal(expected);
  });
  it('should finish yielding FINISH_LOADING action', function () {
    const expected = put(ApplicationActions.finishLoading());
    const result = loadAppGenerator.next().value;
    expect(result).to.deep.equal(expected);
  });
});

In the above example, the first two calls to next() returns a valid state and the tests are passing ok. But, in the third test (when testing RECEIVE_USERS action), when I call loadAppGenerator.next(users).value, it doesn’t pass the users to the action creator. Instead of returning the expected:

PUT: { 
  type: RECEIVE_USERS, 
  payload: { users: [{id:1}, {id:2}, {id:3}] }
}

I’m getting the users as undefined:

PUT: {
  type: RECEIVE_USERS,
  payload: { users: undefined }
}

In other cases is like the next() call was a bit advanced. I have more examples in expenses-api-frontend

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
yelouaficommented, Feb 16, 2016

Yes. Anytime you use the ‘return value’ of yield (e.g via assignament val = yield something, in an expression like if(yield soemthing)), you have to pass the mocked result to the next next call

0reactions
Andaristcommented, Aug 21, 2017

@kumar155 Please share ur problem on something like codesandbox.io so we can help you better.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Redux Saga: weird behaviour when calling API
I'm using sagas in the React/Redux project I'm working on. It's a form to create and edit content. The form is working fine...
Read more >
Testing Sagas - Redux-Saga
There are two main ways to test Sagas: testing the saga generator function step-by-step or running the full saga and asserting the side...
Read more >
Testing Redux-Sagas with a plan. - Medium
Here you will run the saga to the end, mocking select and call effects and asserting the put effects you are interested in....
Read more >
Redux-saga and Typescript, doing it right.
Where inferred generator types fall short ... Inspecting the type of the bar value turns out to be quite weird. Indeed, it is...
Read more >
Efficient Test Cases for Redux-Saga - Innominds
Thus, the native approach for testing a saga is to call the generator ... it("test redux-saga using iterator methods", async() => { const ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found