playwright-bdd
Version:
BDD Testing with Playwright runner
105 lines • 4.62 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const base_1 = __importDefault(require("./base"));
const getColorFns_1 = __importDefault(require("../../cucumber/formatter/getColorFns"));
const requireOrImport_1 = require("../../playwright/requireOrImport");
class CustomReporter extends base_1.default {
constructor(internalOptions, reporterPathOrModule, userOptions = {}) {
super(internalOptions);
this.reporterPathOrModule = reporterPathOrModule;
this.userOptions = userOptions;
this.setOutputStream(this.userOptions.outputFile);
}
printsToStdio() {
// currently always return true, b/c loading of custom reporter is async,
// but printsToStdio() is called synchronously at the beginning.
return true;
}
async init() {
const reporterPath = require.resolve(this.reporterPathOrModule, {
paths: [this.internalOptions.cwd],
});
const module = await (0, requireOrImport_1.requireOrImport)(reporterPath);
this.initFromModule(module, reporterPath);
}
initFromModule(module, reporterPath) {
// Try traditional formatter approach first (default export class with IFormatterOptions).
const defaultExport = extractDefaultExport(module);
if (typeof defaultExport === 'function') {
const formatterOptions = this.buildFormatterOptions();
this.formatter = new defaultExport(formatterOptions);
return;
}
// Fall back to message-printer approach: a named-export class with update(envelope).
// Used e.g. by @cucumber/pretty-formatter v3+.
const PrinterClass = findPrinterConstructor(module);
if (PrinterClass) {
const printer = new PrinterClass({ stream: this.outputStream, options: this.userOptions });
this.eventBroadcaster.on('envelope', (envelope) => printer.update(envelope));
return;
}
const msg = 'file must export a formatter class (default export) or a message printer class.';
throw new Error(`${reporterPath}: ${msg}`);
}
async finished() {
await this.formatter?.finished();
await super.finished();
}
async loadFormatterFromFile() {
// see: https://github.com/microsoft/playwright/blob/main/packages/playwright/src/common/config.ts#L225
const reporterPath = require.resolve(this.reporterPathOrModule, {
paths: [this.internalOptions.cwd],
});
return (0, requireOrImport_1.requireOrImportDefaultFunction)(reporterPath, true);
}
buildFormatterOptions() {
const colorFns = (0, getColorFns_1.default)(this.outputStream, process.env, this.userOptions.colorsEnabled);
return {
cwd: this.internalOptions.cwd,
eventBroadcaster: this.eventBroadcaster,
eventDataCollector: this.eventDataCollector,
parsedArgvOptions: this.userOptions,
colorFns,
stream: this.outputStream,
log: this.outputStream.write.bind(this.outputStream),
cleanup: async () => { },
snippetBuilder: null,
// Build truncated supportCodeLibrary object. It can lead to errors in custom reporters.
// For full object see:
// https://github.com/cucumber/cucumber-js/blob/main/src/support_code_library_builder/types.ts#L160
supportCodeLibrary: {
World: {},
},
};
}
}
exports.default = CustomReporter;
/**
* Extracts the default export from a module (handles both ESM and CJS shapes).
*/
function extractDefaultExport(module) {
if (module && typeof module === 'object' && 'default' in module) {
return module['default'];
}
return typeof module === 'function' ? module : undefined;
}
/**
* Finds a message-printer constructor in a module: a class whose prototype has
* an `update(envelope)` method (e.g. PrettyPrinter from @cucumber/pretty-formatter v3+).
*/
function findPrinterConstructor(module) {
if (!module || typeof module !== 'object')
return undefined;
const values = Object.values(module);
const found = values.find(isPrinterConstructor);
return found;
}
function isPrinterConstructor(value) {
return (typeof value === 'function' &&
Boolean(value.prototype) &&
typeof value.prototype['update'] === 'function');
}
//# sourceMappingURL=custom.js.map