Cypress.io - Programmatically set response based on request parameters in cy.route()
This is sadly currently not supported with cy.server
.
The issue is being tracked here : https://github.com/cypress-io/cypress/issues/521
Workaround
Use standard javascript mocking. You can run this mocks in tests by using cypress onBeforeLoad
, mentioned a few times in the linked issue. Its not pretty. Hopefully cypress gets native support in cy.server
.
I had the same problem and made a feature to enable this. It does require a bounce back url to be stood up but the code install instructions can be seen here https://bitbucket.org/snippets/matt-tasc/onraxo
I believe this should work
cy.server({ onResponse: ({ status, url, response }) => { if(url !== 'yoururl') return response; return { ...response, body: { status: status === 1234 ? 'success' : 'failure' } }; } });