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

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

Before/After memory snapshot on a run of about 40 tests:

Before:

After:

GitHub

I would like a better solution than this, but couldn’t find anything, please be my guest!

Would it be possible to avoid storing the router in a global var and instead get the current router instance from the container? It should be available under router:main registration

!!!

This is not the only thing leaking in this PR fix, but the site part should IMO just removed, I don’t think having router holding a reference to site is a good pattern (I guess it’s legacy?) also I couldn’t find any place where it’s used.

Two notes:

  1. I’d rename the function to teardownRouter (or just teardown if it gets rolled into the Router class) since it doesn’t really removes any registration/references to the instance, but modifies/clear its internal state
  2. I think _extendedRouter.dslCallbacks = [] will achieve the same?

I tried at the beginning but couldn’t have what I wanted… but maybe it was just my limited understanding at that point. Let me check again :+1:

@CvX @davidtaylorhq now that I know what I was looking for I actually found a simpler way… saying it’s less hacky… is left to your appreciation.

I tried to do = [] but it doesn’t work, for a reason I can’t explain sadly :frowning:

Okay, we’ll leave it as an exercise for curious souls. :wink: (maybe me on a weekend :joy:)