diff --git a/addon-test-support/@ember/test-helpers/settled.ts b/addon-test-support/@ember/test-helpers/settled.ts index 7affd134c..0908ef4f2 100644 --- a/addon-test-support/@ember/test-helpers/settled.ts +++ b/addon-test-support/@ember/test-helpers/settled.ts @@ -2,6 +2,8 @@ import { _backburner } from '@ember/runloop'; import Ember from 'ember'; +import EmberApplicationInstance from '@ember/application/instance'; + import { nextTick } from './-utils'; import waitUntil from './wait-until'; import { hasPendingTransitions } from './setup-application-context'; @@ -30,6 +32,26 @@ const _internalPendingRequests = (() => { return () => 0; })(); +if (typeof jQuery !== 'undefined' && _internalPendingRequests) { + // This exists to ensure that the AJAX listeners setup by Ember itself + // (which as of 2.17 are not properly torn down) get cleared and released + // when the application is destroyed. Without this, any AJAX requests + // that happen _between_ acceptance tests will always share + // `pendingRequests`. + // + // This can be removed once Ember 4.0.0 is released + EmberApplicationInstance.reopen({ + willDestroy(...args: any[]) { + jQuery(document).off('ajaxSend', _internalPendingRequests.incrementPendingRequests); + jQuery(document).off('ajaxComplete', _internalPendingRequests.decrementPendingRequests); + + _internalPendingRequests.clearPendingRequests(); + + this._super(...args); + }, + }); +} + let requests: XMLHttpRequest[]; /** diff --git a/index.js b/index.js index 972761643..9167fbb44 100644 --- a/index.js +++ b/index.js @@ -6,12 +6,6 @@ const debugTree = BroccoliDebug.buildDebugCallback('ember-test-helpers'); module.exports = { name: require('./package').name, - included() { - this._super.included.apply(this, arguments); - - this.import('vendor/monkey-patches.js', { type: 'test' }); - }, - treeForAddonTestSupport(tree) { // intentionally not calling _super here // so that can have our `import`'s be diff --git a/vendor/monkey-patches.js b/vendor/monkey-patches.js deleted file mode 100644 index 35dac5da8..000000000 --- a/vendor/monkey-patches.js +++ /dev/null @@ -1,39 +0,0 @@ -/* globals require, Ember, jQuery */ - -(() => { - if (typeof jQuery !== 'undefined') { - let _Ember; - if (typeof Ember !== 'undefined') { - _Ember = Ember; - } else { - _Ember = require('ember').default; - } - - let pendingRequests; - if (Ember.__loader.registry['ember-testing/test/pending_requests']) { - // Ember <= 3.1 - pendingRequests = Ember.__loader.require('ember-testing/test/pending_requests'); - } else if (Ember.__loader.registry['ember-testing/lib/test/pending_requests']) { - // Ember >= 3.2 - pendingRequests = Ember.__loader.require('ember-testing/lib/test/pending_requests'); - } - - if (pendingRequests) { - // This exists to ensure that the AJAX listeners setup by Ember itself - // (which as of 2.17 are not properly torn down) get cleared and released - // when the application is destroyed. Without this, any AJAX requests - // that happen _between_ acceptance tests will always share - // `pendingRequests`. - _Ember.Application.reopen({ - willDestroy() { - jQuery(document).off('ajaxSend', pendingRequests.incrementPendingRequests); - jQuery(document).off('ajaxComplete', pendingRequests.decrementPendingRequests); - - pendingRequests.clearPendingRequests(); - - this._super(...arguments); - }, - }); - } - } -})();