@serenity-js/core
Version:
The core Serenity/JS framework, providing the Screenplay Pattern interfaces, as well as the test reporting and integration infrastructure
79 lines • 4.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PerformActivities = void 0;
const tiny_types_1 = require("tiny-types");
const index_js_1 = require("../../errors/index.js");
const index_js_2 = require("../../events/index.js");
const index_js_3 = require("../../io/index.js");
const index_js_4 = require("../../model/index.js");
const index_js_5 = require("../../model/index.js");
const Interaction_js_1 = require("../Interaction.js");
const Ability_js_1 = require("./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
*/
class PerformActivities extends Ability_js_1.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_js_1.Interaction
? [index_js_2.InteractionStarts, index_js_2.InteractionFinished]
: [index_js_2.TaskStarts, index_js_2.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 index_js_5.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 (0, tiny_types_1.match)(error)
.when(index_js_1.ImplementationPendingError, _ => new index_js_5.ImplementationPending(error))
.when(index_js_1.TestCompromisedError, _ => new index_js_5.ExecutionCompromised(error))
.when(index_js_1.AssertionError, _ => new index_js_5.ExecutionFailedWithAssertionError(error))
.when(Error, _ => {
// Mocha
if (/AssertionError/.test(error.constructor.name)) {
return new index_js_5.ExecutionFailedWithAssertionError(error);
}
// Playwright Test
if (/TestSkipError/.test(error.constructor.name)) {
return new index_js_4.ExecutionSkipped(error);
}
return new index_js_5.ExecutionFailedWithError(error);
})
.else(_ => new index_js_5.ExecutionFailedWithError(error));
}
detailsOf(name, instantiationLocation) {
return new index_js_5.ActivityDetails(new index_js_5.Name(name), instantiationLocation);
}
nameOf(activity) {
const template = index_js_3.ValueInspector.hasItsOwnToString(activity)
? activity.toString()
: `#actor performs ${activity.constructor.name}`;
return template.replaceAll('#actor', this.actor.name);
}
}
exports.PerformActivities = PerformActivities;
//# sourceMappingURL=PerformActivities.js.map