Sometimes we want to validate that a certain API request has not been fired, for this scenarios we're adding to the relevant cy.route obj:
onRequest: () => throw new Error('This BE call should have not been triggered!')
When we first introduced it (don't remember which cypress version and chrome version), it failed the test if requested as desired.
However, testing it now with cypress 3.1.0 and chrome 71 does not.
Throwing an error in cy.route should fail the cypress test
Just add any the above onRequest to a cy.route and see in the console that the error is indeed thrown but that the cypress test doesn't fail.
Cypress 3.1.0, Chrome 71
Any updates on this? I see that it still occurs on 3.1.4
I'm able to reproduce this behavior. When adding a throw into onRequest, the route never resolves.
Example test code
it('throw onRequest', () => {
cy.visit('https://example.cypress.io/commands/network-requests')
cy.server()
cy.route({
method: 'GET',
url: 'comments/*',
onRequest: () => {
throw new Error('This BE call should have not been triggered!')
},
}).as('getComment')
})

onRequest function is called here in code:
https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/commands/xhr.coffee#L189
I'm running into this issue too when trying to make sure that some security-sensitive data is never passed around in plaintext.
I've tested this with onResponse too, and it has the same issue.
Are there any other ways of grabbing the request of an XHR that do allow test failure?
Edit: Yes there is. .wait('@route') yields the xhr object and you can fail a test from there. Sadly this is not a great solution for me as it only handles the first response.
cy.wait('@route')
.then((xhr) => {
throw new Error('I succesfully fail the test');
});
BTW, for us the desired result is for the test NOT to fail when throwing in onResponse, as we are testing that our app handles TypeError's graciously...
Most helpful comment
I'm able to reproduce this behavior. When adding a
throwintoonRequest, the route never resolves.Example test code
onRequestfunction is called here in code:https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/commands/xhr.coffee#L189