Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 50 additions & 31 deletions addon-test-support/asserts/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,73 @@ let TestAdapter = QUnitAdapter.extend({
}
});

export default function() {
let isProductionBuild = (function() {
try {
Ember.assert('fails in debug builds');
} catch(e) {
return false;
}
let isProductionBuild = (function() {
try {
Ember.assert('fails in debug builds');
} catch(e) {
return false;
}

return true;
})();
return true;
})();

export default function() {
QUnit.assert.expectAssertion = function(cb, matcher) {
let assertion = this;
// 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;
let ret;
try {
cb();
ret = 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));

if (isProductionBuild) {
this.pushResult({
result: true,
actual: null,
expected: null,
message: 'Assertions are disabled in production builds.'
});
if (typeof ret === 'object' && ret !== null && typeof ret.then === 'function') {
// handle async
return Ember.RSVP.Promise((resolve, reject) => {
if (error) reject(error)
else resolve(ret);
}).catch(reason => {
handleError(assertion, reason, matcher)
}).finally(() => cleanup(origTestAdapter));;
} 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'
});
handleError(assertion, error, matcher);
cleanup(origTestAdapter);
}
};
}

function handleError(assertion, error, matcher) {
let isEmberError = error instanceof Ember.Error;
let matches = Boolean(isEmberError && error.message.match(matcher));

// Cleanup the test adapter and restore the original.
Ember.run(() => {
Ember.Test.adapter.destroy();
Ember.Test.adapter = origTestAdapter;
if (isProductionBuild) {
assertion.pushResult({
result: true,
actual: null,
expected: null,
message: 'Assertions are disabled in production builds.'
});
};
} else {
assertion.pushResult({
result: isEmberError && matches,
actual: error && error.message,
expected: matcher,
message: matcher ? 'Ember.assert matched specific message' : 'Ember.assert called with any message'
});
}
}

function cleanup(origTestAdapter) {
// Cleanup the test adapter and restore the original.
Ember.run(() => {
Ember.Test.adapter.destroy();
Ember.Test.adapter = origTestAdapter;
});
}