Skip to content

Commit

Permalink
[BUGFIX lts] Allow Ember.Test.adapter to opt out of exception handl…
Browse files Browse the repository at this point in the history
…ing.

In the majority of testing frameworks (e.g. Mocha, QUnit, Jasmine) a
global error handler is attached to `window.onerror`. When an uncaught
exception is thrown, this global handler is invoked and the test suite
will properly fail.

Requiring that the provided test adapter provide an exception method
is strictly worse in nearly all cases than allowing the error to be
bubbled upwards and caught/handled by either `Ember.onerror` (if
present) or `window.onerror` (generally from the test framework).

This commit makes `Ember.Test.adapter.exception` optional. When not
present, errors during testing will be handled by existing "normal"
means.
  • Loading branch information
rwjblue committed Nov 28, 2017
1 parent 1d37ba6 commit 80f7d6b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/ember-testing/lib/test/adapter.js
Expand Up @@ -8,7 +8,7 @@ export function getAdapter() {

export function setAdapter(value) {
adapter = value;
if (value) {
if (value && typeof value.exception === 'function') {
setDispatchOverride(adapterDispatch);
} else {
setDispatchOverride(null);
Expand Down
51 changes: 51 additions & 0 deletions packages/ember/tests/error_handler_test.js
Expand Up @@ -397,6 +397,57 @@ function generateRSVPErrorHandlingTests(message, generatePromise, timeout = 10)
});

if (DEBUG) {
test(`${message} when Ember.Test.adapter without \`exception\` method is present - rsvp`, function(assert) {
assert.expect(1);

let thrown = new Error('the error');
Ember.Test.adapter = Ember.Test.QUnitAdapter.create({
exception: undefined
});

window.onerror = function(message) {
assert.pushResult({
result: /the error/.test(message),
actual: message,
expected: 'to include `the error`',
message: 'error should bubble out to window.onerror, and therefore fail tests (due to QUnit implementing window.onerror)'
});

// prevent "bubbling" and therefore failing the test
return true;
};

generatePromise(thrown);

// RSVP.Promise's are configured to settle within the run loop, this
// ensures that run loop has completed
return new Ember.RSVP.Promise((resolve) => setTimeout(resolve, timeout));
});

test(`${message} when both Ember.onerror and Ember.Test.adapter without \`exception\` method are present - rsvp`, function(assert) {
assert.expect(1);

let thrown = new Error('the error');
Ember.Test.adapter = Ember.Test.QUnitAdapter.create({
exception: undefined
});

Ember.onerror = function(error) {
assert.pushResult({
result: /the error/.test(error.message),
actual: error.message,
expected: 'to include `the error`',
message: 'error should bubble out to window.onerror, and therefore fail tests (due to QUnit implementing window.onerror)'
});
};

generatePromise(thrown);

// RSVP.Promise's are configured to settle within the run loop, this
// ensures that run loop has completed
return new Ember.RSVP.Promise((resolve) => setTimeout(resolve, timeout));
});

test(`${message} when Ember.Test.adapter is present - rsvp`, function(assert) {
assert.expect(1);

Expand Down

0 comments on commit 80f7d6b

Please sign in to comment.