Skip to content

Commit

Permalink
harness/asyncHelpers.js: Refactor assert.throwsAsync to only accept a…
Browse files Browse the repository at this point in the history
… callable
  • Loading branch information
cjtenny authored and ptomato committed Feb 21, 2023
1 parent 4bd0545 commit d810c68
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
20 changes: 5 additions & 15 deletions harness/asyncHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,16 @@ function asyncTest(testFunc) {
}
}

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

try {
Expand Down
4 changes: 3 additions & 1 deletion test/harness/asyncHelpers-throwsAsync-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ includes: [asyncHelpers.js]
function MyError() {}

(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);
await p;
})().then($DONE, $DONE);
4 changes: 3 additions & 1 deletion test/harness/asyncHelpers-throwsAsync-incorrect-ctor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ includes: [asyncHelpers.js]
(async function () {
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);
try {
await p;
Expand Down

0 comments on commit d810c68

Please sign in to comment.