UNPKG

@stryker-mutator/core

Version:

The extendable JavaScript mutation testing framework

63 lines 2.62 kB
import { commonTokens, PluginKind } from '@stryker-mutator/api/plugin'; import { tokens } from 'typed-inject'; import { coreTokens } from '../di/index.js'; export class BroadcastReporter { constructor(options, pluginCreator, log) { this.options = options; this.pluginCreator = pluginCreator; this.log = log; this.reporters = {}; this.options.reporters.forEach((reporterName) => this.createReporter(reporterName)); this.logAboutReporters(); } createReporter(reporterName) { if (reporterName === 'progress' && !process.stdout.isTTY) { this.log.info('Detected that current console does not support the "progress" reporter, downgrading to "progress-append-only" reporter'); reporterName = 'progress-append-only'; } this.reporters[reporterName] = this.pluginCreator.create(PluginKind.Reporter, reporterName); } logAboutReporters() { const reporterNames = Object.keys(this.reporters); if (reporterNames.length) { if (this.log.isDebugEnabled()) { this.log.debug(`Broadcasting to reporters ${JSON.stringify(reporterNames)}`); } } else { this.log.warn("No reporter configured. Please configure one or more reporters in the (for example: reporters: ['progress'])"); } } broadcast(methodName, ...eventArgs) { return Promise.all(Object.entries(this.reporters).map(async ([reporterName, reporter]) => { if (reporter[methodName]) { try { await reporter[methodName](...eventArgs); } catch (error) { this.handleError(error, methodName, reporterName); } } })); } onDryRunCompleted(event) { void this.broadcast('onDryRunCompleted', event); } onMutationTestingPlanReady(event) { void this.broadcast('onMutationTestingPlanReady', event); } onMutantTested(result) { void this.broadcast('onMutantTested', result); } onMutationTestReportReady(report, metrics) { void this.broadcast('onMutationTestReportReady', report, metrics); } async wrapUp() { await this.broadcast('wrapUp'); } handleError(error, methodName, reporterName) { this.log.error(`An error occurred during '${methodName}' on reporter '${reporterName}'.`, error); } } BroadcastReporter.inject = tokens(commonTokens.options, coreTokens.pluginCreator, commonTokens.logger); //# sourceMappingURL=broadcast-reporter.js.map