After encountering and resolving a problem in my code, I wanted to document it here for other users who might face the same issue. This explanation is especially helpful for beginner JS developers like me, as seasoned developers may already be familiar with this error.
The saga that caused the issue is outlined below:
function* patchCart({ payload: { data, sections } }) {
const current = yield select(cartSelectors.getCartModel);
const oldAttributes = pick(current, keys(data));
try {
yield call(patchCartTry, data, sections);
} catch (error) {
yield call(patchCartError, error, oldAttributes);
}
}
During testing, I encountered an error while trying to test the catch block of the saga. A snippet of my test code looked like this:
let current = iterator.next().value;
expect(current).toEqual(select(cartSelectors.getCartModel));
current = iterator.throw(error).value;
expect(current).toEqual(call(utilities.patchCartCatch, error, oldAttributes));
However, when running the test, the output showed:
Error
[object Object] thrown
Despite researching extensively online, I couldn't find a solution. Most cases I found were due to not calling .next()
on the iterator, which I was already doing correctly.
Additionally, even after adding logs within the saga's catch block, they didn't trigger, indicating that the test wasn't reaching that part of the code.
Stay tuned for the forthcoming explanation and solution.