Is there a way to assert that a route has not been called in Cypress?
Unfortunately none of the above really worked for me, I got it working with this command :
Cypress.Commands.add('shouldBeCalled', (alias, timesCalled) => {
expect(
cy.state('requests').filter(call => call.alias === alias),
`${alias} should have been called ${timesCalled} times`
).to.have.length(timesCalled);
});
Which I then use like this :
// Checks that FetchChatList has not been called
cy.shouldBeCalled('FetchChatList', 0);
It is very difficult to test a situation where an action has not occured. With this type of assertion, you can really only say:
"The XHR request was not made within the 400ms
that Cypress looked for this XHR request to have been made (or whatever you set your timeout to be)"
This doesn't really confirm that the XHR request was never called.
That being said, Cypress offers a way to retrieve all XHR requests made using the undocumented cy.state('requests')
. You could check the length of that, filter them by alias, etc to probably determine what you want.
As a variant set in routes options onResponse
function which drops test
e.g. expect(true).to.be.false;
it will fire error if call happened for current route
cy.route({
url: <url>,
onResponse: function () {
expect("Unexpected Https call").to.be.false;
}
})