From 632f0202beb8740e8f3c0f3f5d9f3ae86e0aeabc Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Tue, 12 Oct 2021 19:06:51 -0400 Subject: [PATCH] Setup basic infrastructure to ensure destroyables destroyed --- .../internal-test-helpers/lib/module-for.js | 30 +++++++++++++++++-- packages/internal-test-helpers/lib/run.ts | 3 +- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/packages/internal-test-helpers/lib/module-for.js b/packages/internal-test-helpers/lib/module-for.js index 9840af48551..853a2b8e491 100644 --- a/packages/internal-test-helpers/lib/module-for.js +++ b/packages/internal-test-helpers/lib/module-for.js @@ -1,8 +1,22 @@ +/* globals URLSearchParams */ +import { DEBUG } from '@glimmer/env'; import { isEnabled } from '@ember/canary-features'; import applyMixins from './apply-mixins'; import getAllPropertyNames from './get-all-property-names'; import { setContext, unsetContext } from './test-context'; import { all } from 'rsvp'; +import { enableDestroyableTracking, assertDestroyablesDestroyed } from '@glimmer/destroyable'; + +const ASSERT_DESTROYABLES = (() => { + if (typeof URLSearchParams === 'undefined' || typeof document !== 'object') { + return false; + } + + let queryParams = new URLSearchParams(document.location.search.substring(1)); + let assertDestroyables = queryParams.get('assertDestroyables'); + + return assertDestroyables !== null; +})(); export default function moduleFor(description, TestClass, ...mixins) { QUnit.module(description, function (hooks) { @@ -10,8 +24,20 @@ export default function moduleFor(description, TestClass, ...mixins) { }); } +function afterEachFinally() { + unsetContext(); + + if (DEBUG && ASSERT_DESTROYABLES) { + assertDestroyablesDestroyed(); + } +} + export function setupTestClass(hooks, TestClass, ...mixins) { hooks.beforeEach(function (assert) { + if (DEBUG && ASSERT_DESTROYABLES) { + enableDestroyableTracking(); + } + let instance = new TestClass(assert); this.instance = instance; @@ -41,10 +67,10 @@ export function setupTestClass(hooks, TestClass, ...mixins) { // promise when it is not needed let filteredPromises = promises.filter(Boolean); if (filteredPromises.length > 0) { - return all(filteredPromises).finally(() => unsetContext()); + return all(filteredPromises).finally(afterEachFinally); } - unsetContext(); + afterEachFinally(); }); if (mixins.length > 0) { diff --git a/packages/internal-test-helpers/lib/run.ts b/packages/internal-test-helpers/lib/run.ts index 5c5db9d074a..7e1ae29de89 100644 --- a/packages/internal-test-helpers/lib/run.ts +++ b/packages/internal-test-helpers/lib/run.ts @@ -6,6 +6,7 @@ import { next, run, } from '@ember/runloop'; +import { destroy } from '@glimmer/destroyable'; import { Promise } from 'rsvp'; @@ -15,7 +16,7 @@ export function runAppend(view: any): void { export function runDestroy(toDestroy: any): void { if (toDestroy) { - run(toDestroy, 'destroy'); + run(destroy, toDestroy); } }