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 problem using expectSaga or testSaga #347

Open gothraven opened 4 years ago

gothraven commented 4 years ago

Introduction

I have been using redux-saga-test-plan for a while now, I wrote a bunch of tests using this library and it was a great help, I thank everyone who helped for that, but today we noticed some issue with our tests.

Here is an example:

saga.ts

export function* testMe() {
const result = yield call(someApiFunction);
if (result == 'good') {
yield put(someAction);
}
}

Testing this saga can only be done using expectSaga since we need to provide the result of the call function so we can test various situations.

saga.test.ts

describe('testMe Saga', () => {
it('should do some work', () => {
expectSaga(testMe)
.provide([
[call(someApiFunction), 'good'],
])
.put(someAction)
.run();
});
});

The problems

Conclusion

Maybe i'm missing something, or I'm not using the library right, but i want to know how we can test and make sure that the saga doesn't do any other effect, in this kind of circumstances.

mathewpan commented 3 years ago

I'm incredibly late on this, but in case anyone else sees this and needs the answer, you can test the effects with the following:

describe('testMe Saga', () => {
  it('should do some work', () => {
    expectSaga(testMe)
      .provide([
         [call(someApiFunction), 'good'],
      ])
      .run()
      .then((result) => {
        const { effects } = result;

        // Verify only one action was put
        expect(effects.put).toHaveLength(1);

        // Verify the action data is correct
        expect(effects.put[0]).toEqual(
          put(someAction(expectedPayload))
        );
    })
  });
});

Brackets probably don't match up, but its the 'then' block that matters

uwinkler commented 2 years ago

Hi, i had the same problem. All I had to do is to insert a return statement

describe('testMe Saga', () => {
  it('should do some work', () => {
    // 👇 This return is missing
    return expectSaga(testMe)
       ....
  });
});
gothraven commented 2 years ago

@uwinkler Can you please explain how that helps ?

anwarhamr commented 4 months ago

Any movement on this?