Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to assert that a route has not been called in Cypress?

Tags:

I am trying to assert that a route has not been called in Cypress. I thoroughly looked through the documentation and have found nothing.

I am trying to do something like this:

cy.get('@myRouteAlias').should('have.not.been.called'); 

I am currently working around this by asserting that the successful request toast message is not being displayed but it is a flimsy solution.

Any ideas?

like image 827
Chris Bier Avatar asked Nov 17 '17 15:11

Chris Bier


People also ask

How do I find my route in Cypress?

You can test a route multiple times with unique response objects by using aliases and cy. wait(). Each time we use cy. wait() for an alias, Cypress waits for the next nth matching request.

What are Cypress assertions?

Cypress Commands - UI Interaction Commands. Assertions are the validation steps that determine whether the specified step of the automated test case succeeded or not. In actual, Assertions validates the desired state of your elements, objects, or application under test.


1 Answers

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.

like image 102
Jennifer Shehane Avatar answered Oct 10 '22 15:10

Jennifer Shehane