I wish to create a custom command that contains status code assertions:
cy.wait('@interceptedEndpoint')
.its('response.statusCode')
.should('eq', 200)
the last two lines are what I want inside the custom command
.its('response.statusCode')
.should('eq', 200)
My initial idea is below which obviously wouldn't work
Cypress.commands.add('checkResponse', {prevSubject: 'element'}, () => {
.its('response.statusCode')
.should('eq', 200)
})
What am I missing above to make the custom command creation correct?
EDIT: In addition to Udo.Kier's answer. I needed to set prevSubject
to true to make it accept the response for the expected assertion.
You can try adding the subject
parameter,
Cypress.commands.add('checkResponse', {prevSubject: true}, (subject) => {
cy.wrap(subject)
.its('response.statusCode')
.should('eq', 200)
})
...
cy.wait('@interceptedEndpoint').checkResponse()
Alternatively, with the wait inside the custom command:
Cypress.commandsCommands.add('waitAndCheckResponse', (subjectAlias) => {
cy.wait(subjectAlias)
.its('response.statusCode')
.should('eq', 200);
})
...
cy.waitAndCheckResponse('@interceptedEndpoint')