import { benchmark , Benchmark , Measurement } from "." ; import { BenchmarkFileState } from "./etc" ; const MOCHA_EVENT_TEST_BEGIN = "test" ; const MOCHA_EVENT_RUN_END = "end" ; type KarmaLoggedRecord = { description : Array < string > ; durations : Array < number > ; } ; interface MochaReporterOptions { reporterOptions : { inferDescriptions ? : boolean ; printReportAtEnd ? : boolean ; } ; } interface KarmaReporterOptions { inferBrowsers ? : boolean ; printReportAtEnd ? : boolean ; } interface JestReporterOptions { keepStateAtStart ? : boolean ; keepStateAtEnd ? : boolean ; printReportAtEnd ? : boolean ; } export class JestReporter implements jest . Reporter { options : JestReporterOptions = { keepStateAtStart : false , keepStateAtEnd : false , printReportAtEnd : true } ; constructor ( testData ? , options ? ) { if ( options ) { this . options = { ... this . options , ... options } ; } } static initializeKelonio ( ) { const state = new BenchmarkFileState ( ) ; benchmark . events . on ( "record" , ( description , measurement ) => { const b = new Benchmark ( ) ; if ( state . exists ( ) ) { b . data = state . read ( ) ; } b . incorporate ( description , measurement ) ; state . write ( b . data ) ; } ) ; } onRunStart ( ) { const state = new BenchmarkFileState ( ) ; if ( this . options . keepStateAtStart ) { state . append ( { } ) ; } else { state . write ( { } ) ; } } onRunComplete ( ) { const state = new BenchmarkFileState ( ) ; if ( ! state . exists ( ) ) { throw new Error ( "The Kelonio reporter for Jest requires benchmark serialization." + " Make sure to call `JestReporter.initializeKelonio()`." ) ; } const b = new Benchmark ( ) ; b . data = state . read ( ) ; if ( this . options . printReportAtEnd ) { console . log ( `\n${ b . report ( ) }` ) ; } if ( ! this . options . keepStateAtEnd ) { state . delete ( ) ; } } } export class KarmaReporter { protected onBrowserLog : ( browser : string , log : string , type : string ) => void ; protected onRunComplete : ( ) => void ; static initializeKelonio ( ) { benchmark . events . on ( "record" , ( description , measurement ) => { ( < any > window ) . __karma__ . log ( "kelonio" , [ JSON . stringify ( { description , durations : measurement . durations } ) ] ) ; } ) ; } constructor ( baseReporterDecorator , config , logger , helper , formatError ) { baseReporterDecorator ( this ) ; const activeConfig = { ... { inferBrowsers : true , printReportAtEnd : true } , ... config . kelonioReporter } ; const b = new Benchmark ( ) ; this . onBrowserLog = ( browser , log , type ) => { if ( type === "kelonio" ) { const parsed = JSON . parse ( log . slice ( 1 , - 1 ) ) ; const browserDescription = activeConfig . inferBrowsers ? [ browser ] : [ ] ; b . incorporate ( [ ... browserDescription , ... parsed . description ] , new Measurement ( parsed . durations ) ) ; } } ; this . onRunComplete = ( ) => { if ( activeConfig . printReportAtEnd ) { ( < any > this ) . write ( `${ b . report ( ) }\n` ) ; } } ; } } export class MochaReporter { constructor ( runner , options ) { const b = new Benchmark ( ) ; let baseDescription = [ ] ; const inferDescriptions = options . reporterOptions . inferDescriptions ?? true ; const printReportAtEnd = options . reporterOptions . printReportAtEnd ?? true ; benchmark . events . on ( "record" , ( description , measurement ) => { b . incorporate ( baseDescription . concat ( description ) , measurement ) ; } ) ; if ( inferDescriptions ) { runner . on ( MOCHA_EVENT_TEST_BEGIN , test => { baseDescription = test . titlePath ( ) ; } ) ; } runner . once ( MOCHA_EVENT_RUN_END , ( ) => { if ( printReportAtEnd ) { console . log ( `\n${ b . report ( ) }` ) ; } } ) ; } }