Open matiasgarcia opened 1 year ago
So what is happening here is that your promise (tryFetchSettlementFees()
will return a promise) is rejecting, but nothing is catching it so there is no way to test for it. Try changing the call in your effect to this to see what I mean:
tryFetchSettlementFees().catch((err) => console.error(err));
There isn't a way (that I know of) to capture the request promise into the effect itself without storing the result/error into state like you are doing, so you likely just want to move the throw outside of the effect, something like:
function usePlan(): { loading: boolean, plan?: Plan, error?: Error } {
const [loading, setLoading] = useState(true);
const [plan, setPlan] = useState<Plan>();
const [error, setError] = useState<Error>();
useEffect(() => {
const tryFetchSettlementFees = async () => {
setLoading(true);
try {
const currentPlan = await client.getStorePlan();
setPlan(currentPlan);
setLoading(false);
setError(undefined);
} catch (error: any) {
setError(error);
setLoading(false);
}
};
tryFetchSettlementFees();
}, [setLoading, setPlan]);
if(error && !axios.isAxiosError(error)) throw error;
return {
loading,
error,
plan,
};
}
(note this is entirely untested)
Dependencies:
I have the following hook
And I am trying to test the error is raised
Also tried
Also tried
But in all of them I cannot seem to get the error I expect, which is the useEffect throwing the error. However, I get on jest an unhandledPromiseRejection so I might be doing something wrong: