From 01b222e6c7d404cc526ec8f0f9b81e24260d1957 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sat, 1 Jun 2024 01:17:13 +0100 Subject: [PATCH] Core: Remove deprecated `QUnit.onError()` and `QUnit.onUnhandledRejection()` Deprecated since QUnit 2.17.0. Use `QUnit.onUncaughtException()` instead. Ref https://github.com/qunitjs/qunit/pull/1638. --- Gruntfile.js | 1 - .../extension/QUnit.onUncaughtException.md | 18 ++++- src/core.js | 9 --- src/core/onerror.js | 38 ---------- src/test.js | 11 ++- test/index.html | 1 - test/main/onError.js | 73 ------------------- test/mozjs.js | 1 - test/webWorker-worker.js | 1 - 9 files changed, 19 insertions(+), 134 deletions(-) delete mode 100644 src/core/onerror.js delete mode 100644 test/main/onError.js diff --git a/Gruntfile.js b/Gruntfile.js index 876b5955c..9c441f71d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -127,7 +127,6 @@ module.exports = function (grunt) { 'test/main/dump.js', 'test/main/each.js', 'test/main/modules.js', - 'test/main/onError.js', 'test/main/onUncaughtException.js', 'test/main/promise.js', 'test/main/setTimeout.js', diff --git a/docs/api/extension/QUnit.onUncaughtException.md b/docs/api/extension/QUnit.onUncaughtException.md index 0bbe50500..d1e5feaa8 100644 --- a/docs/api/extension/QUnit.onUncaughtException.md +++ b/docs/api/extension/QUnit.onUncaughtException.md @@ -1,7 +1,7 @@ --- layout: page-api title: QUnit.onUncaughtException() -excerpt: Handle a global error. +excerpt: Report a global error. groups: - extension version_added: "2.17.0" @@ -12,12 +12,22 @@ redirect_from: `QUnit.onUncaughtException( error )` -Handle a global error that should result in a failed test run. +Report a global error that should result in a failed test run. | name | description | |------|-------------| | `error` (any) | Usually an `Error` object, but any other thrown or rejected value may be given as well. | + +This method can be safely called at any time, including between or outside tests. It is designed for use by plugins and integration layers. + +In general, you should not use this method and instead throw an error. QUnit automatically finds and reports uncaught errors. The following are handled by default and should not be connected to `QUnit.onUncaughtException()` a second time: + +* HTML Runner: `window.onerror` +* HTML Runner: `window.addEventListener('unhandledrejection', …)` +* QUnit CLI: `process.on('unhandledRejection', …)` +* QUnit CLI: `process.on('uncaughtException', …)` + ## Examples ```js @@ -26,11 +36,11 @@ QUnit.onUncaughtException(error); ``` ```js -process.on('uncaughtException', QUnit.onUncaughtException); +process.on('unhandledExample', QUnit.onUncaughtException); ``` ```js -window.addEventListener('unhandledrejection', function (event) { +window.addEventListener('unhandledexample', function (event) { QUnit.onUncaughtException(event.reason); }); ``` diff --git a/src/core.js b/src/core.js index dbe664dd9..946ab9f8d 100644 --- a/src/core.js +++ b/src/core.js @@ -4,7 +4,6 @@ import equiv from './equiv'; import dump from './dump'; import { runSuite, module } from './module'; import Assert from './assert'; -import Logger from './logger'; import Test, { test, pushFailure } from './test'; import exportQUnit from './export'; import reporters from './reporters'; @@ -17,7 +16,6 @@ import { sourceFromStacktrace } from './core/stacktrace'; import ProcessingQueue from './core/processing-queue'; import { on, emit } from './events'; -import onWindowError from './core/onerror'; import onUncaughtException from './core/on-uncaught-exception'; import diff from './core/diff'; @@ -50,7 +48,6 @@ extend(QUnit, { is, objectType, on, - onError: onWindowError, onUncaughtException, pushFailure, @@ -94,12 +91,6 @@ extend(QUnit, { } }, - onUnhandledRejection: function (reason) { - Logger.warn('QUnit.onUnhandledRejection is deprecated and will be removed in QUnit 3.0.' + - ' Please use QUnit.onUncaughtException instead.'); - onUncaughtException(reason); - }, - stack: function (offset) { offset = (offset || 0) + 2; return sourceFromStacktrace(offset); diff --git a/src/core/onerror.js b/src/core/onerror.js deleted file mode 100644 index 131ff7b4e..000000000 --- a/src/core/onerror.js +++ /dev/null @@ -1,38 +0,0 @@ -import Logger from '../logger'; -import config from './config'; -import onUncaughtException from './on-uncaught-exception'; - -/** - * Handle a window.onerror error. - * - * If there is a current test that sets the internal `ignoreGlobalErrors` field - * (such as during `assert.throws()`), then the error is ignored and native - * error reporting is suppressed as well. This is because in browsers, an error - * can sometimes end up in `window.onerror` instead of in the local try/catch. - * This ignoring of errors does not apply to our general onUncaughtException - * method, nor to our `unhandledRejection` handlers, as those are not meant - * to receive an "expected" error during `assert.throws()`. - * - * @see - * @deprecated since 2.17.0 Use QUnit.onUncaughtException instead. - * @param {Object} details - * @param {string} details.message - * @param {string} details.fileName - * @param {number} details.lineNumber - * @param {string|undefined} [details.stacktrace] - * @return {bool} True if native error reporting should be suppressed. - */ -export default function onWindowError (details) { - Logger.warn('QUnit.onError is deprecated and will be removed in QUnit 3.0.' + - ' Please use QUnit.onUncaughtException instead.'); - - if (config.current && config.current.ignoreGlobalErrors) { - return true; - } - - const err = new Error(details.message); - err.stack = details.stacktrace || details.fileName + ':' + details.lineNumber; - onUncaughtException(err); - - return false; -} diff --git a/src/test.js b/src/test.js index 21c9cbda8..4114ee79b 100644 --- a/src/test.js +++ b/src/test.js @@ -57,15 +57,14 @@ export default function Test (settings) { } // Queuing a late test after the run has ended is not allowed. - // This was once supported for internal use by QUnit.onError(). + // This was once supported for internal use by QUnit.onUncaughtException(), + // to render a "global error" if the uncaught error happened outside a test + // and after the runEnd event. This was unstable and could be missed by CI. + // (Meaning the CI would pass despite the late-failing test). // Ref https://github.com/qunitjs/qunit/issues/1377 if (config.pq.finished) { - // Using this for anything other than onError(), such as testing in QUnit.done(), - // is unstable and will likely result in the added tests being ignored by CI. - // (Meaning the CI passes irregardless of the added tests). - // // TODO: Make this an error in QUnit 3.0 - // throw new Error( "Unexpected test after runEnd" ); + // throw new Error( 'Unexpected test after runEnd. To report errors, consider calling QUnit.onUncaughtException() instead.' ); Logger.warn('Unexpected test after runEnd. This is unstable and will fail in QUnit 3.0.'); return; } diff --git a/test/index.html b/test/index.html index 0a6d7232e..222ef76e9 100644 --- a/test/index.html +++ b/test/index.html @@ -14,7 +14,6 @@ - diff --git a/test/main/onError.js b/test/main/onError.js deleted file mode 100644 index 0e75235ae..000000000 --- a/test/main/onError.js +++ /dev/null @@ -1,73 +0,0 @@ -QUnit.module('QUnit.onError', function () { - QUnit.test('inside a test', function (assert) { - assert.expect(2); - - var original = assert.pushResult; - var pushed = null; - assert.pushResult = function (resultInfo) { - pushed = resultInfo; - }; - - var suppressed = QUnit.onError({ - message: 'Error message', - fileName: 'filePath.js', - lineNumber: 1 - }); - - assert.pushResult = original; - - assert.strictEqual(suppressed, false, 'onError should allow other error handlers to run'); - assert.propEqual(pushed, { - result: false, - message: 'global failure: Error: Error message', - source: 'filePath.js:1' - }, 'pushed result'); - }); - - QUnit.test('use stacktrace argument', function (assert) { - assert.expect(2); - - var original = assert.pushResult; - var pushed = null; - assert.pushResult = function (result) { - pushed = result; - assert.pushResult = original; - }; - - var suppressed = QUnit.onError({ - message: 'Error message', - fileName: 'filePath.js', - lineNumber: 1, - stacktrace: 'DummyError\nfilePath.js:1 foo()\nfilePath.js:2 bar()' - }); - - assert.strictEqual(suppressed, false, 'onError should allow other error handlers to run'); - assert.propEqual(pushed, { - result: false, - message: 'global failure: Error: Error message', - source: 'DummyError\nfilePath.js:1 foo()\nfilePath.js:2 bar()' - }, 'pushed result'); - }); - - QUnit.test('ignore failure when ignoreGlobalErrors is enabled', function (assert) { - assert.expect(2); - - var original = assert.pushResult; - var pushed = null; - assert.pushResult = function (result) { - pushed = result; - }; - assert.test.ignoreGlobalErrors = true; - - var suppressed = QUnit.onError({ - message: 'Error message', - fileName: 'filePath.js', - lineNumber: 1 - }); - - assert.pushResult = original; - - assert.strictEqual(pushed, null, 'No error should be pushed'); - assert.strictEqual(suppressed, true, 'onError should not allow other error handlers to run'); - }); -}); diff --git a/test/mozjs.js b/test/mozjs.js index e1de1cead..f290613d4 100644 --- a/test/mozjs.js +++ b/test/mozjs.js @@ -20,7 +20,6 @@ loadRelativeToScript('../test/main/deepEqual.js'); loadRelativeToScript('../test/main/dump.js'); loadRelativeToScript('../test/main/each.js'); // loadRelativeToScript( "../test/main/modules.js" ); // Requires setTimeout -loadRelativeToScript('../test/main/onError.js'); loadRelativeToScript('../test/main/onUncaughtException.js'); loadRelativeToScript('../test/main/promise.js'); loadRelativeToScript('../test/main/setTimeout.js'); diff --git a/test/webWorker-worker.js b/test/webWorker-worker.js index fbe667d7a..2433143c3 100644 --- a/test/webWorker-worker.js +++ b/test/webWorker-worker.js @@ -14,7 +14,6 @@ importScripts( 'main/dump.js', 'main/each.js', 'main/modules.js', - 'main/onError.js', 'main/onUncaughtException.js', 'main/promise.js', 'main/setTimeout.js',