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
cy.route2() doesn't work when method is used as the first parameter #8729
Comments
If you want to set the method, without altering the response, you need to do it in following way: cy.route2({ method: 'PUT', pathname: '**/api' }).as('api'); |
That is not at all reflected in the documentation, that same link you shared has this snippet of code: cy.route2(url, routeHandler?)
cy.route2(method, url, routeHandler?)
cy.route2(routeMatcher, routeHandler?) Below that there's an example using the method argument without altering the response: cy.route2('POST', '/organization', (req) => {
expect(req.body).to.include('Acme Company')
}) https://docs.cypress.io/api/commands/route2.html#Intercepting-a-request -- There is definitely an issue here, either with the implementation, or with the documentation. |
It looks like the route for the one with Looking at the code, it looks like it is always expecting a 3rd argument - the it('works', () => {
cy.route2('http://dummy.restapiexample.com/api/v1/create').as('create')
cy.window().then((win) => {
win.eval(
`fetch("http://dummy.restapiexample.com/api/v1/create", {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
});`
)
})
cy.wait('@create')
})
it('should work', () => {
cy.route2('POST', 'http://dummy.restapiexample.com/api/v1/create').as('create')
cy.window().then((win) => {
win.eval(
`fetch("http://dummy.restapiexample.com/api/v1/create", {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
});`
)
})
cy.wait('@create') // wait times out
}) Current WorkaroundPass a 3rd arg until this is fixed / clarified. Just passing an empty object will make it work. cy.route2('POST', 'http://dummy.restapiexample.com/api/v1/create', {}).as('create') |
ProblemThis is happening because there is an ambiguity in the definition of In the case above, the OP is using But it can be 2 calls:
The OP tries to use the second definition, but Cypress interpreted it as the first. Solution?Currently, we're only checking if the first arg is string or not. This ambiguity can be solved by checking if the first arg is an HTTP request method or a WebDAV request method. What do you think, @flotwig? |
@sainthkh yeah, I think that would be a perfect fix, doing a case-insensitive check against known HTTP methods for the first string parameter. Do you want to pick this up? |
I do. I'll get it done today. Maybe you'll be reviewing the PR next morning |
The code for this is done in cypress-io/cypress#8829, but has yet to be released. |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
Current behavior
When passing
POST
as the first parameter toroute2
requests are not properly intercepted.Desired behavior
Using the method parameter should work.
Test code to reproduce
https://github.com/bhgsbatista/cypress-test-tiny/tree/route2_method_fail
Versions
Cypress v5.3.0
Windows 10
The text was updated successfully, but these errors were encountered: