Recently, when setting up an integration E2E test on a BETA site, I ran into an issue where it was taking forever and a day to get through the tests because there was up to 30 seconds of delay trying to load a JavaScript file for google analytics, specifically the ga.js file, before eventually timing out. I obviously don't care about Google Analytics coming from my E2E tests so how to ignore this request entirely. Fortunately, Cypress has a handy way of blacklisting hosts which means it will never even let the request go out. It actually took me a while to find out how to do this although it is available from the Cypress docs site. We essentially stub out Google Analytics and this repo which is linked to from the Cypress docs shows us how.

It's pretty simple actually. It just requires two changes in your files.

  1. Update Cypress.json in project root with this to set the blacklistHosts property. It accepts either a string or an array.
  "blacklistHosts": ["", ""],

2.  Place the following in your /support/index.js file or whatever test file before              your tests.

Cypress.on("window:before:load", (win) => {
  // because this is called before any scripts
  // have loaded - the ga function is undefined
  // so we need to create it. = cy.stub().as("ga");

And that should be enough. Now, you can check the Network tab when you run your tests and you should see a 503 error for the request for ga.js which returns immediately. And wham, you've just saved yourself minutes of time.