Cypress 3.2.0
Aliases test in cypress-example-kitchensink. I am trying to overwrite cy.route command to display the route to be spied on.
Cypress.Commands.overwrite('route', (route, ...args) => {
cy.log('foo').then(() => {
return route(...args)
})
})
it('.as() - alias a route for later use', () => {
cy.visit('https://example.cypress.io/commands/aliasing')
cy.server()
cy.route('GET', 'comments/*').as('getComment')
cy.get('.network-btn').click()
cy.wait('@getComment').its('status').should('eq', 200)
})
Gives me an error message
1) Aliasing .as() - alias a route for later use:
CypressError: Cypress detected that you invoked one or more cy commands in a custom command but returned a different value.
The custom command was:
> cy.server()
The return value was:
> Object{24}
Because cy commands are asynchronous and are queued to be run later, it doesn't make sense to return anything else.
For convenience, you can also simply omit any return value or return 'undefined' and Cypress will not error.
In previous versions of Cypress we automatically detected this and forced the cy commands to be returned. To make things less magical and clearer, we are now throwing an error.
If I return undefined, then alias as seems to not work - even as it is shown in the list of registered aliases
Cypress.Commands.overwrite('route', (route, ...args) => {
cy.log(`cy.route ${args.join(' ')}`)
route(...args)
})
cypress run output
1) .as() - alias a route for later use
visit http://localhost:8080/commands/aliasing
log cy.server
log cy.route GET comments/*
get .network-btn
click
xhr GET https://jsonplaceholder.cypress.io/comments/1
log [getComment]
wait @getComment
1 passing (5s)
1 failing
1) Aliasing .as() - alias a route for later use:
CypressError: cy.wait() only accepts aliases for routes.
The alias: 'getComment' did not match a route.
I can see the route in the list but there is no aliases, which means the cy.route(...).as(...) combination did not work

Quickly check:
Could you try to write the route as '/comments/*' instead of 'comments/*' ?
@bahmutov have you made any progress on this? I'd like to create a route based on the request body (in addition to the method and url).
@bahmutov do you have any updates on this? We also ran into it.
Thanks!
We also have this issue. Once route is defined - it persist the first version of the mock. Makes it harder to check negative cases.
Give this a try. Using "as" with this new route command works as well.
Cypress.Commands.overwrite('route', (route, ...args) => {
return cy.log(`cy.route ${args.join(' ')}`).then(() => {
return route(...args);
})
});
We had an use case where we had to overwrite route and this is the only we could make it work:
Cypress.Commands.overwrite('route', (originalFn, ...args) => {
if (args[2]) {
if (args[2].csrfToken) {
originalFn({
method: args[0],
url: args[1],
onRequest: xhr => {
xhr.setRequestHeader('X-CSRF-Token', 'true');
},
});
}
} else {
originalFn(...args);
}
});
In the documentation it shows that we need to return the original function. But when I returned it, it never works.
Also my another question is, can I overwrite cy.server()?
I tried multiple times it seems to never work.
Most helpful comment
We also have this issue. Once route is defined - it persist the first version of the mock. Makes it harder to check negative cases.