Cypress: cy.route() onRequest callbacks do not execute cy commands

Created on 7 Feb 2019  Â·  5Comments  Â·  Source: cypress-io/cypress

Current behavior:

I'm stubbing a route and would like to execute some cy commands with in the cy.route options.onRequest callback function.

it('Another look at route response callbacks', function() {
    auth.startSession(config.executionMode.e2e).then((token) => {           
        const baseUrl = useCI ? config.environment.CI.url : config.environment.LOCAL.url;
        const authUrl = `${baseUrl}/authenticate/cosi?signontoken=${token}`;

        cy.fixture('_landingPage.json').then((contents) => {
            const pdp1 = contents[3];

            const onRequest = () => {
                console.log(`console.log onRequest`); // logs as expected
                cy.log(`cy.log onRequest`); // *** appears to have no effect ***
            }

            cy.server();
            cy.route({
                'method': pdp1.method,
                'url': pdp1.url,
                'onRequest': onRequest,
                'response': pdp1.body
            }).as(pdp1.url);

            cy.log('hello world'); // logs as expected
            cy.visit(authUrl);
        });
    });
}

image

  • The cy.route works fine and stubs the response.
  • The onRequest callback is getting called as evidenced by the console.log() that works as expected.
  • However the cy.log() (or other cy commands I put w/in the onRequest or onResponse) call backs silently do nothing.

Desired behavior:

  • either execute the cy commands or give an error explaining why they don't work

Steps to reproduce: (app code and test code)

See code above.

Versions

  • cypress: 3.1.4
  • os: Windows 7
  • chrome: 72
proposal 💡 enhancement

Most helpful comment

I have the same issue. Have you found any workaround?

All 5 comments

In my case adding cy commands inside those callbacks causes requests to hang.

Code

    cy.route({
      url: `/v2/initiatives/${initiativeId}/exports`,
      method: 'GET',
      response: exportsData,
      onRequest: () => {
        cy.get('#active-view ul li')
          .should('have.length', 4)
          .contains('button', 'Pending');
      },
      onResponse: () => {
        cy.get('#active-view ul li')
          .should('have.length', 4)
          .contains('button', 'Download');
      },
    }).as('exports');

Execution

Screen Shot 2019-03-12 at 8 26 45 PM

I have the same issue. Have you found any workaround?

My simple workaround is to add cypress command:end event like:

 cy.route({
      url: `/url`,
      method: 'GET',
      response: {},
      onResponse: () => {
          Cypress.once('command:end', (command) => {
              cy.log('IT WORKS :)');
          })
      },
    })

I hope it help you :)

In my case cy.log caused error instead of returning specified response

 cy.route({
            method: 'POST',
            url: '**/Submit’
            onRequest: req => {
                const requestBody = req.request.body;
                cy.log(requestBody);
            },
            response: {},
        }); 

 When I removed cy.log, the test worked as expected.

I would appreciate the fix or at least some error message with a hint, what happens.

 

I had to use this workaround to check the content of the UI onRequest and onResponse. I stored the values in separate variables and wrote a spec to verify the content outside of the callbacks.

let onRequestButtons;
let onResponseButtons;

before(() => {
  cy.route({
    url: `/v2/initiatives/${initiativeId}/exports`,
    method: 'GET',
    response: exportsData,
    onRequest: () => {
      onRequestButtons = Cypress.$('#active-view ul li');
    },
    onResponse: () => {
      onResponseButtons = Cypress.$('#active-view ul li');
    },
  }).as('exports');
})

it('has the correct request content', () => {
  cy.wrap(onRequestButtons).should('have.length', 4).contains('button', 'Pending');
});

it('has the correct response content', () => {
  cy.wrap(onResponseButtons).should('have.length', 4).contains('button', 'Download');
});


Was this page helpful?
0 / 5 - 0 ratings