Cypress: Assertion inside onRequest fails, but test doesn't fail

Created on 29 Aug 2018  路  1Comment  路  Source: cypress-io/cypress

Current behavior:

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.

screen shot 2018-08-30 at 00 41 34

Desired behavior:

Test should not succeed

Steps to reproduce:

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();
    });
});

Versions

"cypress": "3.1.0"
OS X
Chrome

question

Most helpful comment

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);
        });
    });
}); 

>All comments

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);
        });
    });
}); 
Was this page helpful?
0 / 5 - 0 ratings