Skip to content

Commit

Permalink
Improved the implementation of expectAssertion to handle assertions…
Browse files Browse the repository at this point in the history
… thrown inside of a runloop during an integration test. Related to: emberjs/ember.js#14898 .
  • Loading branch information
Wesley Workman authored and workmanw committed Mar 12, 2017
1 parent f46b10b commit b72dbd8
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 11 deletions.
26 changes: 22 additions & 4 deletions addon-test-support/asserts/assertion.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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));
Expand All @@ -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;
});
};
}
13 changes: 11 additions & 2 deletions tests/helpers/module-for-assert.js
Original file line number Diff line number Diff line change
@@ -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 = [];
Expand Down Expand Up @@ -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);
}
}
28 changes: 28 additions & 0 deletions tests/integration/assertion-test.js
Original file line number Diff line number Diff line change
@@ -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');
});
3 changes: 1 addition & 2 deletions tests/unit/assertion-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/deprecation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/run-loop-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/warning-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down

0 comments on commit b72dbd8

Please sign in to comment.