When passing POST as the first parameter to route2 requests are not properly intercepted.
// Does not work
cy.route2('POST', `${BasePage.api_url}/events/createTemplate`).as('create_template')
cy.wait('@create_template')
// Works
cy.route2(`${BasePage.api_url}/events/createTemplate`).as('create_template')
cy.wait('@create_template')
Using the method parameter should work.
https://github.com/bhgsbatista/cypress-test-tiny/tree/route2_method_fail
Cypress v5.3.0
Windows 10
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 POST method is reading the POST as the url argument. I would agree that this is a bug (assuming the documentation is correct).
Looking at the code, it looks like it is always expecting a 3rd argument - the routeMatcher - and won't work without that. Although this contradicts the documentation which says the 3rd arg is optional.

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
})
Pass 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')
This is happening because there is an ambiguity in the definition of route2:
In the case above, the OP is using cy.route2(string, string).
But it can be 2 calls:
cy.route2(url: string, routeHandler: string)cy.route2(method: string, url: string, routeHandler: undefined)The OP tries to use the second definition, but Cypress interpreted it as the first.
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?
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.
@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.
We'll update this issue and reference the changelog when it's released.
Released in 5.5.0.
This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v5.5.0, please open a new issue.
Most helpful comment
That is not at all reflected in the documentation, that same link you shared has this snippet of code:
Below that there's an example using the method argument without altering the response:
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.