Skip to content
Permalink
Browse files
fix(serenity-bdd): serenityBDDReporter reports events that occured in…
… Mocha's before and after hooks
  • Loading branch information
jan-molak committed Jul 4, 2020
1 parent cb051b8 commit a8e0ccb20aa4bd8fed4c63816b4a09254fa09c24
Show file tree
Hide file tree
Showing 9 changed files with 533 additions and 64 deletions.
@@ -1,7 +1,7 @@
import 'mocha';

import { expect } from '@integration/testing-tools';
import { Duration, StageManager } from '@serenity-js/core';
import { StageManager } from '@serenity-js/core';
import {
ActivityRelatedArtifactArchived,
ActivityRelatedArtifactGenerated,
@@ -64,23 +64,6 @@ describe('SerenityBDDReporter', () => {
expect(report.testSteps[0].result).to.equal('SUCCESS');
});

it('ignores any activities that occurred after the scene has finished', () => {
const pickACard = new ActivityDetails(new Name('Pick the default credit card'));

given(reporter).isNotifiedOfFollowingEvents(
new SceneStarts(defaultCardScenario),
new SceneFinished(defaultCardScenario, new ExecutionSuccessful()),
// activities that occur outside of the scenario, i.e. in Mocha's `after` hook, should be ignored by this reporter
new TaskStarts(pickACard),
new TaskFinished(pickACard, new ExecutionSuccessful()),
new TestRunFinishes(),
);

const report: SerenityBDDReport = stageManager.notifyOf.firstCall.lastArg.artifact.map(_ => _);

expect(report.testSteps).to.have.lengthOf(0);
});

/**
* @test {SerenityBDDReporter}
* @test {SceneStarts}
@@ -0,0 +1,50 @@
import 'mocha';
import { contentsOf, EventRecorder, EventStreamEmitter, expect } from '@integration/testing-tools';
import { Actor, Cast, Clock, Duration, Stage, StageManager } from '@serenity-js/core';
import { ArtifactGenerated, DomainEvent } from '@serenity-js/core/lib/events';
import { TestReport } from '@serenity-js/core/lib/model';
import { SerenityBDDReporter } from '../../../../../src';

describe('SerenityBDDReporter', () => {

let reporter: SerenityBDDReporter,
stage: Stage,
emitter: EventStreamEmitter,
recorder: EventRecorder;

beforeEach(() => {
stage = new Stage(new Extras(), new StageManager(Duration.ofMilliseconds(250), new Clock()));
emitter = new EventStreamEmitter(stage);
recorder = new EventRecorder([], stage);

reporter = new SerenityBDDReporter(stage);

stage.assign(recorder);
stage.assign(reporter);
});

/** @test {SerenityBDDReporter} */
it(`includes orphaned events that happened before the scenario in first scenario, and those that happened after - in last scenario`, () => emitter.emit(
contentsOf(__dirname, 'examples', 'scenario_with_interactions_in_before_and_after_hooks.log')
).then(() => {
const reports: TestReport[] = testReportsFrom(recorder.events);
expect(reports).to.have.lengthOf(1);

const generated = reports[0].map(report => report);
const expected = JSON.parse(contentsOf(__dirname, 'examples', 'scenario_with_interactions_in_before_and_after_hooks.json'));

expect(generated).to.deep.equal(expected);
}));
});

function testReportsFrom(events: DomainEvent[]): TestReport[] {
return events
.filter((e: DomainEvent) => e instanceof ArtifactGenerated && e.artifact instanceof TestReport)
.map((e: ArtifactGenerated) => e.artifact);
}

class Extras implements Cast {
prepare(actor: Actor): Actor {
return actor;
}
}

0 comments on commit a8e0ccb

Please sign in to comment.