Cleanup asyncHelpers (merge #4091)

This commit is contained in:
Richard Gibson 2024-08-01 12:21:00 -04:00 committed by GitHub
commit 7a916718cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 66 deletions

View File

@ -209,8 +209,9 @@ This key is for boolean properties associated with the test.
- **raw** - execute the test without any modification (no harness files will be - **raw** - execute the test without any modification (no harness files will be
included); necessary to test the behavior of directive prologue; implies included); necessary to test the behavior of directive prologue; implies
`noStrict` `noStrict`
- **async** - defer interpretation of test results until after the invocation - **async** - defer interpretation of test results until settlement of an
of the global `$DONE` function `asyncTest` callback promise or manual invocation of `$DONE`; refer to
[Writing Asynchronous Tests](#writing-asynchronous-tests) for details
- **generated** - informative flag used to denote test files that were - **generated** - informative flag used to denote test files that were
created procedurally using the project's test generation tool; refer to created procedurally using the project's test generation tool; refer to
[Procedurally-generated tests](#procedurally-generated-tests) [Procedurally-generated tests](#procedurally-generated-tests)
@ -346,7 +347,7 @@ Consumers that violate the spec by throwing exceptions for parsing errors at run
An asynchronous test is any test that include the `async` frontmatter flag. An asynchronous test is any test that include the `async` frontmatter flag.
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: 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).
```js ```js
/*--- /*---

View File

@ -6,6 +6,14 @@ description: |
defines: [asyncTest] defines: [asyncTest]
---*/ ---*/
/**
* Defines the **sole** asynchronous test of a file.
* @see {@link ../docs/rfcs/async-helpers.md} for background.
*
* @param {Function} testFunc a callback whose returned promise indicates test results
* (fulfillment for success, rejection for failure)
* @returns {void}
*/
function asyncTest(testFunc) { function asyncTest(testFunc) {
if (!Object.hasOwn(globalThis, "$DONE")) { if (!Object.hasOwn(globalThis, "$DONE")) {
throw new Test262Error("asyncTest called without async flag"); throw new Test262Error("asyncTest called without async flag");
@ -28,86 +36,62 @@ function asyncTest(testFunc) {
} }
} }
/**
* Asserts that a callback asynchronously throws an instance of a particular
* error (i.e., returns a promise whose rejection value is an object referencing
* the constructor).
*
* @param {Function} expectedErrorConstructor the expected constructor of the
* rejection value
* @param {Function} func the callback
* @param {string} [message] the prefix to use for failure messages
* @returns {Promise<void>} fulfills if the expected error is thrown,
* otherwise rejects
*/
assert.throwsAsync = function (expectedErrorConstructor, func, message) { assert.throwsAsync = function (expectedErrorConstructor, func, message) {
return new Promise(function (resolve) { return new Promise(function (resolve) {
var innerThenable; var expectedName = expectedErrorConstructor.name;
if (message === undefined) { var expectation = "Expected a " + expectedName + " to be thrown asynchronously";
message = ""; var fail = function (detail) {
} else { if (message === undefined) {
message += " "; throw new Test262Error(detail);
}
if (typeof func === "function") {
try {
innerThenable = func();
if (
innerThenable === null ||
typeof innerThenable !== "object" ||
typeof innerThenable.then !== "function"
) {
message +=
"Expected to obtain an inner promise that would reject with a" +
expectedErrorConstructor.name +
" but result was not a thenable";
throw new Test262Error(message);
}
} catch (thrown) {
message +=
"Expected a " +
expectedErrorConstructor.name +
" to be thrown asynchronously but an exception was thrown synchronously while obtaining the inner promise";
throw new Test262Error(message);
} }
} else { throw new Test262Error(message + " " + detail);
message += };
"assert.throwsAsync called with an argument that is not a function"; var res;
throw new Test262Error(message); if (typeof func !== "function") {
fail("assert.throwsAsync called with an argument that is not a function");
}
try {
res = func();
} catch (thrown) {
fail(expectation + " but the function threw synchronously");
}
if (res === null || typeof res !== "object" || typeof res.then !== "function") {
fail(expectation + " but result was not a thenable");
} }
try { try {
resolve(innerThenable.then( resolve(res.then(
function () { function () {
message += fail(expectation + " but no exception was thrown at all");
"Expected a " +
expectedErrorConstructor.name +
" to be thrown asynchronously but no exception was thrown at all";
throw new Test262Error(message);
}, },
function (thrown) { function (thrown) {
var expectedName, actualName; var actualName;
if (typeof thrown !== "object" || thrown === null) { if (thrown === null || typeof thrown !== "object") {
message += "Thrown value was not an object!"; fail(expectation + " but thrown value was not an object");
throw new Test262Error(message);
} else if (thrown.constructor !== expectedErrorConstructor) { } else if (thrown.constructor !== expectedErrorConstructor) {
expectedName = expectedErrorConstructor.name;
actualName = thrown.constructor.name; actualName = thrown.constructor.name;
if (expectedName === actualName) { if (expectedName === actualName) {
message += fail(expectation +
"Expected a " + " but got a different error constructor with the same name");
expectedName +
" but got a different error constructor with the same name";
} else {
message +=
"Expected a " + expectedName + " but got a " + actualName;
} }
throw new Test262Error(message); fail(expectation + " but got a " + actualName);
} }
} }
)); ));
} catch (thrown) { } catch (thrown) {
if (typeof thrown !== "object" || thrown === null) { fail(expectation + " but .then threw synchronously");
message +=
"Expected a " +
expectedErrorConstructor.name +
" to be thrown asynchronously but innerThenable synchronously threw a value that was not an object ";
} else {
message +=
"Expected a " +
expectedErrorConstructor.name +
" to be thrown asynchronously but a " +
thrown.constructor.name +
" was thrown synchronously";
}
throw new Test262Error(message);
} }
}); });
}; };