@serenity-js/core
Version:
The core Serenity/JS framework, providing the Screenplay Pattern interfaces, as well as the test reporting and integration infrastructure
75 lines • 3.93 kB
JavaScript
import { match } from 'tiny-types';
import { AssertionError, ImplementationPendingError, TestCompromisedError } from '../../errors/index.js';
import { InteractionFinished, InteractionStarts, TaskFinished, TaskStarts } from '../../events/index.js';
import { ValueInspector } from '../../io/index.js';
import { ExecutionSkipped } from '../../model/index.js';
import { ActivityDetails, ExecutionCompromised, ExecutionFailedWithAssertionError, ExecutionFailedWithError, ExecutionSuccessful, ImplementationPending, Name } from '../../model/index.js';
import { Interaction } from '../Interaction.js';
import { Ability } from './Ability.js';
/**
* An [`Ability`](https://serenity-js.org/api/core/class/Ability/) that enables an [`Actor`](https://serenity-js.org/api/core/class/Actor/) to perform a given [`Activity`](https://serenity-js.org/api/core/class/Activity/).
*
* [`PerformActivities`](https://serenity-js.org/api/core/class/PerformActivities/) is used internally by [`Actor.attemptsTo`](https://serenity-js.org/api/core/class/Actor/#attemptsTo), and it is unlikely you'll ever need to use it directly in your code.
* That is, unless you're building a custom Serenity/JS extension and want to override the default behaviour of the framework,
* in which case you should check out the [Contributor's Guide](https://serenity-js.org/community/contributing).
*
* @group Abilities
*/
export class PerformActivities extends Ability {
actor;
stage;
constructor(actor, stage) {
super();
this.actor = actor;
this.stage = stage;
}
async perform(activity) {
const sceneId = this.stage.currentSceneId();
const details = this.detailsOf(this.nameOf(activity), activity.instantiationLocation());
const activityId = this.stage.assignNewActivityId(details);
const [activityStarts, activityFinished] = activity instanceof Interaction
? [InteractionStarts, InteractionFinished]
: [TaskStarts, TaskFinished];
try {
this.stage.announce(new activityStarts(sceneId, activityId, details, this.stage.currentTime()));
await activity.performAs(this.actor);
const name = await activity.describedBy(this.actor);
this.stage.announce(new activityFinished(sceneId, activityId, this.detailsOf(name, activity.instantiationLocation()), new ExecutionSuccessful(), this.stage.currentTime()));
}
catch (error) {
this.stage.announce(new activityFinished(sceneId, activityId, details, this.outcomeFor(error), this.stage.currentTime()));
throw error;
}
finally {
await this.stage.waitForNextCue();
}
}
outcomeFor(error) {
return match(error)
.when(ImplementationPendingError, _ => new ImplementationPending(error))
.when(TestCompromisedError, _ => new ExecutionCompromised(error))
.when(AssertionError, _ => new ExecutionFailedWithAssertionError(error))
.when(Error, _ => {
// Mocha
if (/AssertionError/.test(error.constructor.name)) {
return new ExecutionFailedWithAssertionError(error);
}
// Playwright Test
if (/TestSkipError/.test(error.constructor.name)) {
return new ExecutionSkipped(error);
}
return new ExecutionFailedWithError(error);
})
.else(_ => new ExecutionFailedWithError(error));
}
detailsOf(name, instantiationLocation) {
return new ActivityDetails(new Name(name), instantiationLocation);
}
nameOf(activity) {
const template = ValueInspector.hasItsOwnToString(activity)
? activity.toString()
: `#actor performs ${activity.constructor.name}`;
return template.replaceAll('#actor', this.actor.name);
}
}
//# sourceMappingURL=PerformActivities.js.map