import { TestRunnerVisitor } from './testRunnerVisitor' ; import { LoggerTestReporterDecorator , LoggerTestReporterDecoratorConfiguration , } from './decorators/loggerTestReporterDecorator' ; import { TestVisitor } from './testVisitor' ; import { Report } from '../../reporting/report/report' ; import { Logger } from '../../logger/logger' ; import { TapTestReporterDecorator } from './decorators/tapTestReporterDecorator' ; import { TestyConfig } from '../../interfaces/config' ; export type ReporterType = 'standard' | 'TAP' ; type VisitorConstructor = ( baseVisitor : TestVisitor < Report > , logger : Logger ) => TestVisitor < Report > ; export class TestVisitorFactory { constructor ( private logger ) { } public getRunner ( config ) { let testRunnerVisitor = new TestRunnerVisitor ( process , config ) ; const reporterConstructors = [ ] ; if ( config . reporters != null ) { for ( const reporter in config . reporters ) { if ( ! config . reporters . hasOwnProperty ( reporter ) ) { continue ; } const reporterConfig = config . reporters [ reporter ] ; reporterConstructors . push ( this . getReporterConstructor ( reporter , reporterConfig ) ) ; } } if ( config . reporter != null ) { this . logger . warn ( 'WARNING! The "reporter" configuration is deprecated. Prefer using the "reporters" configuration.' ) ; reporterConstructors . push ( this . getReporterConstructor ( config . reporter || 'standard' ) ) ; } if ( reporterConstructors . length === 0 ) { reporterConstructors . push ( this . getReporterConstructor ( 'standard' ) ) ; } for ( const reporterConstructor of reporterConstructors ) { testRunnerVisitor = reporterConstructor ( testRunnerVisitor , this . logger . create ( ) ) ; } return testRunnerVisitor ; } private getReporterConstructor ( reporterName , config ? ) { if ( reporterName === 'standard' ) { const c = config as LoggerTestReporterDecoratorConfiguration ; return ( baseVisitor , logger ) => new LoggerTestReporterDecorator ( baseVisitor , logger , c ) ; } if ( reporterName === 'TAP' ) { return ( baseVisitor , logger ) => new TapTestReporterDecorator ( baseVisitor , logger ) ; } throw new Error ( `The ${ reporterName } reporter is not supported.` ) ; } }