From f80fcff5be973fdeda78324ef7f217550b41348e Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Sun, 17 Dec 2017 21:59:01 -0500 Subject: [PATCH] Add validation for Ember.onerror in testing. --- addon-test-support/ember-qunit/index.js | 23 ++++++++++++++++ .../setup-ember-onerror-validation-test.js | 26 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 tests/unit/setup-ember-onerror-validation-test.js diff --git a/addon-test-support/ember-qunit/index.js b/addon-test-support/ember-qunit/index.js index 1feb3122..d7ce82b5 100644 --- a/addon-test-support/ember-qunit/index.js +++ b/addon-test-support/ember-qunit/index.js @@ -17,6 +17,7 @@ import { teardownRenderingContext, setupApplicationContext, teardownApplicationContext, + validateErrorHandler, } from '@ember/test-helpers'; export function setupTest(hooks, options) { @@ -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 @@ -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) { @@ -158,6 +177,10 @@ export function start(options = {}) { setupEmberTesting(); } + if (options.setupEmberOnerrorValidation !== false) { + setupEmberOnerrorValidation(); + } + if (options.startTests !== false) { startTests(); } diff --git a/tests/unit/setup-ember-onerror-validation-test.js b/tests/unit/setup-ember-onerror-validation-test.js new file mode 100644 index 00000000..ae4872e0 --- /dev/null +++ b/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(); +});