Skip to content

Instantly share code, notes, and snippets.

@simenbrekken
Last active September 5, 2024 07:13
Show Gist options
  • Save simenbrekken/3d2248f9e50c1143bf9dbe02e67f5399 to your computer and use it in GitHub Desktop.
Save simenbrekken/3d2248f9e50c1143bf9dbe02e67f5399 to your computer and use it in GitHub Desktop.
Hide fetch/XHR in Cypress command log

This novel workaround simply hides any command log entries that originate from fetch/XHR requests.

While I've updated this receipe for Cypress 10 and converted it to TypeScript you should be able to use it in a JavaScript project by ignoring the cypress.d.ts file and placing the snippet from e2e.ts in e2e.js instead.

// Add the following to cypress/support/cypress.d.ts
declare namespace Cypress {
interface ResolvedConfigOptions {
hideXHRInCommandLog?: boolean;
}
}
// Add the following to cypress/support/e2e.ts or cypress/support/e2e.js
// Hide fetch/XHR requests from command log
if (Cypress.config('hideXHRInCommandLog')) {
const app = window.top;
if (
app &&
!app.document.head.querySelector('[data-hide-command-log-request]')
) {
const style = app.document.createElement('style');
style.innerHTML =
'.command-name-request, .command-name-xhr { display: none }';
style.setAttribute('data-hide-command-log-request', '');
app.document.head.appendChild(style);
}
}
@shikder-klikit
Copy link

thanks a lot

@Yevhen-Nistratov
Copy link

A slightly easier way of disabling, using cy.intercept():
in your './cypress/support/e2e' set:

before(function () {
    // disable Cypress's default behavior of logging all XMLHttpRequests and fetches to the Command Log
    cy.intercept({ resourceType: /xhr|fetch/ }, { log: false })
})

@Brugui7
Copy link

Brugui7 commented Jul 14, 2023

Based on this thread I created a cypress plugin to filter logs both with cypress open and cypress run based on log levels.

You can check it out here: https://www.npmjs.com/package/cypress-log-filter

@elycruz
Copy link

elycruz commented Jul 25, 2023

Based on this thread I created a cypress plugin to filter logs both with cypress open and cypress run based on log levels.

You can check it out here: https://www.npmjs.com/package/cypress-log-filter

This library would've been great if didn't permit the log messages' markup to render at all (in my scenario I have many requests coming in, almost never ending, which causes the host OS to run out of memory (which just causes cypress to crash)).

Nevermind comment - Just realized your plugin is for filtering ('live') but not necessarily preventing the markup rendering.

Thanks for sharing!

@Brugui7
Copy link

Brugui7 commented Jul 26, 2023

Based on this thread I created a cypress plugin to filter logs both with cypress open and cypress run based on log levels.
You can check it out here: https://www.npmjs.com/package/cypress-log-filter

This library would've been great if didn't permit the log messages' markup to render at all (in my scenario I have many requests coming in, almost never ending, which causes the host OS to run out of memory (which just causes cypress to crash)).

Nevermind comment - Just realized your plugin is for filtering ('live') but not necessarily preventing the markup rendering.

Thanks for sharing!

Yeah, the thing is that I tried filtering them before they were added in the first place but the logs API doesn't allow you to do that :'(

That's as much as I was able to do

@elycruz
Copy link

elycruz commented Aug 2, 2023

👍 , I hear you - In the end I went with the "shimming the Cypress.log method approach (mentioned by @cie), but more refined (used a regular expression, defined outside of custom method, and just ran a test on incoming log displayName types).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment