harness/asyncHelpers.js: Refactor asyncTest to always return undefined instead of a Promise

This commit is contained in:
Cam Tenny 2022-12-30 16:06:33 -08:00 committed by Philip Chimento
parent d810c684ae
commit 1d116b48a9
5 changed files with 80 additions and 70 deletions

View File

@ -10,17 +10,12 @@ 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");
} }
const resolveThenable = {
then(resolve, reject) {
resolve();
},
};
if (typeof testFunc !== "function") { if (typeof testFunc !== "function") {
$DONE(new Test262Error("asyncTest called with non-function argument")); $DONE(new Test262Error("asyncTest called with non-function argument"));
return resolveThenable; return;
} }
try { try {
return testFunc().then( testFunc().then(
function () { function () {
$DONE(); $DONE();
}, },
@ -30,7 +25,6 @@ function asyncTest(testFunc) {
); );
} catch (syncError) { } catch (syncError) {
$DONE(syncError); $DONE(syncError);
return resolveThenable;
} }
} }

View File

@ -11,6 +11,6 @@ function $DONE(error) {
assert(error instanceof Test262Error); assert(error instanceof Test262Error);
} }
asyncTest(function () { asyncTest(function () {
throw new Test262Error("Should not be evaluated"); throw new Test262Error("Should not be rethrown");
}); });
assert(called, "asyncTest called $DONE with a synchronously thrown error"); assert(called, "asyncTest called $DONE with a synchronously thrown error");

View File

@ -2,7 +2,7 @@
// This code is governed by the BSD license found in the LICENSE file. // This code is governed by the BSD license found in the LICENSE file.
/*--- /*---
description: | description: |
The 'asyncTest' helper when called with async flag always returns a promise that resolves to undefined. The 'asyncTest' helper when called with async flag always returns to undefined.
flags: [async] flags: [async]
includes: [asyncHelpers.js] includes: [asyncHelpers.js]
---*/ ---*/
@ -12,34 +12,28 @@ globalThis.$DONE = function () {
doneCalls++; doneCalls++;
}; };
async function assertPromiseUndefined(returnedPromise) {
assert(
returnedPromise instanceof Promise,
"returned value should be a Promise"
);
assert.sameValue(
await returnedPromise,
undefined,
"returned Promise should resolve to undefined"
);
}
(async function () { (async function () {
await assertPromiseUndefined(asyncTest({})); assert.sameValue(undefined, asyncTest({}));
await assertPromiseUndefined( assert.sameValue(
undefined,
asyncTest(function () { asyncTest(function () {
return "non-thenable"; return "non-thenable";
}) })
); );
await assertPromiseUndefined( assert.sameValue(
undefined,
asyncTest(function () { asyncTest(function () {
return Promise.resolve(true); return Promise.resolve(true);
}) })
); );
await assertPromiseUndefined( assert.sameValue(
undefined,
asyncTest(function () { asyncTest(function () {
return Promise.reject(new Test262Error("oh no")); return Promise.reject(new Test262Error("oh no"));
}) })
); );
})()
.then(() => {
assert.sameValue(doneCalls, 4, "asyncTest must call $DONE"); assert.sameValue(doneCalls, 4, "asyncTest must call $DONE");
})().then(realDone, realDone); })
.then(realDone, realDone);

View File

@ -14,24 +14,35 @@ globalThis.$DONE = function (mustBeDefined) {
const someObject = {}; const someObject = {};
(async function () { (async function () {
await asyncTest(function () { asyncTest(function () {
return Promise.reject(null); return Promise.reject(null);
}); });
await asyncTest(function () { })()
.then(() => {
asyncTest(function () {
return Promise.reject(someObject); return Promise.reject(someObject);
}); });
await asyncTest(function () { })
.then(() => {
asyncTest(function () {
return Promise.reject("hi"); return Promise.reject("hi");
}); });
await asyncTest(function () { })
.then(() => {
asyncTest(function () {
return Promise.reject(10); return Promise.reject(10);
}); });
await asyncTest(function () { })
.then(() => {
asyncTest(function () {
return { return {
then(res, rej) { then(res, rej) {
rej(true); rej(true);
}, },
}; };
}); });
})
.then(() => {
assert.compareArray(rejectionValues, [null, someObject, "hi", 10, true]); assert.compareArray(rejectionValues, [null, someObject, "hi", 10, true]);
})().then(realDone, realDone); })
.then(realDone, realDone);

View File

@ -18,28 +18,39 @@ globalThis.$DONE = function (noError) {
}; };
(async function () { (async function () {
await asyncTest(function () { asyncTest(function () {
return Promise.resolve(null); return Promise.resolve(null);
}); });
})()
.then(() => {
assert.sameValue(doneCalls, 1, "asyncTest called $DONE with undefined"); assert.sameValue(doneCalls, 1, "asyncTest called $DONE with undefined");
await asyncTest(function () { asyncTest(function () {
return Promise.resolve({}); return Promise.resolve({});
}); });
})
.then(() => {
assert.sameValue(doneCalls, 2, "asyncTest called $DONE with undefined"); assert.sameValue(doneCalls, 2, "asyncTest called $DONE with undefined");
await asyncTest(function () { asyncTest(function () {
return Promise.resolve("hi"); return Promise.resolve("hi");
}); });
})
.then(() => {
assert.sameValue(doneCalls, 3, "asyncTest called $DONE with undefined"); assert.sameValue(doneCalls, 3, "asyncTest called $DONE with undefined");
await asyncTest(function () { asyncTest(function () {
return Promise.resolve(10); return Promise.resolve(10);
}); });
})
.then(() => {
assert.sameValue(doneCalls, 4, "asyncTest called $DONE with undefined"); assert.sameValue(doneCalls, 4, "asyncTest called $DONE with undefined");
await asyncTest(function () { asyncTest(function () {
return { return {
then(res, rej) { then(res, rej) {
res(true); res(true);
}, },
}; };
}); });
})
.then(() => {
assert.sameValue(doneCalls, 5, "asyncTest called $DONE with undefined"); assert.sameValue(doneCalls, 5, "asyncTest called $DONE with undefined");
})().then(realDone, realDone); })
.then(realDone, realDone);