| 1 |
// Smoke test for `Promise.any`, test262 should cover the function in |
| 2 |
// more detail. |
| 3 |
|
| 4 |
function expectedError() { |
| 5 |
reportCompare(true, false, "expected error"); |
| 6 |
} |
| 7 |
|
| 8 |
// Empty elements. |
| 9 |
Promise.any([]).then(expectedError, e => { |
| 10 |
assertEq(e instanceof AggregateError, true); |
| 11 |
assertEq(e.errors.length, 0); |
| 12 |
}); |
| 13 |
|
| 14 |
// Single element. |
| 15 |
Promise.any([Promise.resolve(0)]).then(v => { |
| 16 |
assertEq(v, 0); |
| 17 |
}); |
| 18 |
Promise.any([Promise.reject(1)]).then(expectedError, e => { |
| 19 |
assertEq(e instanceof AggregateError, true); |
| 20 |
assertEq(e.errors.length, 1); |
| 21 |
assertEq(e.errors[0], 1); |
| 22 |
}); |
| 23 |
|
| 24 |
// Multiple elements. |
| 25 |
Promise.any([Promise.resolve(1), Promise.resolve(2)]).then(v => { |
| 26 |
assertEq(v, 1); |
| 27 |
}); |
| 28 |
Promise.any([Promise.resolve(3), Promise.reject(4)]).then(v => { |
| 29 |
assertEq(v, 3); |
| 30 |
}); |
| 31 |
Promise.any([Promise.reject(5), Promise.resolve(6)]).then(v => { |
| 32 |
assertEq(v, 6); |
| 33 |
}); |
| 34 |
Promise.any([Promise.reject(7), Promise.reject(8)]).then(expectedError, e => { |
| 35 |
assertEq(e instanceof AggregateError, true); |
| 36 |
assertEq(e.errors.length, 2); |
| 37 |
assertEq(e.errors[0], 7); |
| 38 |
assertEq(e.errors[1], 8); |
| 39 |
}); |
| 40 |
|
| 41 |
// Cross-Realm tests. |
| 42 |
// |
| 43 |
// Note: When |g| is a cross-compartment global, Promise.any creates the errors |
| 44 |
// array and the AggregateError in |g|'s Realm. This doesn't follow the spec, but |
| 45 |
// the code in js/src/builtin/Promise.cpp claims this is useful when the Promise |
| 46 |
// compartment is less-privileged. This means for this test we can't use |
| 47 |
// assertDeepEq below, because the result array/error may have the wrong prototype. |
| 48 |
let g = newGlobal(); |
| 49 |
|
| 50 |
if (typeof isSameCompartment !== "function") { |
| 51 |
var isSameCompartment = SpecialPowers.Cu.getJSTestingFunctions().isSameCompartment; |
| 52 |
} |
| 53 |
|
| 54 |
// Test wrapping when no `Promise.any Reject Element Function` is called. |
| 55 |
Promise.any.call(g.Promise, []).then(expectedError, e => { |
| 56 |
assertEq(e.name, "AggregateError"); |
| 57 |
|
| 58 |
assertEq(isSameCompartment(e, g), true); |
| 59 |
assertEq(isSameCompartment(e.errors, g), true); |
| 60 |
|
| 61 |
assertEq(e.errors.length, 0); |
| 62 |
}); |
| 63 |
|
| 64 |
// Test wrapping in `Promise.any Reject Element Function`. |
| 65 |
Promise.any.call(g.Promise, [Promise.reject("err")]).then(expectedError, e => { |
| 66 |
assertEq(e.name, "AggregateError"); |
| 67 |
|
| 68 |
assertEq(isSameCompartment(e, g), true); |
| 69 |
assertEq(isSameCompartment(e.errors, g), true); |
| 70 |
|
| 71 |
assertEq(e.errors.length, 1); |
| 72 |
assertEq(e.errors[0], "err"); |
| 73 |
}); |
| 74 |
|
| 75 |
if (typeof reportCompare === "function") |
| 76 |
reportCompare(0, 0); |