From 90fcfcf2bf35e29e40e17798fc07eafb83834e39 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Mon, 7 May 2018 11:30:16 -0400 Subject: [PATCH] Polyfill QUnit memory leak prevention. This polyfills the changes from https://github.com/qunitjs/qunit/pull/1279 to older versions of QUnit. --- Prior to the changes in this PR, all module and test callbacks are retained (forever). This may not seem significant, but as folks use closure scope to store data across tests (which is very common). For example, prior to the changes in this PR the following would retain the local variable: ```js QUnit.module('top', function(hooks) { let largeThing; hooks.beforeEach(function() { largeThing = new LargeThing(); }); hooks.afterEach(function() { largeThing.destroy(); }); test('something that uses largeThing', function(assert) { // ...snip... largeThing.someMethod(); }); }); ``` --- tests/index.html | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/index.html b/tests/index.html index 77af4d38995..6fae6ab8432 100644 --- a/tests/index.html +++ b/tests/index.html @@ -192,6 +192,9 @@ }); QUnit.testDone(function(results) { + // release the test callback (polyfill for https://github.com/qunitjs/qunit/pull/1279) + QUnit.config.current.callback = undefined; + var oldFixture = document.getElementById('qunit-fixture'); var parent = oldFixture.parentElement; var newFixture = document.createElement('div'); @@ -207,6 +210,11 @@ } }); + QUnit.moduleDone(function() { + // release the module hooks (polyfill for https://github.com/qunitjs/qunit/pull/1279) + QUnit.config.current.module.hooks = {}; + }); + QUnit.done(function(result) { console.log('\n' + 'Took ' + result.runtime + 'ms to run ' + testsTotal + ' tests. ' + testsPassed + ' passed, ' + testsFailed + ' failed.'); });