cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Support dynamically disabling request logs via `cy.intercept()`

Open flotwig opened this issue 2 years ago • 9 comments

What would you like?

Add the ability to dynamically disable request logs in cy.intercept(), like so:

cy.intercept('/foo', (req) => {
  req.log = false
}

Why is this needed?

#25547 adds the ability to control log via StaticResponseWithOptions. However, this can only be done statically, not dynamically via an interceptor function.

The reasons why it was done this way are detailed in the original ClickUp initiative. TL;DR: dynamically setting log can cause a log to be hidden/shown asynchronously, which requires making some hard UI decisions about how to indicate this at each step.

Other

No response

flotwig avatar Mar 09 '23 17:03 flotwig

Make sure to update the docs when this issue is closed in the future. https://github.com/cypress-io/cypress-documentation/pull/5106

jennifer-shehane avatar Mar 09 '23 18:03 jennifer-shehane

Any updates on this?

SebeFromGermany avatar Mar 25 '24 17:03 SebeFromGermany

It would be nice to have an option to log only when there is an error.

tuliobluz avatar Jun 03 '24 15:06 tuliobluz

Other options are:

  • to change the signature and move the log option to a "third" options argument (or "fourth" if method or url are passed on separately), like so:
    cy.intercept({method and/or url or RouteMatcher}, {StaticResponse or RouteHandler}, { log: false })
    
    Currently it is a bit of an odd-one-out option in the StaticResponse object. Not sure if other intercept options would qualify for this object.
  • to add the possibility to set the RouteHandler via an option in StaticResponse, like so:
    cy.intercept({method and/or url or RouteMatcer}, { handler: (req) => { ... }, log: false })
    

Obviously thorough analysis is necessary to check if these signatures are not dubiously conflicting with the existing signature.

verheyenkoen avatar Jun 27 '24 11:06 verheyenkoen

Argh, also needed this to make my logs look a bit prettier.

pbassut avatar Oct 19 '24 00:10 pbassut

same here

MagicDuck avatar Oct 22 '24 02:10 MagicDuck

Other options are:

* to change the signature and move the log option to a "third" options argument (or "fourth" if method or url are passed on separately), like so:
  ```ts
  cy.intercept({method and/or url or RouteMatcher}, {StaticResponse or RouteHandler}, { log: false })
  ```
  Currently it is a bit of an odd-one-out option in the StaticResponse object. Not sure if other intercept options would qualify for this object.

* to add the possibility to set the RouteHandler via an option in StaticResponse, like so:
  ```ts
  cy.intercept({method and/or url or RouteMatcer}, { handler: (req) => { ... }, log: false })
  ```

Obviously thorough analysis is necessary to check if these signatures are not dubiously conflicting with the existing signature.

Wouldn't the last option you suggest also be compatible with the current signature ? The code basically has to check if the 2nd argument is a plain handler, or if it's an object with a "handler" property, and decide what to do based on that. This way there's no need to check all the codebase for conflicts, as it would be backwards-compatible. Am I missing something @verheyenkoen ?

nerochiaro avatar Oct 22 '24 11:10 nerochiaro

Also in need of something like this, we are using a custom handler function to set aliases for our GraphQL requests and would like to also control the logging behavior at the same time

Tzinov15 avatar Oct 22 '24 18:10 Tzinov15

might be a workaround for some cases. This worked for me:

        cy.intercept('/something/**', (req) => {
            // redirect to local dev server
            const url = new URL(req.url)
            url.hostname = Cypress.env('dev_host')
            url.port = Cypress.env('dev_port')
            url.pathname = url.pathname.replace('/something', '')
            req.url = url.toString()
        })
        // disable logs 
        cy.intercept('/something/**', { log: false })

MagicDuck avatar Oct 22 '24 18:10 MagicDuck