Moved fn skipping policy out of QueueRunner

This should allow us to more easily support complex skipping strategies
like skipping nested cleanup fns when the corresponding befores were
skipped, or skipping specs and suites when a beforeAll fails.

* #1533
This commit is contained in:
Steve Gravrock
2021-09-29 12:14:07 -07:00
parent 457a2727ba
commit 5eaeeb0b6c
8 changed files with 228 additions and 57 deletions
+59 -26
View File
@@ -82,6 +82,10 @@ var getJasmineRequireObj = (function(jasmineGlobal) {
j$.MapContaining = jRequire.MapContaining(j$); j$.MapContaining = jRequire.MapContaining(j$);
j$.SetContaining = jRequire.SetContaining(j$); j$.SetContaining = jRequire.SetContaining(j$);
j$.QueueRunner = jRequire.QueueRunner(j$); j$.QueueRunner = jRequire.QueueRunner(j$);
j$.NeverSkipPolicy = jRequire.NeverSkipPolicy(j$);
j$.CompleteOnFirstErrorSkipPolicy = jRequire.CompleteOnFirstErrorSkipPolicy(
j$
);
j$.ReportDispatcher = jRequire.ReportDispatcher(j$); j$.ReportDispatcher = jRequire.ReportDispatcher(j$);
j$.Spec = jRequire.Spec(j$); j$.Spec = jRequire.Spec(j$);
j$.Spy = jRequire.Spy(j$); j$.Spy = jRequire.Spy(j$);
@@ -1608,12 +1612,17 @@ getJasmineRequireObj().Env = function(j$) {
}; };
var queueRunnerFactory = function(options, args) { var queueRunnerFactory = function(options, args) {
var failFast = false; if (
if (options.isLeaf) { // A spec
failFast = true; options.isLeaf ||
} else if (!options.isReporter) { // A suite, and config.stopOnSpecFailure is set
failFast = config.stopOnSpecFailure; (!options.isLeaf && !options.isReporter && config.stopOnSpecFailure)
) {
options.SkipPolicy = j$.CompleteOnFirstErrorSkipPolicy;
} else {
options.SkipPolicy = j$.NeverSkipPolicy;
} }
options.clearStack = options.clearStack || clearStack; options.clearStack = options.clearStack || clearStack;
options.timeout = { options.timeout = {
setTimeout: realSetTimeout, setTimeout: realSetTimeout,
@@ -1621,7 +1630,6 @@ getJasmineRequireObj().Env = function(j$) {
}; };
options.fail = self.fail; options.fail = self.fail;
options.globalErrors = globalErrors; options.globalErrors = globalErrors;
options.completeOnFirstError = failFast;
options.onException = options.onException =
options.onException || options.onException ||
function(e) { function(e) {
@@ -3380,6 +3388,26 @@ getJasmineRequireObj().Clock = function() {
return Clock; return Clock;
}; };
getJasmineRequireObj().CompleteOnFirstErrorSkipPolicy = function(j$) {
function CompleteOnFirstErrorSkipPolicy(queueableFns, firstCleanupIx) {
this.queueableFns_ = queueableFns;
this.firstCleanupIx_ = firstCleanupIx;
}
CompleteOnFirstErrorSkipPolicy.prototype.skipTo = function(
lastRanFnIx,
errored
) {
if (errored && lastRanFnIx < this.firstCleanupIx_) {
return this.firstCleanupIx_;
} else {
return lastRanFnIx + 1;
}
};
return CompleteOnFirstErrorSkipPolicy;
};
getJasmineRequireObj().DelayedFunctionScheduler = function(j$) { getJasmineRequireObj().DelayedFunctionScheduler = function(j$) {
function DelayedFunctionScheduler() { function DelayedFunctionScheduler() {
var self = this; var self = this;
@@ -7257,6 +7285,16 @@ getJasmineRequireObj().MockDate = function(j$) {
return MockDate; return MockDate;
}; };
getJasmineRequireObj().NeverSkipPolicy = function(j$) {
function NeverSkipPolicy(queueableFns, firstCleanupIx) {}
NeverSkipPolicy.prototype.skipTo = function(lastRanFnIx, errored) {
return lastRanFnIx + 1;
};
return NeverSkipPolicy;
};
getJasmineRequireObj().makePrettyPrinter = function(j$) { getJasmineRequireObj().makePrettyPrinter = function(j$) {
function SinglePrettyPrintRun(customObjectFormatters, pp) { function SinglePrettyPrintRun(customObjectFormatters, pp) {
this.customObjectFormatters_ = customObjectFormatters; this.customObjectFormatters_ = customObjectFormatters;
@@ -7721,7 +7759,9 @@ getJasmineRequireObj().QueueRunner = function(j$) {
pushListener: emptyFn, pushListener: emptyFn,
popListener: emptyFn popListener: emptyFn
}; };
this.completeOnFirstError = !!attrs.completeOnFirstError;
const SkipPolicy = attrs.SkipPolicy || j$.NeverSkipPolicy;
this.skipPolicy_ = new SkipPolicy(this.queueableFns, this.firstCleanupIx);
this.errored = false; this.errored = false;
if (typeof this.onComplete !== 'function') { if (typeof this.onComplete !== 'function') {
@@ -7742,14 +7782,6 @@ getJasmineRequireObj().QueueRunner = function(j$) {
this.run(0); this.run(0);
}; };
QueueRunner.prototype.skipToCleanup = function(lastRanIndex) {
if (lastRanIndex < this.firstCleanupIx) {
this.run(this.firstCleanupIx);
} else {
this.run(lastRanIndex + 1);
}
};
QueueRunner.prototype.clearTimeout = function(timeoutId) { QueueRunner.prototype.clearTimeout = function(timeoutId) {
Function.prototype.apply.apply(this.timeout.clearTimeout, [ Function.prototype.apply.apply(this.timeout.clearTimeout, [
j$.getGlobal(), j$.getGlobal(),
@@ -7790,11 +7822,7 @@ getJasmineRequireObj().QueueRunner = function(j$) {
} }
function runNext() { function runNext() {
if (self.completeOnFirstError && errored) { self.run(self.nextFnIx_(iterativeIndex));
self.skipToCleanup(iterativeIndex);
} else {
self.run(iterativeIndex + 1);
}
} }
if (completedSynchronously) { if (completedSynchronously) {
@@ -7892,7 +7920,7 @@ getJasmineRequireObj().QueueRunner = function(j$) {
for ( for (
iterativeIndex = recursiveIndex; iterativeIndex = recursiveIndex;
iterativeIndex < length; iterativeIndex < length;
iterativeIndex++ iterativeIndex = this.nextFnIx_(iterativeIndex)
) { ) {
var result = this.attempt(iterativeIndex); var result = this.attempt(iterativeIndex);
@@ -7901,11 +7929,6 @@ getJasmineRequireObj().QueueRunner = function(j$) {
} }
self.errored = self.errored || result.errored; self.errored = self.errored || result.errored;
if (this.completeOnFirstError && result.errored) {
this.skipToCleanup(iterativeIndex);
return;
}
} }
this.clearStack(function() { this.clearStack(function() {
@@ -7919,6 +7942,16 @@ getJasmineRequireObj().QueueRunner = function(j$) {
}); });
}; };
QueueRunner.prototype.nextFnIx_ = function(currentFnIx) {
const result = this.skipPolicy_.skipTo(currentFnIx, this.errored);
if (result === currentFnIx) {
throw new Error("Can't skip to the same queueable fn that just finished");
}
return result;
};
QueueRunner.prototype.diagnoseConflictingAsync_ = function(fn, retval) { QueueRunner.prototype.diagnoseConflictingAsync_ = function(fn, retval) {
var msg; var msg;
@@ -0,0 +1,45 @@
describe('CompleteOnFirstErrorSkipPolicy', function() {
describe('#skipTo', function() {
describe('When errored is false', function() {
it('returns the next index', function() {
const policy = new jasmineUnderTest.CompleteOnFirstErrorSkipPolicy(
arrayOfArbitraryFns(4),
4
);
expect(policy.skipTo(1, false)).toEqual(2);
});
});
describe('When errored is true', function() {
it('returns the first cleanup fn when called with a non cleanup fn', function() {
const policy = new jasmineUnderTest.CompleteOnFirstErrorSkipPolicy(
arrayOfArbitraryFns(4),
2
);
expect(policy.skipTo(0, true)).toEqual(2);
expect(policy.skipTo(1, true)).toEqual(2);
});
it('returns the next index when called with a cleanup fn', function() {
const policy = new jasmineUnderTest.CompleteOnFirstErrorSkipPolicy(
arrayOfArbitraryFns(4),
1
);
expect(policy.skipTo(1, true)).toEqual(2);
expect(policy.skipTo(2, true)).toEqual(3);
});
});
});
});
function arrayOfArbitraryFns(n) {
const result = [];
for (let i = 0; i < n; i++) {
result.push({ fn: () => {} });
}
return result;
}
+67 -5
View File
@@ -649,6 +649,68 @@ describe('QueueRunner', function() {
expect(nextQueueableFn.fn).toHaveBeenCalled(); expect(nextQueueableFn.fn).toHaveBeenCalled();
}); });
describe('When configured with a skip policy', function() {
it('instantiates the skip policy', function() {
const SkipPolicy = jasmine.createSpy('SkipPolicy ctor');
const queueableFns = [{ fn: () => {} }, { fn: () => {} }];
const cleanupFns = [{ fn: () => {} }];
new jasmineUnderTest.QueueRunner({
queueableFns,
cleanupFns,
SkipPolicy
});
expect(SkipPolicy).toHaveBeenCalledWith(
[...queueableFns, ...cleanupFns],
2
);
});
it('uses the skip policy to determine which fn to run next', function() {
const queueableFns = [
{ fn: jasmine.createSpy('fn0') },
{ fn: jasmine.createSpy('fn1') },
{ fn: jasmine.createSpy('fn2').and.throwError(new Error('nope')) },
{ fn: jasmine.createSpy('fn3') }
];
const skipPolicy = jasmine.createSpyObj('skipPolicy', ['skipTo']);
skipPolicy.skipTo.and.callFake(function(lastRanIx) {
return lastRanIx === 0 ? 2 : lastRanIx + 1;
});
const queueRunner = new jasmineUnderTest.QueueRunner({
queueableFns,
SkipPolicy: function() {
return skipPolicy;
}
});
queueRunner.execute();
expect(skipPolicy.skipTo).toHaveBeenCalledWith(0, false);
expect(skipPolicy.skipTo).toHaveBeenCalledWith(2, true);
expect(queueableFns[0].fn).toHaveBeenCalled();
expect(queueableFns[1].fn).not.toHaveBeenCalled();
expect(queueableFns[2].fn).toHaveBeenCalled();
expect(queueableFns[3].fn).toHaveBeenCalled();
});
it('throws if the skip policy returns the current fn', function() {
const skipPolicy = { skipTo: i => i };
const queueableFns = [{ fn: () => {} }];
const queueRunner = new jasmineUnderTest.QueueRunner({
queueableFns,
SkipPolicy: function() {
return skipPolicy;
}
});
expect(function() {
queueRunner.execute();
}).toThrowError("Can't skip to the same queueable fn that just finished");
});
});
describe('When configured to complete on first error', function() { describe('When configured to complete on first error', function() {
it('skips to cleanup functions on the first exception', function() { it('skips to cleanup functions on the first exception', function() {
var queueableFn = { var queueableFn = {
@@ -663,7 +725,7 @@ describe('QueueRunner', function() {
queueableFns: [queueableFn, nextQueueableFn], queueableFns: [queueableFn, nextQueueableFn],
cleanupFns: [cleanupFn], cleanupFns: [cleanupFn],
onComplete: onComplete, onComplete: onComplete,
completeOnFirstError: true SkipPolicy: jasmineUnderTest.CompleteOnFirstErrorSkipPolicy
}); });
queueRunner.execute(); queueRunner.execute();
@@ -685,7 +747,7 @@ describe('QueueRunner', function() {
queueRunner = new jasmineUnderTest.QueueRunner({ queueRunner = new jasmineUnderTest.QueueRunner({
queueableFns: [queueableFn], queueableFns: [queueableFn],
cleanupFns: [cleanupFn1, cleanupFn2], cleanupFns: [cleanupFn1, cleanupFn2],
completeOnFirstError: true SkipPolicy: jasmineUnderTest.CompleteOnFirstErrorSkipPolicy
}); });
queueRunner.execute(); queueRunner.execute();
@@ -721,7 +783,7 @@ describe('QueueRunner', function() {
}, },
queueableFns: [queueableFn, nextQueueableFn], queueableFns: [queueableFn, nextQueueableFn],
cleanupFns: [cleanupFn], cleanupFns: [cleanupFn],
completeOnFirstError: true SkipPolicy: jasmineUnderTest.CompleteOnFirstErrorSkipPolicy
}), }),
queueableFnDone; queueableFnDone;
@@ -744,7 +806,7 @@ describe('QueueRunner', function() {
queueRunner = new jasmineUnderTest.QueueRunner({ queueRunner = new jasmineUnderTest.QueueRunner({
queueableFns: [queueableFn, nextQueueableFn], queueableFns: [queueableFn, nextQueueableFn],
cleanupFns: [cleanupFn], cleanupFns: [cleanupFn],
completeOnFirstError: true SkipPolicy: jasmineUnderTest.CompleteOnFirstErrorSkipPolicy
}); });
queueRunner.execute(); queueRunner.execute();
@@ -764,7 +826,7 @@ describe('QueueRunner', function() {
queueRunner = new jasmineUnderTest.QueueRunner({ queueRunner = new jasmineUnderTest.QueueRunner({
queueableFns: [queueableFn, nextQueueableFn], queueableFns: [queueableFn, nextQueueableFn],
cleanupFns: [cleanupFn], cleanupFns: [cleanupFn],
completeOnFirstError: true SkipPolicy: jasmineUnderTest.CompleteOnFirstErrorSkipPolicy
}); });
queueRunner.execute(); queueRunner.execute();
@@ -0,0 +1,19 @@
getJasmineRequireObj().CompleteOnFirstErrorSkipPolicy = function(j$) {
function CompleteOnFirstErrorSkipPolicy(queueableFns, firstCleanupIx) {
this.queueableFns_ = queueableFns;
this.firstCleanupIx_ = firstCleanupIx;
}
CompleteOnFirstErrorSkipPolicy.prototype.skipTo = function(
lastRanFnIx,
errored
) {
if (errored && lastRanFnIx < this.firstCleanupIx_) {
return this.firstCleanupIx_;
} else {
return lastRanFnIx + 1;
}
};
return CompleteOnFirstErrorSkipPolicy;
};
+10 -6
View File
@@ -502,12 +502,17 @@ getJasmineRequireObj().Env = function(j$) {
}; };
var queueRunnerFactory = function(options, args) { var queueRunnerFactory = function(options, args) {
var failFast = false; if (
if (options.isLeaf) { // A spec
failFast = true; options.isLeaf ||
} else if (!options.isReporter) { // A suite, and config.stopOnSpecFailure is set
failFast = config.stopOnSpecFailure; (!options.isLeaf && !options.isReporter && config.stopOnSpecFailure)
) {
options.SkipPolicy = j$.CompleteOnFirstErrorSkipPolicy;
} else {
options.SkipPolicy = j$.NeverSkipPolicy;
} }
options.clearStack = options.clearStack || clearStack; options.clearStack = options.clearStack || clearStack;
options.timeout = { options.timeout = {
setTimeout: realSetTimeout, setTimeout: realSetTimeout,
@@ -515,7 +520,6 @@ getJasmineRequireObj().Env = function(j$) {
}; };
options.fail = self.fail; options.fail = self.fail;
options.globalErrors = globalErrors; options.globalErrors = globalErrors;
options.completeOnFirstError = failFast;
options.onException = options.onException =
options.onException || options.onException ||
function(e) { function(e) {
+9
View File
@@ -0,0 +1,9 @@
getJasmineRequireObj().NeverSkipPolicy = function(j$) {
function NeverSkipPolicy(queueableFns, firstCleanupIx) {}
NeverSkipPolicy.prototype.skipTo = function(lastRanFnIx, errored) {
return lastRanFnIx + 1;
};
return NeverSkipPolicy;
};
+15 -20
View File
@@ -56,7 +56,9 @@ getJasmineRequireObj().QueueRunner = function(j$) {
pushListener: emptyFn, pushListener: emptyFn,
popListener: emptyFn popListener: emptyFn
}; };
this.completeOnFirstError = !!attrs.completeOnFirstError;
const SkipPolicy = attrs.SkipPolicy || j$.NeverSkipPolicy;
this.skipPolicy_ = new SkipPolicy(this.queueableFns, this.firstCleanupIx);
this.errored = false; this.errored = false;
if (typeof this.onComplete !== 'function') { if (typeof this.onComplete !== 'function') {
@@ -77,14 +79,6 @@ getJasmineRequireObj().QueueRunner = function(j$) {
this.run(0); this.run(0);
}; };
QueueRunner.prototype.skipToCleanup = function(lastRanIndex) {
if (lastRanIndex < this.firstCleanupIx) {
this.run(this.firstCleanupIx);
} else {
this.run(lastRanIndex + 1);
}
};
QueueRunner.prototype.clearTimeout = function(timeoutId) { QueueRunner.prototype.clearTimeout = function(timeoutId) {
Function.prototype.apply.apply(this.timeout.clearTimeout, [ Function.prototype.apply.apply(this.timeout.clearTimeout, [
j$.getGlobal(), j$.getGlobal(),
@@ -125,11 +119,7 @@ getJasmineRequireObj().QueueRunner = function(j$) {
} }
function runNext() { function runNext() {
if (self.completeOnFirstError && errored) { self.run(self.nextFnIx_(iterativeIndex));
self.skipToCleanup(iterativeIndex);
} else {
self.run(iterativeIndex + 1);
}
} }
if (completedSynchronously) { if (completedSynchronously) {
@@ -227,7 +217,7 @@ getJasmineRequireObj().QueueRunner = function(j$) {
for ( for (
iterativeIndex = recursiveIndex; iterativeIndex = recursiveIndex;
iterativeIndex < length; iterativeIndex < length;
iterativeIndex++ iterativeIndex = this.nextFnIx_(iterativeIndex)
) { ) {
var result = this.attempt(iterativeIndex); var result = this.attempt(iterativeIndex);
@@ -236,11 +226,6 @@ getJasmineRequireObj().QueueRunner = function(j$) {
} }
self.errored = self.errored || result.errored; self.errored = self.errored || result.errored;
if (this.completeOnFirstError && result.errored) {
this.skipToCleanup(iterativeIndex);
return;
}
} }
this.clearStack(function() { this.clearStack(function() {
@@ -254,6 +239,16 @@ getJasmineRequireObj().QueueRunner = function(j$) {
}); });
}; };
QueueRunner.prototype.nextFnIx_ = function(currentFnIx) {
const result = this.skipPolicy_.skipTo(currentFnIx, this.errored);
if (result === currentFnIx) {
throw new Error("Can't skip to the same queueable fn that just finished");
}
return result;
};
QueueRunner.prototype.diagnoseConflictingAsync_ = function(fn, retval) { QueueRunner.prototype.diagnoseConflictingAsync_ = function(fn, retval) {
var msg; var msg;
+4
View File
@@ -60,6 +60,10 @@ var getJasmineRequireObj = (function(jasmineGlobal) {
j$.MapContaining = jRequire.MapContaining(j$); j$.MapContaining = jRequire.MapContaining(j$);
j$.SetContaining = jRequire.SetContaining(j$); j$.SetContaining = jRequire.SetContaining(j$);
j$.QueueRunner = jRequire.QueueRunner(j$); j$.QueueRunner = jRequire.QueueRunner(j$);
j$.NeverSkipPolicy = jRequire.NeverSkipPolicy(j$);
j$.CompleteOnFirstErrorSkipPolicy = jRequire.CompleteOnFirstErrorSkipPolicy(
j$
);
j$.ReportDispatcher = jRequire.ReportDispatcher(j$); j$.ReportDispatcher = jRequire.ReportDispatcher(j$);
j$.Spec = jRequire.Spec(j$); j$.Spec = jRequire.Spec(j$);
j$.Spy = jRequire.Spy(j$); j$.Spy = jRequire.Spy(j$);