harness/asyncHelpers.js: Further reduce the size of assert.throwsAsync

This commit is contained in:
Richard Gibson 2024-05-19 13:51:39 -04:00
parent fa3d0246e7
commit f83c1b9387

View File

@ -51,59 +51,47 @@ function asyncTest(testFunc) {
assert.throwsAsync = function (expectedErrorConstructor, func, message) { assert.throwsAsync = function (expectedErrorConstructor, func, message) {
return new Promise(function (resolve) { return new Promise(function (resolve) {
var expectedName = expectedErrorConstructor.name; var expectedName = expectedErrorConstructor.name;
var expectation = "Expected a " + expectedName + " to be thrown asynchronously";
var fail = function (detail) { var fail = function (detail) {
if (message === undefined) { if (message === undefined) {
throw new Test262Error(detail); throw new Test262Error(detail);
} }
throw new Test262Error(message + " " + detail); throw new Test262Error(message + " " + detail);
}; };
var innerThenable; var res;
if (typeof func !== "function") { if (typeof func !== "function") {
fail("assert.throwsAsync called with an argument that is not a function"); fail("assert.throwsAsync called with an argument that is not a function");
} }
try { try {
innerThenable = func(); res = func();
} catch (thrown) { } catch (thrown) {
fail("Expected a " + fail(expectation + " but the function threw synchronously");
expectedName +
" to be thrown asynchronously but the function threw synchronously");
} }
if ( if (res === null || typeof res !== "object" || typeof res.then !== "function") {
innerThenable === null || fail(expectation + " but result was not a thenable");
typeof innerThenable !== "object" ||
typeof innerThenable.then !== "function"
) {
fail("Expected to obtain a promise that would reject with a " +
expectedName +
" but result was not a thenable");
} }
try { try {
resolve(innerThenable.then( resolve(res.then(
function () { function () {
fail("Expected a " + fail(expectation + " but no exception was thrown at all");
expectedName +
" to be thrown asynchronously but no exception was thrown at all");
}, },
function (thrown) { function (thrown) {
var actualName; var actualName;
if (typeof thrown !== "object" || thrown === null) { if (thrown === null || typeof thrown !== "object") {
fail("Thrown value was not an object!"); fail(expectation + " but thrown value was not an object");
} else if (thrown.constructor !== expectedErrorConstructor) { } else if (thrown.constructor !== expectedErrorConstructor) {
actualName = thrown.constructor.name; actualName = thrown.constructor.name;
if (expectedName === actualName) { if (expectedName === actualName) {
fail("Expected a " + fail(expectation +
expectedName +
" but got a different error constructor with the same name"); " but got a different error constructor with the same name");
} }
fail("Expected a " + expectedName + " but got a " + actualName); fail(expectation + " but got a " + actualName);
} }
} }
)); ));
} catch (thrown) { } catch (thrown) {
fail("Expected a " + fail(expectation + " but .then threw synchronously");
expectedName +
" to be thrown asynchronously but .then threw synchronously");
} }
}); });
}; };