From 1d70169640400a604e35a273621eb2a455b15c0a Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Mon, 18 Dec 2017 08:54:02 -0500 Subject: [PATCH] Add failing tests for unhandled promise rejections. --- addon-test-support/ember-qunit/adapter.js | 2 +- tests/unit/unhandled-rejection-test.js | 45 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/unit/unhandled-rejection-test.js diff --git a/addon-test-support/ember-qunit/adapter.js b/addon-test-support/ember-qunit/adapter.js index fd1d919a..6ef761c6 100644 --- a/addon-test-support/ember-qunit/adapter.js +++ b/addon-test-support/ember-qunit/adapter.js @@ -15,7 +15,7 @@ function unhandledRejectionAssertion(current, error) { source = 'unknown source'; } - current.pushResult({ + current.assert.pushResult({ result: false, actual: false, expected: true, diff --git a/tests/unit/unhandled-rejection-test.js b/tests/unit/unhandled-rejection-test.js new file mode 100644 index 00000000..03071d84 --- /dev/null +++ b/tests/unit/unhandled-rejection-test.js @@ -0,0 +1,45 @@ +import { Promise as RSVPPromise } from 'rsvp'; +import { module, test } from 'qunit'; + +const HAS_NATIVE_PROMISE = typeof Promise !== 'undefined'; +const HAS_UNHANDLED_REJECTION_HANDLER = 'onunhandledrejection' in window; + +module('unhandle promise rejections', 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); + }; + }); + + test('RSVP promises cause an unhandled rejection', function(assert) { + let done = assert.async(); + + // ensure we do not exit this test until the assertion has happened + setTimeout(done, 10); + + new RSVPPromise(resolve => { + setTimeout(resolve); + }).then(function() { + throw new Error('whoops!'); + }); + }); + + if (HAS_NATIVE_PROMISE && HAS_UNHANDLED_REJECTION_HANDLER) { + test('native promises cause an unhandled rejection', function(assert) { + let done = assert.async(); + + // ensure we do not exit this test until the assertion has happened + setTimeout(done, 10); + + new self.Promise(resolve => { + setTimeout(resolve); + }).then(function() { + throw new Error('whoops!'); + }); + }); + } +});