question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cannot overwrite cy.route command

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
tryshchenkocommented, Jul 10, 2019

We also have this issue. Once route is defined - it persist the first version of the mock. Makes it harder to check negative cases.

4reactions
tozescommented, Jun 20, 2019

@bahmutov do you have any updates on this? We also ran into it.

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overwriting an existing command with Cypress - Stack Overflow
I'm trying to overwrite an existing command in Cypress.io. I'm looking to log() a route response's status & the route's url to extend...
Read more >
Cypress cy.intercept Problems - Gleb Bahmutov
You can overwrite Cypress commands and log a message to the Command Log. Unfortunately overwriting cy.intercept has a bug with aliases #9580 ...
Read more >
Migrating .route() to .intercept() in Cypress - Filip Hric
I have written a blog about this(it was available as an experimental feature). You can judge by the name, that there was clearly...
Read more >
intercept - Cypress Documentation
cy.intercept can be used solely for spying: to passively listen for matching routes and apply aliases to them without manipulating the request or...
Read more >
Cypress v6: Network control using cy.intercept introduction
In this video, I will give a glimpse of cy.intercept command (see ... a much more powerful replacement for the deprecated cy. route...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found