Cypress: Stub response for same route with three different responses
I did one dirty work around that worked, I didn't like it but I am out of options.
I simply combined all the responses in the same response.
My Mock Response
{
balance: {..},
transactionHistory: {..},
currencyRates: {..}
}
The each response handler simply processes part it is interested in, if one of the response is array, we'll need to change it to an object.
I'll be on a lookout for a better work around.
I had the exact same problem and found @Richard Matsen's answer very useful, however when using the whitelist
option it isn't possible to access proxy.request
, which returns undefined
. But if you use onRequest
instead of whitelist
, you can access the request and thus implement any action depending on that request's body.
So this should work:
cy.server({
onRequest: (xhr) => {
xhr.url = xhr.url +
xhr.request.body.action == 'CARD_TRANSACTION_HISTORY' ? '?transactionHistory'
: xhr.request.body.action == 'CARD_BALANCE' ? '?balance'
: xhr.request.body.action == 'CURRENCY_RATES' ? '?currencyRates'
: ''
}
})