The easiest way is to send a mock response, which by-passes waiting for the server. It's feasible if the app does not use the response.
cy.intercept(url, {}).as('my-alias') // 2nd argument is a very simple mock response
// request never gets to the server
// trigger request in app
cy.wait('@my-alias') // should fire on outbound request
If you find the app does complain about the mock response, a stub function could be used instead.
You cannot cy.wait('@stub')
on a stub alias, but you can use cy.get('@stub').should('be.called')
to do the same thing.
Here's a working example.
const stub = cy.stub().as('stub') // set up the stub
cy.intercept('**/my/api/items?1', (req) => {
stub() // call the stub as soon as the request is made
req.reply({
delay: 60_000, // simulate response after 1 minute
})
})
.as('response') // this is the response alias
cy.window().then(win => {
setTimeout(() => {
win.fetch('my/api/items?1') // fire the request after 2 seconds
}, 2_000)
})
cy.get('@stub').should('be.called') // wait for stub call up to 4 seconds
// or add a timeout if required
// cy.wait('@response') // response alias fails after 30 seconds
In your test you can use a shorter version, since you don't need to artificially delay the response
cy.intercept(url, cy.stub().as('stub'))
// trigger request
cy.get('@stub').should('be.called')