From 5217ef396e3e55cf0a48347710b0807e1b39da52 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 17 May 2024 09:13:21 -0700 Subject: [PATCH] throwsAsync, `Promise.try`: avoid unneeded syntax Followup to #4086 --- harness/asyncHelpers.js | 144 ++++++++++++++------------- test/built-ins/Promise/try/throws.js | 4 +- 2 files changed, 75 insertions(+), 73 deletions(-) diff --git a/harness/asyncHelpers.js b/harness/asyncHelpers.js index 904b50aa98..c8e58457fc 100644 --- a/harness/asyncHelpers.js +++ b/harness/asyncHelpers.js @@ -28,84 +28,86 @@ function asyncTest(testFunc) { } } -assert.throwsAsync = async function (expectedErrorConstructor, func, message) { - var innerThenable; - if (message === undefined) { - message = ""; - } else { - message += " "; - } - if (typeof func === "function") { - try { - innerThenable = func(); - if ( - innerThenable === null || - typeof innerThenable !== "object" || - typeof innerThenable.then !== "function" - ) { - message += - "Expected to obtain an inner promise that would reject with a" + - expectedErrorConstructor.name + - " but result was not a thenable"; - throw new Test262Error(message); - } - } catch (thrown) { - message += - "Expected a " + - expectedErrorConstructor.name + - " to be thrown asynchronously but an exception was thrown synchronously while obtaining the inner promise"; - throw new Test262Error(message); +assert.throwsAsync = function (expectedErrorConstructor, func, message) { + return new Promise(function (resolve) { + var innerThenable; + if (message === undefined) { + message = ""; + } else { + message += " "; } - } else { - message += - "assert.throwsAsync called with an argument that is not a function"; - throw new Test262Error(message); - } - - try { - return innerThenable.then( - function () { + if (typeof func === "function") { + try { + innerThenable = func(); + if ( + innerThenable === null || + typeof innerThenable !== "object" || + typeof innerThenable.then !== "function" + ) { + message += + "Expected to obtain an inner promise that would reject with a" + + expectedErrorConstructor.name + + " but result was not a thenable"; + throw new Test262Error(message); + } + } catch (thrown) { message += "Expected a " + expectedErrorConstructor.name + - " to be thrown asynchronously but no exception was thrown at all"; + " to be thrown asynchronously but an exception was thrown synchronously while obtaining the inner promise"; throw new Test262Error(message); - }, - function (thrown) { - var expectedName, actualName; - if (typeof thrown !== "object" || thrown === null) { - message += "Thrown value was not an object!"; - throw new Test262Error(message); - } else if (thrown.constructor !== expectedErrorConstructor) { - expectedName = expectedErrorConstructor.name; - actualName = thrown.constructor.name; - if (expectedName === actualName) { - message += - "Expected a " + - expectedName + - " but got a different error constructor with the same name"; - } else { - message += - "Expected a " + expectedName + " but got a " + actualName; - } - throw new Test262Error(message); - } } - ); - } catch (thrown) { - if (typeof thrown !== "object" || thrown === null) { - message += - "Expected a " + - expectedErrorConstructor.name + - " to be thrown asynchronously but innerThenable synchronously threw a value that was not an object "; } else { message += - "Expected a " + - expectedErrorConstructor.name + - " to be thrown asynchronously but a " + - thrown.constructor.name + - " was thrown synchronously"; + "assert.throwsAsync called with an argument that is not a function"; + throw new Test262Error(message); } - throw new Test262Error(message); - } + + try { + resolve(innerThenable.then( + function () { + message += + "Expected a " + + expectedErrorConstructor.name + + " to be thrown asynchronously but no exception was thrown at all"; + throw new Test262Error(message); + }, + function (thrown) { + var expectedName, actualName; + if (typeof thrown !== "object" || thrown === null) { + message += "Thrown value was not an object!"; + throw new Test262Error(message); + } else if (thrown.constructor !== expectedErrorConstructor) { + expectedName = expectedErrorConstructor.name; + actualName = thrown.constructor.name; + if (expectedName === actualName) { + message += + "Expected a " + + expectedName + + " but got a different error constructor with the same name"; + } else { + message += + "Expected a " + expectedName + " but got a " + actualName; + } + throw new Test262Error(message); + } + } + )); + } catch (thrown) { + if (typeof thrown !== "object" || thrown === null) { + message += + "Expected a " + + expectedErrorConstructor.name + + " to be thrown asynchronously but innerThenable synchronously threw a value that was not an object "; + } else { + message += + "Expected a " + + expectedErrorConstructor.name + + " to be thrown asynchronously but a " + + thrown.constructor.name + + " was thrown synchronously"; + } + throw new Test262Error(message); + } + }); }; diff --git a/test/built-ins/Promise/try/throws.js b/test/built-ins/Promise/try/throws.js index 5590fab6e1..4ec3a3225d 100644 --- a/test/built-ins/Promise/try/throws.js +++ b/test/built-ins/Promise/try/throws.js @@ -9,8 +9,8 @@ flags: [async] includes: [asyncHelpers.js] ---*/ -asyncTest(async function() { - await assert.throwsAsync( +asyncTest(function () { + return assert.throwsAsync( Test262Error, function () { Promise.try(function () { throw new Test262Error(); })