Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change cy.intercept override behavior #14543

merged 35 commits into from Mar 26, 2021


Copy link

@flotwig flotwig commented Jan 13, 2021

User facing changelog

  • BREAKING CHANGE: Request handlers supplied to cy.intercept are now matched starting with the most-recently-defined request interceptor. This allows users to override request handlers by calling cy.intercept again. This matches the previous behavior that was standard in cy.route.
  • There are now new events on the req object for cy.intercept: before:response, response, after:response.
  • Response handlers (supplied via event handlers or via req.continue(cb)) supplied to cy.intercept will be called in reverse order until res.send is called or until there are no more response handlers.
  • A new option, middleware, has been added to the RouteMatcher type. If true, the supplied request handler will be called before any non-middleware request handlers.
  • A new function, req.continue(cb), is available on request objects yielded by cy.intercept. It is functionally the same as req.reply(cb) - the request will stop propagating to other event handlers, will be sent outgoing, and the response will be handled by cb.
  • cy.intercept can now be called with a new overload: cy.intercept(url, routeMatcher, handler). routeMatcher will be merged with url, and the handler is required.
  • Fixed an issue where the response.body would be incorrect after stubbing a response using res.send({ fixture }) in a req.continue callback or a before:response or response handler.

Additional details

See changes to net_stubbing_spec for new + updated tests.

How has the user experience changed?

  • New RouteMatcher option: middleware: boolean
    • Handlers with middleware: true will be called in the order they are defined and chained.
    • With middleware: true, only dynamic handlers are supported - makes no sense to support cy.intercept({ middleware: true }, staticResponse)
  • req now emits events (regardless of middleware setting)
  • Events on req:
    interface InterceptionEvents {
    * Emitted before `response` and before any `req.continue` handlers.
    * Modifications to `res` will be applied to the incoming response.
    * If a promise is returned from `cb`, it will be awaited before processing other event handlers.
    on(eventName: 'before:response', cb: HttpResponseInterceptor): Interception
    * Emitted after `before:response` and after any `req.continue` handlers - before the response is sent to the browser.
    * Modifications to `res` will be applied to the incoming response.
    * If a promise is returned from `cb`, it will be awaited before processing other event handlers.
    on(eventName: 'response', cb: HttpResponseInterceptor): Interception
    * Emitted once the response to a request has finished sending to the browser.
    * Modifications to `res` have no impact.
    * If a promise is returned from `cb`, it will be awaited before processing other event handlers.
    on(eventName: 'after:response', cb: (res: CyHttpMessages.IncomingResponse) => void | Promise<void>): Interception
  • req.continue(cb) has been added. req.reply(cb) still works.
  • A third overload of cy.intercept has been added, to allow users to more easily do things like cy.intercept('/url', { middleware: true }, ...) to set middleware, etc:
    * Use `cy.intercept()` to stub and intercept HTTP requests and responses.
    * @see
    * @example
    * cy.intercept('/fruits', { middleware: true }, (req) => { ... })
    * @param mergeRouteMatcher Additional route matcher options to merge with `url`. Typically used for middleware.
    intercept(url: string, mergeRouteMatcher: Omit<RouteMatcherOptions, 'url'>, response: RouteHandler): Chainable<null>

PR Tasks

Copy link

cypress-bot bot commented Jan 13, 2021

Thanks for taking the time to open a PR!

Copy link

cypress bot commented Jan 13, 2021

Test summary

9497 0 111 3Flakiness 0

Run details

Project cypress
Status Passed
Commit ae33091
Started Mar 26, 2021 5:21 PM
Ended Mar 26, 2021 5:34 PM
Duration 12:06 💡
OS Linux Debian - 10.8
Browser Multiple

View run in Cypress Dashboard ➡️

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@flotwig flotwig changed the base branch from develop to 7.0-release March 3, 2021 19:27
Copy link

@flotwig I think I have found a regression with this patch against the RWA with request modifications not being honored in some way.

In v6.6.0, when I remove the if-none-match header from a request (

cy.intercept("GET", "/notifications", (req) => {
  delete req.headers["if-none-match"];

Subsequent cy.wait's do not return a 304 from the endpoint

Screen Shot 2021-03-12 at 11 02 11 AM

Under this branch, the response is 304 instead of the results:

Screen Shot 2021-03-12 at 11 11 13 AM

Copy link

@flotwig flotwig marked this pull request as ready for review March 24, 2021 22:06
Copy link

@chrisbreiding chrisbreiding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Copy link
Contributor Author

flotwig commented Mar 26, 2021

The 2 failing stages are currently failing in 7.0-release as well, merging.

Copy link

cypress-bot bot commented Apr 5, 2021

Released in 7.0.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v7.0.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Apr 5, 2021
@flotwig flotwig deleted the cy-intercept-overrides branch January 24, 2022 18:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
type: breaking change Requires a new major release version
None yet

Successfully merging this pull request may close these issues.

None yet

3 participants