Skip to content

Commit

Permalink
feat(reporting): AssertionError translated to a Test Failure, not a T…
Browse files Browse the repository at this point in the history
…est Error

When a Task of Interaction throw or resolve to an AssertionError, the associated scenario step will

be marked as Failed. This helps to make reporting more accurate.
  • Loading branch information
jan-molak committed Jan 18, 2017
1 parent afff421 commit f3b3066
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
26 changes: 25 additions & 1 deletion spec/api/serenity/recording/step_annotation.spec.ts
Expand Up @@ -83,7 +83,20 @@ describe('Notifiers', () => {
}
}

it('Notifies the Stage Manager when the Activity fails', () =>
class PayWithInvalidCreditCardThrowingAnAssertionError implements Task {
static number(creditCardNumber: string) {
return new PayWithInvalidCreditCardThrowingAnAssertionError(creditCardNumber);
}

constructor(private cardNumber: string) {}

@step('{0} pays with an invalid credit card number #cardNumber')
performAs(actor: PerformsTasks): PromiseLike<void> {
return expect(Promise.resolve(false)).to.eventually.equal(true);
}
}

it('Notifies the Stage Manager when the Activity throws an Error', () =>

expect(bruce.attemptsTo(PayWithInvalidCreditCardThrowingAnError.number('1234 1234 1234 1234')) ).
to.be.rejected.then(() => {
Expand All @@ -93,6 +106,17 @@ describe('Notifiers', () => {
expect(lastEntry.value.error.message).to.equal('Payment failed');
expect(lastEntry.value.result).to.equal(Result.ERROR);
}));

it('Notifies the Stage Manager when the Activity fails', () =>

expect(bruce.attemptsTo(PayWithInvalidCreditCardThrowingAnAssertionError.number('1234 1234 1234 1234'))).
to.be.rejected.then(() => {

let lastEntry = stageManager.readNewJournalEntriesAs('unit-test').pop();

expect(lastEntry.value.error.message).to.equal('expected false to equal true');
expect(Result[lastEntry.value.result]).to.equal(Result[Result.FAILURE]);
}));
});

describe('When things go wrong and the Activity throws an Error', () => {
Expand Down
12 changes: 10 additions & 2 deletions src/serenity/recording/step_annotation.ts
Expand Up @@ -45,11 +45,19 @@ export class Step {
}

private onFailure(activity: Activity, error: Error) {
// todo: sniff the exception to find out about the Result. Did the test fail, or was it compromised?
this.stageManager.notifyOf(new ActivityFinished(new Outcome(activity, Result.ERROR, error)));
this.stageManager.notifyOf(new ActivityFinished(new Outcome(activity, this.resultFrom(error), error)));

return Promise.reject(error);
}

private resultFrom(error: Error): Result {
const constructorOf = e => e && e.constructor ? e.constructor.name : '';

// todo: sniff the exception to find out about the Result. Did the test fail, or was it compromised?
return /AssertionError/.test(constructorOf(error))
? Result.FAILURE
: Result.ERROR;
}
}

export function step<T extends Performable>(stepDescriptionTemplate: string): StepAnnotation<T> {
Expand Down

0 comments on commit f3b3066

Please sign in to comment.