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
1 changed files with 13 additions and 25 deletions

View File

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