diff --git a/addon-test-support/asserts/assertion.js b/addon-test-support/asserts/assertion.js index 90d82d2..7b1ea4a 100644 --- a/addon-test-support/asserts/assertion.js +++ b/addon-test-support/asserts/assertion.js @@ -12,36 +12,38 @@ let TestAdapter = QUnitAdapter.extend({ let noop = () => {}; -function reset(origTestAdapter, origLoggerError) { +let cleanup = (origTestAdapter, origLoggerError) => { // Cleanup the test adapter and restore the original. - Ember.run(() => { + return Ember.run(() => { Ember.Test.adapter.destroy(); Ember.Test.adapter = origTestAdapter; Ember.Logger.error = origLoggerError; }); -} +}; -function handleError(syncErrorInCallback) { - let error = syncErrorInCallback || Ember.Test.adapter.lastError; - let isEmberError = error instanceof Ember.Error; - let matches = Boolean(isEmberError && checkMatcher(error.message, matcher)); +let handleError = (context, error, matcher, isProductionBuild) => { + let isEmberError = error instanceof Ember.Error; + let matches = Boolean(isEmberError && checkMatcher(error.message, matcher)); + let errObj = {}; - if (isProductionBuild) { - this.pushResult({ - result: true, - actual: null, - expected: null, - message: 'Assertions are disabled in production builds.' - }); - } else { - this.pushResult({ - result: isEmberError && matches, - actual: error && error.message, - expected: matcher, - message: matcher ? 'Ember.assert matched specific message' : 'Ember.assert called with any message' - }); - } -} + if (isProductionBuild) { + errObj = { + result: true, + actual: null, + expected: null, + message: 'Assertions are disabled in production builds.' + }; + } else { + errObj = { + result: isEmberError && matches, + actual: error && error.message, + expected: matcher, + message: matcher ? 'Ember.assert matched specific message' : 'Ember.assert called with any message' + }; + } + + context.pushResult(errObj); +}; export default function() { let isProductionBuild = (function() { @@ -71,13 +73,18 @@ export default function() { error = e; } - if (result && typeof result.then === 'function') { + if (error) { + handleError(this, error, matcher, isProductionBuild); + } else if (Ember.Test.adapter.lastError) { + handleError(this, Ember.Test.adapter.lastError, matcher, isProductionBuild); + } else if(result && typeof result === 'object' && result !== null && typeof result.then === 'function') { return result - .then(null, () => handleError(error)) + .then(null, () => handleError(this, error, matcher, isProductionBuild)) .finally(() => cleanup(origTestAdapter, origLoggerError)); + } else { + handleError(this, null, matcher, null); } - - cleanup(origTestAdapter, origLoggerError); + return cleanup(origTestAdapter, origLoggerError); }; } diff --git a/tests/helpers/module-for-assert.js b/tests/helpers/module-for-assert.js deleted file mode 100644 index 0400b8f..0000000 --- a/tests/helpers/module-for-assert.js +++ /dev/null @@ -1,43 +0,0 @@ -import { moduleForComponent } from 'ember-qunit'; -import { module } from 'qunit'; - - -export default function(name, options = {}) { - let opts = { - integration: options.integration, - beforeEach(assert) { - let originalPushResult = assert.pushResult; - this.pushedResults = []; - - assert.pushResult = result => { - this.pushedResults.push(result); - }; - - this.restoreAsserts = () => { - if (originalPushResult) { - assert.pushResult = originalPushResult; - originalPushResult = null; - } - }; - - if (options.beforeEach) { - return options.beforeEach.apply(this, arguments); - } - }, - - afterEach() { - this.restoreAsserts(); - - if (options.afterEach) { - return options.afterEach.apply(this, arguments); - } - } - }; - - if (opts.integration) { - // Really we just want to use an integration loop, but we have to have a target for the moduleFor. - moduleForComponent('component:x-assert-helpers', name, opts); - } else { - module(name, opts); - } -} diff --git a/tests/helpers/setup-assert-test.js b/tests/helpers/setup-assert-test.js new file mode 100644 index 0000000..ab01a3f --- /dev/null +++ b/tests/helpers/setup-assert-test.js @@ -0,0 +1,23 @@ +import QUnit from 'qunit'; + +export default function setupAssertTest(hooks) { + hooks.beforeEach(function() { + let originalPushResult = QUnit.assert.pushResult; + this.pushedResults = []; + + QUnit.assert.pushResult = (result) => { + this.pushedResults.push(result); + }; + + this.restoreAsserts = () => { + if (originalPushResult) { + QUnit.assert.pushResult = originalPushResult; + originalPushResult = null; + } + }; + }); + + hooks.afterEach(function() { + this.restoreAsserts(); + }); +}