Skip to content

Commit

Permalink
Add validation for Ember.onerror in testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Dec 18, 2017
1 parent f1d1469 commit f80fcff
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
23 changes: 23 additions & 0 deletions addon-test-support/ember-qunit/index.js
Expand Up @@ -17,6 +17,7 @@ import {
teardownRenderingContext,
setupApplicationContext,
teardownApplicationContext,
validateErrorHandler,
} from '@ember/test-helpers';

export function setupTest(hooks, options) {
Expand Down Expand Up @@ -127,6 +128,22 @@ export function setupEmberTesting() {
});
}

/**
Ensures that `Ember.onerror` (if present) is properly configured to re-throw
errors that occur while `Ember.testing` is `true`.
*/
export function setupEmberOnerrorValidation() {
QUnit.module('ember-qunit: Ember.onerror validation', function() {
QUnit.test('Ember.onerror is functioning properly', function(assert) {
let result = validateErrorHandler();
assert.ok(
result.isValid,
`Ember.onerror handler with invalid testing behavior detected. An Ember.onerror handler _must_ rethrow exceptions when \`Ember.testing\` is \`true\` or the test suite is unreliable. See https://git.io/vbine for more details.`
);
});
});
}

/**
@method start
@param {Object} [options] Options to be used for enabling/disabling behaviors
Expand All @@ -140,6 +157,8 @@ export function setupEmberTesting() {
@param {Boolean} [options.setupEmberTesting] `false` opts out of the
default behavior of setting `Ember.testing` to `true` before all tests and
back to `false` after each test will.
@param {Boolean} [options.setupEmberOnerrorValidation] If `false` validation
of `Ember.onerror` will be disabled.
*/
export function start(options = {}) {
if (options.loadTests !== false) {
Expand All @@ -158,6 +177,10 @@ export function start(options = {}) {
setupEmberTesting();
}

if (options.setupEmberOnerrorValidation !== false) {
setupEmberOnerrorValidation();
}

if (options.startTests !== false) {
startTests();
}
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/setup-ember-onerror-validation-test.js
@@ -0,0 +1,26 @@
import Ember from 'ember';
import { module } from 'qunit';
import { setupEmberOnerrorValidation } from 'ember-qunit';

module('setupEmberOnerrorValidation', function(hooks) {
hooks.beforeEach(function(assert) {
let originalPushResult = assert.pushResult;
assert.pushResult = function(resultInfo) {
// Inverts the result so we can test failing assertions
resultInfo.result = !resultInfo.result;
resultInfo.message = `Failed: ${resultInfo.message}`;
originalPushResult(resultInfo);
};

this.originalEmberOnerror = Ember.onerror;
Ember.onerror = function() {
// intentionally swallowing here
};
});

hooks.afterEach(function() {
Ember.onerror = this.originalEmberOnerror;
});

setupEmberOnerrorValidation();
});

0 comments on commit f80fcff

Please sign in to comment.