var keyword before a variable assignment, and you’ve inadvertently leaked a variable by defining it in the
Detecting Leaks with Siesta
Siesta, an awesome general-purpose unit and UI testing tool by the fine folks at Bryntum, includes a handy feature that allows you to automatically check for unexpected globals added during test runs. When setting up your test harness, you simply enable monitoring and define any expected globals like so:
expectedGlobals: ['Ext', 'Extensible']
If anything unexpected shows up in global scope during testing it gets flagged as a test failure — awesome!
However, tracking down the offending code can sometimes be tricky. The simple approach is to search in your project for instances of variable assignment. If you are leaking a variable named
foo, a search on “foo =” will likely locate the problem code pretty quickly. In my case, I was leaking a variable named
el, which is only assigned about a bazillion times in Extensible. My initial search returned a ton of matches, so it was time to think of something quicker.
Locating any Leak Instantly
I threw that into the top of my test case and refreshed, and instantly the browser paused in the console right on the
debugger; line. Looking at the previous line in the stack trace showed me this lovely piece of code in the calendar’s Month view:
Oy. I don’t write code like that any more, but this was a holdover from several years ago that simply never got noticed before. A simple fix that literally took seconds to find with this handy little hack.
Don’t Forget to Remove Your Hack
One gotcha that took a few minutes for me to realize… this hack itself, by defining the setter function, is effectively adding the exact same property to the global scope (you end up with a property called
window.el by doing this). So Siesta will still see
window.el after the test and register a failure, but if you’ve fixed the actual bug, the
debugger; line will no longer be hit. This was momentarily confusing, but once I realized what was happening and simply removed my temporary setter override, everything worked with no errors.