tl;dr: to test a saga, it's way, way better to run it as a whole (using runSaga()) than to do it step-by-step (using gen.next())
In my team, we're...
For further actions, you may consider blocking this person and/or reporting abuse
This is a great article! Thank you for this!
How would you throw exceptions to test for api call failures?
For example, if you had a try / catch block for,
const profile = yield call(getProfile, action.profileId);
When you test the saga as a whole, how do you force an error?
I tried using to mockImplementation to return a new Error(), but that doesn't go into the catch block.
I don't know if the APIs are changed, at the moment (Feb, 2020)
runSaga
returns a Task that has atoPromise()
method.So
await runSaga(...).done
does not make sense, you need to doanyway: thank you so much for the article 😊
i'm not sure that's entirely true. when i've gone down the path of testing sagas step-by-step, i've thrown
gen.next()
s into the body of the test without any assertions to get to the step i'm actually trying to test. it's not great and it's pretty brittle if you change your order of operations, but it's not silly either. i don't need to assert that the saga isn't calling a function.Phil, this is brilliant- thanks for writing the article. I'm trying to (roughly) define a testing approach for sagas that'll encourage a more maintainable test suite, and I think you've just nailed a decent way of going about it without introducing another dependency!
Agree with you Phil! I've come across this repo where you can set up end to end test for sagas
github.com/jfairbank/redux-saga-te...
How you got the api calls to be overrided inside the saga?
Thanks bro, you help me a lot..!
I'd love it if you wanted to dissect my Redux one line replacement hook... useSync
dev.to/chadsteele/redux-one-liner-...
Hi Phil, I've been trying the runSaga approach but my dispatch is never called... :-/
I'm not sure if this still relevant, but .done seems to be depracated. Have you tried appending .toPromise() to the end of runsaga ?
Really helpful article. Thanks Phil! This helped me alot.
Great overview, its surprising how little info there is on testing sagas this way.
Awesome post!