diff --git a/Gruntfile.js b/Gruntfile.js index 1892c423a..38f4db48f 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -123,6 +123,7 @@ module.exports = function (grunt) { 'test/main/assert-step.js', 'test/main/assert-timeout.js', 'test/main/async.js', + 'test/main/browser-runner.js', 'test/main/deepEqual.js', 'test/main/diff.js', 'test/main/dump.js', diff --git a/test/index-es5.html b/test/index-es5.html index 0da3de537..b7ea181fc 100644 --- a/test/index-es5.html +++ b/test/index-es5.html @@ -15,6 +15,7 @@ + diff --git a/test/index-xhtml.xhtml b/test/index-xhtml.xhtml index 7e3c29404..e566f1db2 100644 --- a/test/index-xhtml.xhtml +++ b/test/index-xhtml.xhtml @@ -27,6 +27,7 @@ + @@ -43,8 +44,6 @@ - -
diff --git a/test/index.html b/test/index.html index e041175b9..c0807c405 100644 --- a/test/index.html +++ b/test/index.html @@ -15,6 +15,7 @@ + @@ -31,8 +32,6 @@ - -
diff --git a/test/main/browser-runner.js b/test/main/browser-runner.js new file mode 100644 index 000000000..2e25b4317 --- /dev/null +++ b/test/main/browser-runner.js @@ -0,0 +1,38 @@ +QUnit.module('browser-runner', function () { + /* global window, Promise, setTimeout */ + var HAS_UNHANDLED_REJECTION_HANDLER = (typeof window !== 'undefined' && 'onunhandledrejection' in window && typeof Promise === 'function'); + + // Skip in non-browser and unsupporting browsers + QUnit.module.if('onunhandledrejection', HAS_UNHANDLED_REJECTION_HANDLER, function (hooks) { + // TODO: Once we run browser tests with QTap, remove this hack + // and instead write expected failures in .tap.txt snapshots. + hooks.beforeEach(function (assert) { + var test = this; + var original = assert.pushResult; + assert.pushResult = function (resultInfo) { + if (test.expectedFailure && test.expectedFailure === resultInfo.message) { + // Restore + assert.pushResult = original; + test.expectedFailure = null; + // Inverts the result of the (one) expected failure + resultInfo.result = !resultInfo.result; + } + + return original.call(assert, resultInfo); + }; + }); + + QUnit.test('example', function (assert) { + var done = assert.async(); + + this.expectedFailure = 'global failure: Error: Error thrown in non-returned promise!'; + + // eslint-disable-next-line compat/compat -- Checked + Promise.resolve().then(function () { + // prevent test from exiting before unhandled rejection fires + setTimeout(done, 20); + throw new Error('Error thrown in non-returned promise!'); + }); + }); + }); +}); diff --git a/test/mozjs.js b/test/mozjs.js index 2371dbc75..4c9f83a4f 100644 --- a/test/mozjs.js +++ b/test/mozjs.js @@ -17,6 +17,7 @@ loadRelativeToScript('../test/main/assert-es6.js'); loadRelativeToScript('../test/main/assert-step.js'); // loadRelativeToScript( "../test/main/assert-timeout.js" ); // Requires setTimeout // loadRelativeToScript( "../test/main/async.js" ); // Requires setTimeout +loadRelativeToScript('../test/main/browser-runner.js'); loadRelativeToScript('../test/main/deepEqual.js'); loadRelativeToScript('../test/main/diff.js'); loadRelativeToScript('../test/main/dump.js'); diff --git a/test/mozjs.mjs b/test/mozjs.mjs index 25dee4ad2..58d540178 100644 --- a/test/mozjs.mjs +++ b/test/mozjs.mjs @@ -6,6 +6,7 @@ import '../test/main/assert-es6.js'; import '../test/main/assert-step.js'; // import "../test/main/assert-timeout.js"; // Requires setTimeout // import "../test/main/async.js"; // Requires setTimeout +import '../test/main/browser-runner.js'; import '../test/main/deepEqual.js'; import '../test/main/diff.js'; import '../test/main/dump.js'; diff --git a/test/reporter-html/unhandled-rejection.js b/test/reporter-html/unhandled-rejection.js deleted file mode 100644 index e2ce40484..000000000 --- a/test/reporter-html/unhandled-rejection.js +++ /dev/null @@ -1,30 +0,0 @@ -/* eslint-env browser */ -/* global Promise */ - -// Detect if the current browser supports `onunhandledrejection` -// (avoiding errors in browsers without the capability) -var HAS_UNHANDLED_REJECTION_HANDLER = 'onunhandledrejection' in window; - -if (HAS_UNHANDLED_REJECTION_HANDLER) { - QUnit.module('Unhandled Rejections inside test context', function (hooks) { - hooks.beforeEach(function (assert) { - var originalPushResult = assert.pushResult; - assert.pushResult = function (resultInfo) { - // Invert the result so we can test failing assertions - resultInfo.result = !resultInfo.result; - originalPushResult.call(this, resultInfo); - }; - }); - - QUnit.test('test passes just fine, but has a rejected promise', function (assert) { - var done = assert.async(); - - Promise.resolve().then(function () { - throw new Error('Error thrown in non-returned promise!'); - }); - - // prevent test from exiting before unhandled rejection fires - setTimeout(done, 10); - }); - }); -} diff --git a/test/webWorker-worker.js b/test/webWorker-worker.js index cc672258a..69e92a5da 100644 --- a/test/webWorker-worker.js +++ b/test/webWorker-worker.js @@ -11,6 +11,7 @@ importScripts( 'main/assert-step.js', 'main/assert-timeout.js', 'main/async.js', + 'main/browser-runner.js', 'main/deepEqual.js', 'main/diff.js', 'main/dump.js',