jest . mock ( '../../layouts/layouts' , ( ) => { const { schema } = require ( '@osd/config-schema' ) ; return { Layouts : { configSchema : schema . object ( { kind : schema . literal ( 'mock' ) , } ) , } , } ; } ) ; import { LogRecord , LogLevel } from '@osd/logging' ; import { ConsoleAppender } from './console_appender' ; test ( '`configSchema` creates correct schema.' , ( ) => { const appenderSchema = ConsoleAppender . configSchema ; const validConfig = { kind : 'console' , layout : { kind : 'mock' } } ; expect ( appenderSchema . validate ( validConfig ) ) . toEqual ( { kind : 'console' , layout : { kind : 'mock' } , } ) ; const wrongConfig1 = { kind : 'not-console' , layout : { kind : 'mock' } } ; expect ( ( ) => appenderSchema . validate ( wrongConfig1 ) ) . toThrow ( ) ; const wrongConfig2 = { kind : 'file' , layout : { kind : 'mock' } , path : 'path' } ; expect ( ( ) => appenderSchema . validate ( wrongConfig2 ) ) . toThrow ( ) ; } ) ; test ( '`append()` correctly formats records and pushes them to console.' , ( ) => { jest . spyOn ( global . console , 'log' ) . mockImplementation ( ( ) => { } ) ; const records = [ { context : 'context-1' , level : LogLevel . All , message : 'message-1' , timestamp : new Date ( ) , pid : 5355 , } , { context : 'context-2' , level : LogLevel . Trace , message : 'message-2' , timestamp : new Date ( ) , pid : 5355 , } , { context : 'context-3' , error : new Error ( 'Error' ) , level : LogLevel . Fatal , message : 'message-3' , timestamp : new Date ( ) , pid : 5355 , } , ] ; const appender = new ConsoleAppender ( { format ( record ) { return `mock-${ JSON . stringify ( record ) }` ; } , } ) ; for ( const record of records ) { appender . append ( record ) ; expect ( console . log ) . toHaveBeenCalledWith ( `mock-${ JSON . stringify ( record ) }` ) ; } expect ( console . log ) . toHaveBeenCalledTimes ( records . length ) ; } ) ;