Cypress Intercepted Responses

Profile picture for user arilio666

Intercepted responses can be made in two ways. One is done by passing a callback to req.continue(). Another is done by listening for the before:response or response request events. Using the response object, a res is passed as the first argument to the handler function:

cy.intercept('/users', (req) => {
 req.on('before:response', (res) => {
//
 })
  • Using 'before' is because this will be called before any req.continue or response handler.
cy.intercept({
           method: 'POST', 
           url: 'https://reqres.in/api/users',
           middleware: false
       },(req) => {
           req.continue((res) => {
               res.send({ "name":"JohnWick",
               "job":"Assasin"
               })
           })
       })
  • This is called after all, before:response.
  • By calling req.continue, we acknowledge that this handler will be the last.
  • There can only be one reason the request should be sent outgoing.
  • One req.continue handler per request.
 req.on('response', (res) => {
 //
 })
  • This will also be called after all before:response.
  • It is also called after the req.continue handler and before the response is sent to the browser.