I have the below spec file :
describe('The First Page', () => {
beforeEach(() => {
cy.server();
cy.route({
method : 'GET',
url: '**/v3/user/*',
status: 204,
response: {
id: 1,
firstName: 'Dev',
lastName: 'Dev',
email: '[email protected]',
},
});
});
it('successfully loads', () => {
cy.visit('/dashboard/account');
});
});
By reading the cypress documentation, I thought that this is the way we can stub a http request to the web server. This is not working though as I expected since the http request to 'api/v3/user' returns 504 status. Is there a way to mock/stub a http request using any command of cypress ? I am using the
whatwg-fetch
module in order to make request in my application.
You can test a route multiple times with unique response objects by using aliases and cy. wait(). Each time we use cy. wait() for an alias, Cypress waits for the next nth matching request.
Replace a function, record its usage and control its behavior.
cy. intercept does not make a request, but rather "listens" to requests that occur on the network layer. If we "ask" Cypress to name a certain request that we expect to occur after some action, we can also "ask" it to wait for it before moving on when it notices that such a request occurred. That is, cy.
I finally succeed to resolve the issue. The main reason that I could not stub a request is that I was using on my React-app the
window.fetch
method from the 'whatwg-fetch ' module. As I saw here, https://github.com/cypress-io/cypress/issues/687 there is an issue with the 'fetch' method. So, when I changed the fetch to axios all problems were solved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With