diff --git a/e2e/support/cypress.js b/e2e/support/cypress.js
index bedcc72d030d0060def6e8f87edfc46d5a10e0ba..62225c4f456e4ce0daf284db3f04756621d31511 100644
--- a/e2e/support/cypress.js
+++ b/e2e/support/cypress.js
@@ -54,3 +54,24 @@ Cypress.on("test:after:run", (test, runnable) => {
     );
   }
 });
+
+/**
+ * Our app registers beforeunload event listener e.g. when editing a native SQL question.
+ * Cypress does not automatically close the browser prompt and does not allow manually
+ * interacting with it (unlike with window.confirm). The test will hang forever with
+ * the prompt displayed and will eventually time out. We need to work around this by
+ * monkey-patching window.addEventListener to ignore beforeunload event handlers.
+ *
+ * @see https://github.com/cypress-io/cypress/issues/2118
+ */
+Cypress.on("window:load", window => {
+  const addEventListener = window.addEventListener;
+
+  window.addEventListener = function (event) {
+    if (event === "beforeunload") {
+      return;
+    }
+
+    return addEventListener.apply(this, arguments);
+  };
+});
diff --git a/e2e/test/scenarios/visualizations-tabular/pivot_tables.cy.spec.js b/e2e/test/scenarios/visualizations-tabular/pivot_tables.cy.spec.js
index d701f09cdbb3f973dde0703c792cd51aa002be14..c8bdbc70668a12a6ff53aac14575fcf6dc00118b 100644
--- a/e2e/test/scenarios/visualizations-tabular/pivot_tables.cy.spec.js
+++ b/e2e/test/scenarios/visualizations-tabular/pivot_tables.cy.spec.js
@@ -36,27 +36,6 @@ const TEST_CASES = [
   { case: "dashboard", subject: DASHBOARD_NAME, confirmSave: false },
 ];
 
-/**
- * Our app registers beforeunload event listener e.g. when editing a native SQL question.
- * Cypress does not automatically close the browser prompt and does not allow manually
- * interacting with it (unlike with window.confirm). The test will hang forever with
- * the prompt displayed and will eventually time out. We need to work around this by
- * monkey-patching window.addEventListener to ignore beforeunload event handlers.
- *
- * @see https://github.com/cypress-io/cypress/issues/2118
- */
-Cypress.on("window:load", window => {
-  const addEventListener = window.addEventListener;
-
-  window.addEventListener = function (event) {
-    if (event === "beforeunload") {
-      return;
-    }
-
-    return addEventListener.apply(this, arguments);
-  };
-});
-
 describe("scenarios > visualizations > pivot tables", { tags: "@slow" }, () => {
   beforeEach(() => {
     restore();