DEV: prevents route nodes to leak on each test run (#14457)

DEV: prevents route nodes to leak on each test run (#14457)

Note this commit is also adding support for teardown in pre-initializers just like we have for initializers.

Co-authored-by: Jarek Radosz jradosz@gmail.com Co-authored-by: David Taylor david@taylorhq.com

diff --git a/app/assets/javascripts/discourse/app/mapping-router.js b/app/assets/javascripts/discourse/app/mapping-router.js
index 39ab250..d5f1e18 100644
--- a/app/assets/javascripts/discourse/app/mapping-router.js
+++ b/app/assets/javascripts/discourse/app/mapping-router.js
@@ -141,6 +141,11 @@ export function mapRoutes() {
     this.route("unknown", { path: "*path" });
   });
 }
+export function teardownRouter(container) {
+  const router = container.lookup("router:main");
+  const constructor = Object.getPrototypeOf(router).constructor;
+  constructor.dslCallbacks.splice(0, constructor.dslCallbacks.length);
+}
 
 export function registerRouter(registry) {
   registry.unregister("router:main");
diff --git a/app/assets/javascripts/discourse/app/pre-initializers/map-routes.js b/app/assets/javascripts/discourse/app/pre-initializers/map-routes.js
index d064dd3..85bb8d2 100644
--- a/app/assets/javascripts/discourse/app/pre-initializers/map-routes.js
+++ b/app/assets/javascripts/discourse/app/pre-initializers/map-routes.js
@@ -1,6 +1,6 @@
 import Application from "@ember/application";
 import { isLegacyEmber } from "discourse-common/config/environment";
-import { registerRouter } from "discourse/mapping-router";
+import { registerRouter, teardownRouter } from "discourse/mapping-router";
 
 let originalBuildInstance;
 
@@ -23,4 +23,8 @@ export default {
       };
     }
   },
+
+  teardown(container) {
+    teardownRouter(container);
+  },
 };
diff --git a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js
index 04e981f..3b2a062 100644
--- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js
+++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js
@@ -296,6 +296,11 @@ export function acceptance(name, optionsOrCallback) {
           initializer.teardown(this.container);
         }
       });
+      app._runInitializer("initializers", (initName, initializer) => {
+        if (initializer && initializer.teardown) {
+          initializer.teardown(this.container);
+        }
+      });
 
       if (LEGACY_ENV) {
         app.__registeredObjects__ = false;

GitHub sha: 4dc14e3a3f4523e672370a81f064d984d905a96b

This commit appears in #14457 which was approved by CvX and davidtaylorhq. It was merged by jjaffeux.