Skip to content

Commit f83c1b9

Browse files
committed
harness/asyncHelpers.js: Further reduce the size of assert.throwsAsync
1 parent fa3d024 commit f83c1b9

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

harness/asyncHelpers.js

+13-25
Original file line numberDiff line numberDiff line change
@@ -51,59 +51,47 @@ function asyncTest(testFunc) {
5151
assert.throwsAsync = function (expectedErrorConstructor, func, message) {
5252
return new Promise(function (resolve) {
5353
var expectedName = expectedErrorConstructor.name;
54+
var expectation = "Expected a " + expectedName + " to be thrown asynchronously";
5455
var fail = function (detail) {
5556
if (message === undefined) {
5657
throw new Test262Error(detail);
5758
}
5859
throw new Test262Error(message + " " + detail);
5960
};
60-
var innerThenable;
61+
var res;
6162
if (typeof func !== "function") {
6263
fail("assert.throwsAsync called with an argument that is not a function");
6364
}
6465
try {
65-
innerThenable = func();
66+
res = func();
6667
} catch (thrown) {
67-
fail("Expected a " +
68-
expectedName +
69-
" to be thrown asynchronously but the function threw synchronously");
68+
fail(expectation + " but the function threw synchronously");
7069
}
71-
if (
72-
innerThenable === null ||
73-
typeof innerThenable !== "object" ||
74-
typeof innerThenable.then !== "function"
75-
) {
76-
fail("Expected to obtain a promise that would reject with a " +
77-
expectedName +
78-
" but result was not a thenable");
70+
if (res === null || typeof res !== "object" || typeof res.then !== "function") {
71+
fail(expectation + " but result was not a thenable");
7972
}
8073

8174
try {
82-
resolve(innerThenable.then(
75+
resolve(res.then(
8376
function () {
84-
fail("Expected a " +
85-
expectedName +
86-
" to be thrown asynchronously but no exception was thrown at all");
77+
fail(expectation + " but no exception was thrown at all");
8778
},
8879
function (thrown) {
8980
var actualName;
90-
if (typeof thrown !== "object" || thrown === null) {
91-
fail("Thrown value was not an object!");
81+
if (thrown === null || typeof thrown !== "object") {
82+
fail(expectation + " but thrown value was not an object");
9283
} else if (thrown.constructor !== expectedErrorConstructor) {
9384
actualName = thrown.constructor.name;
9485
if (expectedName === actualName) {
95-
fail("Expected a " +
96-
expectedName +
86+
fail(expectation +
9787
" but got a different error constructor with the same name");
9888
}
99-
fail("Expected a " + expectedName + " but got a " + actualName);
89+
fail(expectation + " but got a " + actualName);
10090
}
10191
}
10292
));
10393
} catch (thrown) {
104-
fail("Expected a " +
105-
expectedName +
106-
" to be thrown asynchronously but .then threw synchronously");
94+
fail(expectation + " but .then threw synchronously");
10795
}
10896
});
10997
};

0 commit comments

Comments
 (0)