My team and I are currently exploring ways to make dependent API calls in Cypress more readable. Our current code looks like this:
// nested code
cy.request('GET', myUrl).its('body').then(res => {
cy.request('GET', res).its('body').then(subRes => {
cy.request('GET', subRes).its('body').then(subSubRes => {
expect(subSubRes, myMessage).to.eq(myEvaluation);
})
})
})
We've also considered the following solution, but we don't feel it significantly improves readability:
// less nested code?
let response;
let subResponse;
cy.request('GET', myUrl).its('body').then(res => {
response = res;
})
cy.then(() => {
cy.request('GET', response).its('body').then(subRes => {
subResponse = subRes;
})
})
cy.then(() => {
cy.request('GET', subResponse).its('body').then(subSubRes => {
expect(subSubRes, myMessage).to.eq(myEvaluation);
})
})
If you have any ideas on how to avoid creating nested pyramids of logic, please share them with us. Thanks in advance!