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

Add validation for Ember.onerror in testing. #304

Merged
merged 1 commit into from Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
});