Skip to content

Commit

Permalink
Polyfill QUnit memory leak prevention.
Browse files Browse the repository at this point in the history
This polyfills the changes from qunitjs/qunit#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();
  });
});
```
  • Loading branch information
rwjblue committed May 7, 2018
1 parent ce5af99 commit 90fcfcf
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/index.html
Expand Up @@ -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');
Expand All @@ -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.');
});
Expand Down

0 comments on commit 90fcfcf

Please sign in to comment.