Assert that promise callbacks were actually called. Closes #1328

Originally reported here: https://github.com/tc39/test262/pull/1328, OP passed on revising.
This commit is contained in:
Rick Waldron 2018-01-05 13:23:35 -05:00
parent 02f1a0b630
commit 66df349af3
5 changed files with 44 additions and 40 deletions

View File

@ -6,41 +6,34 @@ description: finally observably calls .then
esid: sec-promise.prototype.finally esid: sec-promise.prototype.finally
features: [Promise.prototype.finally] features: [Promise.prototype.finally]
flags: [async] flags: [async]
includes: [promiseHelper.js]
---*/ ---*/
var sequence = [];
var initialThenCount = 0;
var noReason = {}; var noReason = {};
var no = Promise.reject(noReason); var no = Promise.reject(noReason);
no.then = function () { no.then = function () {
initialThenCount += 1; sequence.push(1);
return Promise.prototype.then.apply(this, arguments); return Promise.prototype.then.apply(this, arguments);
}; };
var onFinallyThenCount = 0;
var yesValue = {}; var yesValue = {};
var yes = Promise.resolve(yesValue); var yes = Promise.resolve(yesValue);
yes.then = function () { yes.then = function () {
onFinallyThenCount += 1; sequence.push(4);
return Promise.prototype.then.apply(this, arguments); return Promise.prototype.then.apply(this, arguments);
}; };
var finallyCalled = false;
var catchCalled = false;
no.catch(function (e) { no.catch(function (e) {
sequence.push(2);
assert.sameValue(e, noReason); assert.sameValue(e, noReason);
throw e; throw e;
}).finally(function () { }).finally(function () {
finallyCalled = true; sequence.push(3);
return yes; return yes;
}).catch(function (e) { }).catch(function (e) {
catchCalled = true; sequence.push(5);
assert.sameValue(e, noReason); assert.sameValue(e, noReason);
}).then(function () { }).then(function () {
assert.sameValue(finallyCalled, true, 'initial finally was called'); checkSequence(sequence, "All expected callbacks called in correct order");
assert.sameValue(initialThenCount, 1, 'initial finally invokes .then once');
assert.sameValue(catchCalled, true, 'catch was called');
assert.sameValue(onFinallyThenCount, 1, 'onFinally return promise has .then invoked once');
$DONE(); $DONE();
}).catch($ERROR); }).catch($ERROR);

View File

@ -6,18 +6,24 @@ description: finally on a rejected promise can not convert to a fulfillment
esid: sec-promise.prototype.finally esid: sec-promise.prototype.finally
features: [Promise.prototype.finally] features: [Promise.prototype.finally]
flags: [async] flags: [async]
includes: [promiseHelper.js]
---*/ ---*/
var sequence = [];
var original = {}; var original = {};
var replacement = {}; var replacement = {};
var p = Promise.reject(original); var p = Promise.reject(original);
p.finally(function () { p.finally(function() {
sequence.push(1);
assert.sameValue(arguments.length, 0, 'onFinally receives zero args'); assert.sameValue(arguments.length, 0, 'onFinally receives zero args');
return replacement; return replacement;
}).then(function () { }).then(function() {
$ERROR('promise is rejected pre-finally; onFulfill should not be called'); $ERROR('promise is rejected pre-finally; onFulfill should not be called');
}).catch(function (reason) { }).catch(function(reason) {
sequence.push(2);
assert.sameValue(reason, original, 'onFinally can not override the rejection value by returning'); assert.sameValue(reason, original, 'onFinally can not override the rejection value by returning');
}).then($DONE).catch($ERROR); }).then(function() {
checkSequence(sequence, "All expected callbacks called in correct order");
$DONE();
}).catch($ERROR);

View File

@ -6,18 +6,24 @@ description: finally on a rejected promise can override the rejection reason
esid: sec-promise.prototype.finally esid: sec-promise.prototype.finally
features: [Promise.prototype.finally] features: [Promise.prototype.finally]
flags: [async] flags: [async]
includes: [promiseHelper.js]
---*/ ---*/
var sequence = [];
var original = {}; var original = {};
var thrown = {}; var thrown = {};
var p = Promise.reject(original); var p = Promise.reject(original);
p.finally(function () { p.finally(function () {
sequence.push(1);
assert.sameValue(arguments.length, 0, 'onFinally receives zero args'); assert.sameValue(arguments.length, 0, 'onFinally receives zero args');
throw thrown; throw thrown;
}).then(function () { }).then(function () {
$ERROR('promise is rejected; onFulfill should not be called'); $ERROR('promise is rejected; onFulfill should not be called');
}).catch(function (reason) { }).catch(function (reason) {
sequence.push(2);
assert.sameValue(reason, thrown, 'onFinally can override the rejection reason by throwing'); assert.sameValue(reason, thrown, 'onFinally can override the rejection reason by throwing');
}).then($DONE).catch($ERROR); }).then(function() {
checkSequence(sequence, "All expected callbacks called in correct order");
$DONE();
}).catch($ERROR);

View File

@ -6,15 +6,20 @@ description: finally on a fulfilled promise can not override the resolution valu
esid: sec-promise.prototype.finally esid: sec-promise.prototype.finally
features: [Promise.prototype.finally] features: [Promise.prototype.finally]
flags: [async] flags: [async]
includes: [promiseHelper.js]
---*/ ---*/
var sequence = [];
var obj = {}; var obj = {};
var p = Promise.resolve(obj); var p = Promise.resolve(obj);
p.finally(function () { p.finally(function () {
sequence.push(1);
assert.sameValue(arguments.length, 0, 'onFinally receives zero args'); assert.sameValue(arguments.length, 0, 'onFinally receives zero args');
return {}; return {};
}).then(function (x) { }).then(function (x) {
sequence.push(2);
assert.sameValue(x, obj, 'onFinally can not override the resolution value'); assert.sameValue(x, obj, 'onFinally can not override the resolution value');
}).then($DONE).catch($ERROR); }).then(function() {
checkSequence(sequence, "All expected callbacks called in correct order");
$DONE();
}).catch($ERROR);

View File

@ -6,41 +6,35 @@ description: finally observably calls .then
esid: sec-promise.prototype.finally esid: sec-promise.prototype.finally
features: [Promise.prototype.finally] features: [Promise.prototype.finally]
flags: [async] flags: [async]
includes: [promiseHelper.js]
---*/ ---*/
var sequence = [];
var initialThenCount = 0;
var yesValue = {}; var yesValue = {};
var yes = Promise.resolve(yesValue); var yes = Promise.resolve(yesValue);
yes.then = function () { yes.then = function () {
initialThenCount += 1; sequence.push(1);
return Promise.prototype.then.apply(this, arguments); return Promise.prototype.then.apply(this, arguments);
}; };
var onFinallyThenCount = 0;
var noReason = {}; var noReason = {};
var no = Promise.reject(noReason); var no = Promise.reject(noReason);
no.then = function () { no.then = function () {
onFinallyThenCount += 1; sequence.push(4);
return Promise.prototype.then.apply(this, arguments); return Promise.prototype.then.apply(this, arguments);
}; };
var finallyCalled = false;
var catchCalled = false;
yes.then(function (x) { yes.then(function (x) {
sequence.push(2);
assert.sameValue(x, yesValue); assert.sameValue(x, yesValue);
return x; return x;
}).finally(function () { }).finally(function () {
finallyCalled = true; sequence.push(3);
return no; return no;
}).catch(function (e) { }).catch(function (e) {
catchCalled = true; sequence.push(5);
assert.sameValue(e, noReason); assert.sameValue(e, noReason);
}).then(function () { }).then(function () {
assert.sameValue(finallyCalled, true, 'initial finally was called'); checkSequence(sequence, "All expected callbacks called in correct order");
assert.sameValue(initialThenCount, 1, 'initial finally invokes .then once');
assert.sameValue(catchCalled, true, 'catch was called');
assert.sameValue(onFinallyThenCount, 1, 'onFinally return promise has .then invoked once');
$DONE(); $DONE();
}).catch($ERROR); }).catch($ERROR);