More reliably report errors that occur late in the suite/spec lifecycle

Previously, an error that occurred after Jasmine started to report the
suiteDone or specDone event for the current runable would not be reliably
reported. Now such an error is reported on the nearest ancestor suite whose
suiteDone event has not yet been reported.
This commit is contained in:
Steve Gravrock
2022-05-08 15:29:58 -07:00
parent 9ea8a2096f
commit bbb1b69b2e
8 changed files with 610 additions and 107 deletions

View File

@@ -404,6 +404,105 @@ describe('Spec', function() {
]);
});
it('forwards late expectation failures to onLateError', function() {
const onLateError = jasmine.createSpy('onLateError');
const expectationResultFactory = jasmine
.createSpy('expectationResultFactory')
.and.returnValue('built expectation result');
const spec = new jasmineUnderTest.Spec({
expectationResultFactory,
onLateError,
queueableFn: { fn: function() {} }
});
const data = {
matcherName: '',
passed: false,
expected: '',
actual: '',
error: new Error('nope')
};
spec.reportedDone = true;
spec.addExpectationResult(false, data, true);
expect(expectationResultFactory).toHaveBeenCalledWith(data);
expect(onLateError).toHaveBeenCalledWith('built expectation result');
expect(spec.result.failedExpectations).toEqual([]);
});
it('does not forward non-late expectation failures to onLateError', function() {
const onLateError = jasmine.createSpy('onLateError');
const spec = new jasmineUnderTest.Spec({
expectationResultFactory: r => r,
onLateError,
queueableFn: { fn: function() {} }
});
const data = {
matcherName: '',
passed: false,
expected: '',
actual: '',
error: new Error('nope')
};
spec.addExpectationResult(false, data, true);
expect(onLateError).not.toHaveBeenCalled();
});
it('forwards late handleException calls to onLateError', function() {
const onLateError = jasmine.createSpy('onLateError');
const expectationResultFactory = jasmine
.createSpy('expectationResultFactory')
.and.returnValue('built expectation result');
const spec = new jasmineUnderTest.Spec({
expectationResultFactory,
onLateError,
queueableFn: { fn: function() {} }
});
const error = new Error('oops');
spec.reportedDone = true;
spec.handleException(error);
expect(expectationResultFactory).toHaveBeenCalledWith({
matcherName: '',
passed: false,
expected: '',
actual: '',
error
});
expect(onLateError).toHaveBeenCalledWith('built expectation result');
expect(spec.result.failedExpectations).toEqual([]);
});
it('does not forward non-late handleException calls to onLateError', function() {
const onLateError = jasmine.createSpy('onLateError');
const spec = new jasmineUnderTest.Spec({
expectationResultFactory: r => r,
onLateError,
queueableFn: { fn: function() {} }
});
const error = new Error('oops');
spec.handleException(error);
expect(onLateError).not.toHaveBeenCalled();
expect(spec.result.failedExpectations.length).toEqual(1);
});
it('clears the reportedDone flag when reset', function() {
const spec = new jasmineUnderTest.Spec({
expectationResultFactory: r => r,
queueableFn: { fn: function() {} }
});
spec.reportedDone = true;
spec.reset();
expect(spec.reportedDone).toBeFalse();
});
it('does not throw an ExpectationFailed error when handling an error', function() {
const resultCallback = jasmine.createSpy('resultCallback'),
spec = new jasmineUnderTest.Spec({
@@ -418,7 +517,7 @@ describe('Spec', function() {
throwOnExpectationFailure: true
});
spec.onException('failing exception');
spec.handleException('failing exception');
});
it('can return its full name', function() {
@@ -490,7 +589,7 @@ describe('Spec', function() {
resultCallback: resultCallback
});
spec.onException('foo');
spec.handleException('foo');
spec.execute();
const args = fakeQueueRunner.calls.mostRecent().args[0];
@@ -518,7 +617,7 @@ describe('Spec', function() {
resultCallback: resultCallback
});
spec.onException(new jasmineUnderTest.errors.ExpectationFailed());
spec.handleException(new jasmineUnderTest.errors.ExpectationFailed());
spec.execute();
const args = fakeQueueRunner.calls.mostRecent().args[0];
@@ -636,7 +735,7 @@ describe('Spec', function() {
resultCallback: resultCallback,
queueRunnerFactory: function(config) {
spec.debugLog('msg');
spec.onException(new Error('nope'));
spec.handleException(new Error('nope'));
for (const fn of config.queueableFns) {
fn.fn();
}