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

cy.route works fine and stubs the response.onRequest callback is getting called as evidenced by the console.log() that works as expected.cy.log() (or other cy commands I put w/in the onRequest or onResponse) call backs silently do nothing.See code above.
In my case adding cy commands inside those callbacks causes requests to hang.
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');

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');
});
Most helpful comment
I have the same issue. Have you found any workaround?