diff --git a/addon-test-support/asserts/assertion.js b/addon-test-support/asserts/assertion.js index 7b72444..d9af108 100644 --- a/addon-test-support/asserts/assertion.js +++ b/addon-test-support/asserts/assertion.js @@ -1,6 +1,12 @@ import Ember from 'ember'; import QUnit from 'qunit'; +import { QUnitAdapter } from 'ember-qunit'; +let TestAdapter = QUnitAdapter.extend({ + exception(error) { + this.lastError = error; + } +}); export default function() { let isProductionBuild = (function() { @@ -14,12 +20,18 @@ export default function() { })(); QUnit.assert.expectAssertion = function(cb, matcher) { + // Save off the original adapter and replace it with a test one. + let origTestAdapter = Ember.Test.adapter; + Ember.run(() => { Ember.Test.adapter = TestAdapter.create(); }); + let error = null; try { - cb(); - } catch (e) { - error = e; - } + cb(); + } catch (e) { + error = e; + } finally { + error = error || Ember.Test.adapter.lastError; + } let isEmberError = error instanceof Ember.Error; let matches = Boolean(isEmberError && error.message.match(matcher)); @@ -39,5 +51,11 @@ export default function() { message: matcher ? 'Ember.assert matched specific message' : 'Ember.assert called with any message' }); } + + // Cleanup the test adapter and restore the original. + Ember.run(() => { + Ember.Test.adapter.destroy(); + Ember.Test.adapter = origTestAdapter; + }); }; } diff --git a/tests/helpers/module-for-assert.js b/tests/helpers/module-for-assert.js index 24f215e..0400b8f 100644 --- a/tests/helpers/module-for-assert.js +++ b/tests/helpers/module-for-assert.js @@ -1,8 +1,10 @@ +import { moduleForComponent } from 'ember-qunit'; import { module } from 'qunit'; export default function(name, options = {}) { - module(name, { + let opts = { + integration: options.integration, beforeEach(assert) { let originalPushResult = assert.pushResult; this.pushedResults = []; @@ -30,5 +32,12 @@ export default function(name, options = {}) { 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/integration/assertion-test.js b/tests/integration/assertion-test.js new file mode 100644 index 0000000..40fd61f --- /dev/null +++ b/tests/integration/assertion-test.js @@ -0,0 +1,28 @@ +import Ember from 'ember'; +import { test } from 'ember-qunit'; +import hbs from 'htmlbars-inline-precompile'; +import moduleForAssert from '../helpers/module-for-assert'; + + +moduleForAssert('Integration: Assertion', { + integration: true, + beforeEach() { + this.register('component:x-assert-test', Ember.Component.extend({ + init() { + this._super(); + Ember.assert('x-assert-test will always assert'); + } + })); + } +}); + +test('Check for assert', function(assert) { + assert.expectAssertion(() => { + this.render(hbs`{{x-assert-test}}`); + }, /x-assert-test will always assert/); + + // Restore the asserts (removes the mocking) + this.restoreAsserts(); + + assert.ok(this.pushedResults[0].result, '`expectWarning` captured warning call'); +}); diff --git a/tests/unit/assertion-test.js b/tests/unit/assertion-test.js index 893cbb3..02b39e3 100644 --- a/tests/unit/assertion-test.js +++ b/tests/unit/assertion-test.js @@ -2,8 +2,7 @@ import Ember from 'ember'; import { test } from 'ember-qunit'; import moduleForAssert from '../helpers/module-for-assert'; - -moduleForAssert('Assertion'); +moduleForAssert('Assertion', { integration: false }); test('expectAssertion called with assert', function(assert) { assert.expectAssertion(() => { diff --git a/tests/unit/deprecation-test.js b/tests/unit/deprecation-test.js index b074267..5f1d197 100644 --- a/tests/unit/deprecation-test.js +++ b/tests/unit/deprecation-test.js @@ -3,7 +3,7 @@ import { test } from 'ember-qunit'; import moduleForAssert from '../helpers/module-for-assert'; -moduleForAssert('Deprecation Assert'); +moduleForAssert('Deprecation Assert', { integration: false }); test('expectDeprecation called after test and with deprecation', function(assert) { Ember.deprecate('Something deprecated', false, { id: 'deprecation-test', until: '3.0.0' }); diff --git a/tests/unit/run-loop-test.js b/tests/unit/run-loop-test.js index 71c24e8..d87f41a 100644 --- a/tests/unit/run-loop-test.js +++ b/tests/unit/run-loop-test.js @@ -3,7 +3,7 @@ import { test } from 'ember-qunit'; import moduleForAssert from '../helpers/module-for-assert'; -moduleForAssert('Run Loop Assert'); +moduleForAssert('Run Loop Assert', { integration: false }); test('`expectNoRunLoop` in a run loop', function(assert) { Ember.run.begin(); diff --git a/tests/unit/warning-test.js b/tests/unit/warning-test.js index 9a504d9..451debf 100644 --- a/tests/unit/warning-test.js +++ b/tests/unit/warning-test.js @@ -3,7 +3,7 @@ import { test } from 'ember-qunit'; import moduleForAssert from '../helpers/module-for-assert'; -moduleForAssert('Warning Assert'); +moduleForAssert('Warning Assert', { integration: false }); test('expectWarning called after test and with warning', function(assert) { Ember.warn('Something warned', false, { id: 'warning-test', until: '3.0.0' });