Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot overwrite cy.route command #3890

Closed
bahmutov opened this issue Apr 4, 2019 · 10 comments · Fixed by #9613 or #14202
Closed

cannot overwrite cy.route command #3890

bahmutov opened this issue Apr 4, 2019 · 10 comments · Fixed by #9613 or #14202
Labels
pkg/driver This is due to an issue in the packages/driver directory type: bug

Comments

@bahmutov
Copy link
Contributor

bahmutov commented Apr 4, 2019

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.
@bahmutov
Copy link
Contributor Author

bahmutov commented Apr 4, 2019

I can see the route in the list but there is no aliases, which means the cy.route(...).as(...) combination did not work

Screen Shot 2019-04-04 at 1 14 04 PM

@mateosilguero
Copy link

mateosilguero commented Apr 5, 2019

Quickly check:

Could you try to write the route as '/comments/*' instead of 'comments/*' ?

@mccataldo
Copy link

@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).

@cypress-bot cypress-bot bot added the stage: ready for work The issue is reproducible and in scope label May 24, 2019
@tozes
Copy link

tozes commented Jun 20, 2019

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

Thanks!

@jennifer-shehane jennifer-shehane added the pkg/driver This is due to an issue in the packages/driver directory label Jun 21, 2019
@tryshchenko
Copy link

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

@iamrandys
Copy link

iamrandys commented Mar 5, 2020

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

@rochanram
Copy link

rochanram commented Jul 29, 2020

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.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 16, 2020

The code for this is done in cypress-io/cypress#9613, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 16, 2020

The code for this is done in cypress-io/cypress#14202, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 21, 2020

Released in 6.2.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v6.2.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Dec 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pkg/driver This is due to an issue in the packages/driver directory type: bug
Projects
None yet
8 participants