@serenity-js/core
Version:
The core Serenity/JS framework, providing the Screenplay Pattern interfaces, as well as the test reporting and integration infrastructure
46 lines • 2.56 kB
JavaScript
import path from 'node:path';
import { ErrorStackParser } from '../errors/index.js';
import { FileSystemLocation, Path } from '../io/index.js';
import { Describable } from './questions/Describable.js';
/**
* **Activities** represents [tasks](https://serenity-js.org/api/core/class/Task/) and [interactions](https://serenity-js.org/api/core/class/Interaction/) to be performed by an [actor](https://serenity-js.org/api/core/class/Actor/).
*
* Learn more about:
* - [Performing activities at multiple levels](https://serenity-js.org/handbook/design/screenplay-pattern#performing-activities-at-multiple-levels)
* - [`Actor`](https://serenity-js.org/api/core/class/Actor/)
* - [`PerformsActivities`](https://serenity-js.org/api/core/interface/PerformsActivities/)
* - [Command design pattern on Wikipedia](https://en.wikipedia.org/wiki/Command_pattern)
*
* @group Screenplay Pattern
*/
export class Activity extends Describable {
static errorStackParser = new ErrorStackParser();
#location;
constructor(description, location = Activity.callerLocation(5)) {
super(description);
this.#location = location;
}
/**
* Returns the location where this [`Activity`](https://serenity-js.org/api/core/class/Activity/) was instantiated.
*/
instantiationLocation() {
return this.#location;
}
static callerLocation(frameOffset) {
const originalStackTraceLimit = Error.stackTraceLimit;
Error.stackTraceLimit = 30;
const error = new Error('Caller location marker');
Error.stackTraceLimit = originalStackTraceLimit;
const nonSerenityNodeModulePattern = new RegExp(`node_modules` + `\\` + path.sep + `(?!@serenity-js` + `\\` + path.sep + `)`);
const frames = this.errorStackParser.parse(error);
const userLandFrames = frames.filter(frame => !(frame?.fileName.startsWith('node:') || // node 16 and 18
frame?.fileName.startsWith('internal') || // node 14
nonSerenityNodeModulePattern.test(frame?.fileName) // ignore node_modules, except for @serenity-js/*
));
const index = Math.min(Math.max(1, frameOffset), userLandFrames.length - 1);
// use the desired user-land frame, or the last one from the stack trace for internal invocations
const invocationFrame = userLandFrames[index] || frames.at(-1);
return new FileSystemLocation(Path.from(invocationFrame.fileName?.replace(/^file:/, '')), invocationFrame.lineNumber, invocationFrame.columnNumber);
}
}
//# sourceMappingURL=Activity.js.map