Skip to content

Commit 7a91671

Browse files
authored
Cleanup asyncHelpers (merge #4091)
2 parents 5492929 + f83c1b9 commit 7a91671

File tree

2 files changed

+51
-66
lines changed

2 files changed

+51
-66
lines changed

CONTRIBUTING.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ This key is for boolean properties associated with the test.
209209
- **raw** - execute the test without any modification (no harness files will be
210210
included); necessary to test the behavior of directive prologue; implies
211211
`noStrict`
212-
- **async** - defer interpretation of test results until after the invocation
213-
of the global `$DONE` function
212+
- **async** - defer interpretation of test results until settlement of an
213+
`asyncTest` callback promise or manual invocation of `$DONE`; refer to
214+
[Writing Asynchronous Tests](#writing-asynchronous-tests) for details
214215
- **generated** - informative flag used to denote test files that were
215216
created procedurally using the project's test generation tool; refer to
216217
[Procedurally-generated tests](#procedurally-generated-tests)
@@ -346,7 +347,7 @@ Consumers that violate the spec by throwing exceptions for parsing errors at run
346347

347348
An asynchronous test is any test that include the `async` frontmatter flag.
348349

349-
For most asynchronous tests, the `asyncHelpers.js` harness file includes an `asyncTest` method that precludes needing to interact with the test runner via the `$DONE` function. `asyncTest` takes an async function and will ensure that `$DONE` is called properly if the async function returns or throws an exception. For example, a test written using `asyncTest` might look like:
350+
Most asynchronous tests should include the `asyncHelpers.js` harness file and call its `asyncTest` function **exactly once**, with a callback returning a promise that indicates test failure via rejection and otherwise fulfills upon test conclusion (such as an async function).
350351

351352
```js
352353
/*---

harness/asyncHelpers.js

+47-63
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ description: |
66
defines: [asyncTest]
77
---*/
88

9+
/**
10+
* Defines the **sole** asynchronous test of a file.
11+
* @see {@link ../docs/rfcs/async-helpers.md} for background.
12+
*
13+
* @param {Function} testFunc a callback whose returned promise indicates test results
14+
* (fulfillment for success, rejection for failure)
15+
* @returns {void}
16+
*/
917
function asyncTest(testFunc) {
1018
if (!Object.hasOwn(globalThis, "$DONE")) {
1119
throw new Test262Error("asyncTest called without async flag");
@@ -28,86 +36,62 @@ function asyncTest(testFunc) {
2836
}
2937
}
3038

39+
/**
40+
* Asserts that a callback asynchronously throws an instance of a particular
41+
* error (i.e., returns a promise whose rejection value is an object referencing
42+
* the constructor).
43+
*
44+
* @param {Function} expectedErrorConstructor the expected constructor of the
45+
* rejection value
46+
* @param {Function} func the callback
47+
* @param {string} [message] the prefix to use for failure messages
48+
* @returns {Promise<void>} fulfills if the expected error is thrown,
49+
* otherwise rejects
50+
*/
3151
assert.throwsAsync = function (expectedErrorConstructor, func, message) {
3252
return new Promise(function (resolve) {
33-
var innerThenable;
34-
if (message === undefined) {
35-
message = "";
36-
} else {
37-
message += " ";
38-
}
39-
if (typeof func === "function") {
40-
try {
41-
innerThenable = func();
42-
if (
43-
innerThenable === null ||
44-
typeof innerThenable !== "object" ||
45-
typeof innerThenable.then !== "function"
46-
) {
47-
message +=
48-
"Expected to obtain an inner promise that would reject with a" +
49-
expectedErrorConstructor.name +
50-
" but result was not a thenable";
51-
throw new Test262Error(message);
52-
}
53-
} catch (thrown) {
54-
message +=
55-
"Expected a " +
56-
expectedErrorConstructor.name +
57-
" to be thrown asynchronously but an exception was thrown synchronously while obtaining the inner promise";
58-
throw new Test262Error(message);
53+
var expectedName = expectedErrorConstructor.name;
54+
var expectation = "Expected a " + expectedName + " to be thrown asynchronously";
55+
var fail = function (detail) {
56+
if (message === undefined) {
57+
throw new Test262Error(detail);
5958
}
60-
} else {
61-
message +=
62-
"assert.throwsAsync called with an argument that is not a function";
63-
throw new Test262Error(message);
59+
throw new Test262Error(message + " " + detail);
60+
};
61+
var res;
62+
if (typeof func !== "function") {
63+
fail("assert.throwsAsync called with an argument that is not a function");
64+
}
65+
try {
66+
res = func();
67+
} catch (thrown) {
68+
fail(expectation + " but the function threw synchronously");
69+
}
70+
if (res === null || typeof res !== "object" || typeof res.then !== "function") {
71+
fail(expectation + " but result was not a thenable");
6472
}
6573

6674
try {
67-
resolve(innerThenable.then(
75+
resolve(res.then(
6876
function () {
69-
message +=
70-
"Expected a " +
71-
expectedErrorConstructor.name +
72-
" to be thrown asynchronously but no exception was thrown at all";
73-
throw new Test262Error(message);
77+
fail(expectation + " but no exception was thrown at all");
7478
},
7579
function (thrown) {
76-
var expectedName, actualName;
77-
if (typeof thrown !== "object" || thrown === null) {
78-
message += "Thrown value was not an object!";
79-
throw new Test262Error(message);
80+
var actualName;
81+
if (thrown === null || typeof thrown !== "object") {
82+
fail(expectation + " but thrown value was not an object");
8083
} else if (thrown.constructor !== expectedErrorConstructor) {
81-
expectedName = expectedErrorConstructor.name;
8284
actualName = thrown.constructor.name;
8385
if (expectedName === actualName) {
84-
message +=
85-
"Expected a " +
86-
expectedName +
87-
" but got a different error constructor with the same name";
88-
} else {
89-
message +=
90-
"Expected a " + expectedName + " but got a " + actualName;
86+
fail(expectation +
87+
" but got a different error constructor with the same name");
9188
}
92-
throw new Test262Error(message);
89+
fail(expectation + " but got a " + actualName);
9390
}
9491
}
9592
));
9693
} catch (thrown) {
97-
if (typeof thrown !== "object" || thrown === null) {
98-
message +=
99-
"Expected a " +
100-
expectedErrorConstructor.name +
101-
" to be thrown asynchronously but innerThenable synchronously threw a value that was not an object ";
102-
} else {
103-
message +=
104-
"Expected a " +
105-
expectedErrorConstructor.name +
106-
" to be thrown asynchronously but a " +
107-
thrown.constructor.name +
108-
" was thrown synchronously";
109-
}
110-
throw new Test262Error(message);
94+
fail(expectation + " but .then threw synchronously");
11195
}
11296
});
11397
};

0 commit comments

Comments
 (0)