Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP async support #10

Closed
wants to merge 1 commit into from
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
WIP async support
stefanpenner committed Apr 28, 2017
commit a9f01cf7cbddfa9a85855b364ed311e1bb5558ee
81 changes: 50 additions & 31 deletions addon-test-support/asserts/assertion.js
Original file line number Diff line number Diff line change
@@ -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;
});
}