I have some tests that look like this. The point is that after submitting a form app redirect to other page with other requests that I wouldn't like to stub. That's why I don't use .then() construction and would like to finish tests before I get a response (even stubbed). Currently this test shows failed assertion but it's difficult to notice because test looks to be passed successfully.

Test should not succeed
describe('Login', function() {
it('should fail', function() {
cy.server();
cy.visit('/login');
cy.route({
method: 'POST',
url: `/authenticate`,
onRequest: xhr => {
// In this place I have assertions with xhr.request
// Let's do something that fails
expect(2 + 2).to.equal(5);
},
response: {},
});
cy.get('form').submit();
});
});
"cypress": "3.1.0"
OS X
Chrome
Finally solved it with stubbing all insignificant routes that are requested after redirect to other pages by one line.
describe('Login', function() {
it('should fail', function() {
cy.visit('/login');
// Stub all requests
cy.server({ response: {} });
cy.route({
method: 'POST',
url: `/authenticate`,
}).as('authenticate');
cy.get('form').submit();
cy.wait('@authenticate').then(a => {
expect(2 + 2).to.equal(5);
});
});
});
Most helpful comment
Finally solved it with stubbing all insignificant routes that are requested after redirect to other pages by one line.