harness/asyncHelpers.js: Refactor assert.throwsAsync to only accept a callable

This commit is contained in:
Cam Tenny 2022-12-05 15:21:05 -08:00 committed by Philip Chimento
parent 4bd0545b29
commit d810c684ae
3 changed files with 12 additions and 18 deletions

View File

@ -34,20 +34,16 @@ function asyncTest(testFunc) {
} }
} }
assert.throwsAsync = async function ( assert.throwsAsync = async function (expectedErrorConstructor, func, message) {
expectedErrorConstructor,
funcOrThenable,
message
) {
var innerThenable; var innerThenable;
if (message === undefined) { if (message === undefined) {
message = ""; message = "";
} else { } else {
message += " "; message += " ";
} }
if (typeof funcOrThenable === "function") { if (typeof func === "function") {
try { try {
innerThenable = funcOrThenable(); innerThenable = func();
if ( if (
innerThenable === null || innerThenable === null ||
typeof innerThenable !== "object" || typeof innerThenable !== "object" ||
@ -66,16 +62,10 @@ assert.throwsAsync = async function (
" to be thrown asynchronously but an exception was thrown synchronously while obtaining the inner promise"; " to be thrown asynchronously but an exception was thrown synchronously while obtaining the inner promise";
throw new Test262Error(message); throw new Test262Error(message);
} }
} else if (
funcOrThenable === null ||
typeof funcOrThenable !== "object" ||
typeof funcOrThenable.then !== "function"
) {
message +=
"assert.throwsAsync called with an argument that is neither a function nor a thenable";
throw new Test262Error(message);
} else { } else {
innerThenable = funcOrThenable; message +=
"assert.throwsAsync called with an argument that is not a function";
throw new Test262Error(message);
} }
try { try {

View File

@ -11,7 +11,9 @@ includes: [asyncHelpers.js]
function MyError() {} function MyError() {}
(async function () { (async function () {
const p = assert.throwsAsync(MyError, Promise.reject(new MyError())); const p = assert.throwsAsync(MyError, function () {
return Promise.reject(new MyError());
});
assert(p instanceof Promise); assert(p instanceof Promise);
await p; await p;
})().then($DONE, $DONE); })().then($DONE, $DONE);

View File

@ -11,7 +11,9 @@ includes: [asyncHelpers.js]
(async function () { (async function () {
var caught = false; var caught = false;
const p = assert.throwsAsync(Error, Promise.reject(new TypeError())); const p = assert.throwsAsync(Error, function () {
return Promise.reject(new TypeError());
});
assert(p instanceof Promise); assert(p instanceof Promise);
try { try {
await p; await p;