shell.js (3626B)
1 // GENERATED, DO NOT EDIT 2 // file: asyncHelpers.js 3 // Copyright (C) 2022 Igalia, S.L. All rights reserved. 4 // This code is governed by the BSD license found in the LICENSE file. 5 /*--- 6 description: | 7 A collection of assertion and wrapper functions for testing asynchronous built-ins. 8 defines: [asyncTest, assert.throwsAsync] 9 ---*/ 10 11 /** 12 * Defines the **sole** asynchronous test of a file. 13 * @see {@link ../docs/rfcs/async-helpers.md} for background. 14 * 15 * @param {Function} testFunc a callback whose returned promise indicates test results 16 * (fulfillment for success, rejection for failure) 17 * @returns {void} 18 */ 19 function asyncTest(testFunc) { 20 if (!Object.prototype.hasOwnProperty.call(globalThis, "$DONE")) { 21 throw new Test262Error("asyncTest called without async flag"); 22 } 23 if (typeof testFunc !== "function") { 24 $DONE(new Test262Error("asyncTest called with non-function argument")); 25 return; 26 } 27 try { 28 testFunc().then( 29 function () { 30 $DONE(); 31 }, 32 function (error) { 33 $DONE(error); 34 } 35 ); 36 } catch (syncError) { 37 $DONE(syncError); 38 } 39 } 40 41 /** 42 * Asserts that a callback asynchronously throws an instance of a particular 43 * error (i.e., returns a promise whose rejection value is an object referencing 44 * the constructor). 45 * 46 * @param {Function} expectedErrorConstructor the expected constructor of the 47 * rejection value 48 * @param {Function} func the callback 49 * @param {string} [message] the prefix to use for failure messages 50 * @returns {Promise<void>} fulfills if the expected error is thrown, 51 * otherwise rejects 52 */ 53 assert.throwsAsync = function (expectedErrorConstructor, func, message) { 54 return new Promise(function (resolve) { 55 var fail = function (detail) { 56 if (message === undefined) { 57 throw new Test262Error(detail); 58 } 59 throw new Test262Error(message + " " + detail); 60 }; 61 if (typeof expectedErrorConstructor !== "function") { 62 fail("assert.throwsAsync called with an argument that is not an error constructor"); 63 } 64 if (typeof func !== "function") { 65 fail("assert.throwsAsync called with an argument that is not a function"); 66 } 67 var expectedName = expectedErrorConstructor.name; 68 var expectation = "Expected a " + expectedName + " to be thrown asynchronously"; 69 var res; 70 try { 71 res = func(); 72 } catch (thrown) { 73 fail(expectation + " but the function threw synchronously"); 74 } 75 if (res === null || typeof res !== "object" || typeof res.then !== "function") { 76 fail(expectation + " but result was not a thenable"); 77 } 78 var onResFulfilled, onResRejected; 79 var resSettlementP = new Promise(function (onFulfilled, onRejected) { 80 onResFulfilled = onFulfilled; 81 onResRejected = onRejected; 82 }); 83 try { 84 res.then(onResFulfilled, onResRejected) 85 } catch (thrown) { 86 fail(expectation + " but .then threw synchronously"); 87 } 88 resolve(resSettlementP.then( 89 function () { 90 fail(expectation + " but no exception was thrown at all"); 91 }, 92 function (thrown) { 93 var actualName; 94 if (thrown === null || typeof thrown !== "object") { 95 fail(expectation + " but thrown value was not an object"); 96 } else if (thrown.constructor !== expectedErrorConstructor) { 97 actualName = thrown.constructor.name; 98 if (expectedName === actualName) { 99 fail(expectation + 100 " but got a different error constructor with the same name"); 101 } 102 fail(expectation + " but got a " + actualName); 103 } 104 } 105 )); 106 }); 107 };