'Mocking server side requests for end to end testing by cypress

I am using Cypress for end to end tests. To test my payment process I need to mock the request of my back end(express) to bank for transaction. So when the cypress test clicks buy, a request goes to backend and the backend sends a request to bank for purchase, then the bank sends response to backend. So I need to test everything except requests to/from bank. From this answer I assume there is no way to mock server side requests by cypress. Hope someone can show me that I am wrong and the way of doing that! I was hoping that cypress plugins will let me to do that, but seems like I misunderstood the purpose of them.



Solution 1:[1]

Cypress provides a very easy way to intercept backend requests. You can use below example to mock the server side request.

cy.intercept method takes three parameters HTTP method type, endpoint & mock data. In below example for mock data I am reading it from fixtures. So you just need to have a json file which you want to use as mock response and put in fixture and can be used as below.

cy.intercept('GET', '**/dashboard', { fixture: 'response.json' })

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prerit Jain