How to mock Promise.all? #969
Unanswered
alessandrocapra
asked this question in
Q&A
Replies: 1 comment 3 replies
-
Hey, @alessandrocapra. First things first, you'd have to create a handler to account for the rest.post('/balances/:currency', (req, res, ctx) => {
// req.params.currency — to get the exact value
return res(/* your mocked response */)
}) Once this is done, go to the test case that asserts how your UI handles that request's failures and override the response in the same manner as you've posted above: it('shows error message if /balances/:currenty API returns an error', async () => {
server.use(
rest.post('/balances/:currency', (req, res, ctx) => {
return res(ctx.status(500))
})
)
// ...assert your UI here.
}) Keep in mind that different request clients handle 4xx/5xx responses differently. For instance, |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Let's say I have an endpoint called
/balances
.Mocking the single request is easy, but another use case is to call multiple times this endpoint for each currency (e.g.
/balances/EUR
,/balances/USD
. To do so, I pass all the currency codes and make a Promise.all(...) request with the different URLs.The following test works for a single call, but if the widget uses the Promise.all(...) case I just get an array with the
ctx.json
message, but the promise is not rejected so the test is failing.Am I missing something? Thanks!
Beta Was this translation helpful? Give feedback.
All reactions