Skip to content

Commit

Permalink
WIP: expectAssertion updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdiliberto committed Oct 26, 2017
1 parent 884baf3 commit 9ecbb46
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 79 deletions.
68 changes: 41 additions & 27 deletions addon-test-support/asserts/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -71,13 +73,25 @@ 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(() => {
if (Ember.Test.adapter.lastError) {
handleError(this, Ember.Test.adapter.lastError, matcher, isProductionBuild);
} else {
handleError(this, null, matcher, null);
}
})
.catch(() => handleError(this, error, matcher, isProductionBuild))
.finally(() => cleanup(origTestAdapter, origLoggerError));
} else {
handleError(this, null, matcher, null);
}


cleanup(origTestAdapter, origLoggerError);
return cleanup(origTestAdapter, origLoggerError);
};
}
43 changes: 0 additions & 43 deletions tests/helpers/module-for-assert.js

This file was deleted.

23 changes: 23 additions & 0 deletions tests/helpers/setup-assert-test.js
Original file line number Diff line number Diff line change
@@ -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();
});
}
18 changes: 9 additions & 9 deletions tests/integration/assertion-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ module('Assertion', function(hooks) {
}));
});

test('Check for assert', function(assert) {
assert.expectAssertion(() => {
render(hbs`{{x-assert-test}}`);
test('Check for assert', async function(assert) {
await assert.expectAssertion(() => {
return 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, 'properly catured assertion');
assert.ok(this.pushedResults[0].result, 'properly captured assertion');
});

test('Check for async assert', function(assert) {
test('Check for async assert', async function(assert) {
this.owner.register('component:x-assert-async-test', Ember.Component.extend({
init() {
this._super();
Expand All @@ -43,14 +43,14 @@ module('Assertion', function(hooks) {
}
}));

assert.expectAssertion(async function() {
await render(hbs`{{x-assert-test}}`);
}, /x-assert-test will always assert/);
await assert.expectAssertion(() => {
return render(hbs`{{x-assert-async-test}}`);
}, /x-assert-async-test will asynchronously assert/);

// Restore the asserts (removes the mocking)
this.restoreAsserts();

assert.ok(this.pushedResults[0].result, '`expectWarning` captured warning call');
assert.ok(this.pushedResults[0].result, 'properly captured an async assertion');
});

test('Does not log caught assertions', function(assert) {
Expand Down

0 comments on commit 9ecbb46

Please sign in to comment.