import { Controller , Get , Injectable , OnModuleInit , Logger , } from '@nestjs/common' ; import MemoryStream = require ( 'memorystream' ) ; import pino from 'pino' ; import { FastifyAdapter } from '@nestjs/platform-fastify' ; import { TestCase } from './utils/test-case' ; import { LogsContainer } from './utils/logs' ; import { platforms } from './utils/platforms' ; describe ( 'useExisting property' , ( ) => { describe ( FastifyAdapter . name , ( ) => { it ( 'should use adapter logger in req context and default beyond' , async ( ) => { const stream = new MemoryStream ( '' , { readable : false } ) ; const inReqContextMsg = Math . random ( ) . toString ( ) ; const outReqContextMsg = Math . random ( ) . toString ( ) ; @ Injectable ( ) class TestService implements OnModuleInit { private readonly logger = new Logger ( TestService . name ) ; someMethod ( ) { this . logger . log ( inReqContextMsg ) ; } onModuleInit ( ) { this . logger . log ( outReqContextMsg ) ; } } @ Controller ( '/' ) class TestController { constructor ( private readonly service ) { } @ Get ( '/' ) get ( ) { this . service . someMethod ( ) ; return { } ; } } await new TestCase ( new FastifyAdapter ( { logger : pino ( stream ) } ) , { controllers : [ TestController ] , providers : [ TestService ] , } ) . forRoot ( { useExisting : true } ) . run ( ) ; const logs = LogsContainer . from ( stream ) ; expect ( logs . some ( ( v ) => v . msg === inReqContextMsg ) ) . toBeTruthy ( ) ; expect ( logs . some ( ( v ) => v . msg === outReqContextMsg ) ) . toBeFalsy ( ) ; } ) ; } ) ; } ) ; describe ( 'pass existing pino instance' , ( ) => { for ( const PlatformAdapter of platforms ) { describe ( PlatformAdapter . name , ( ) => { it ( 'should use passed instance out of context' , async ( ) => { const stream = new MemoryStream ( '' , { readable : false } ) ; const msg = Math . random ( ) . toString ( ) ; @ Injectable ( ) class TestService implements OnModuleInit { private readonly logger = new Logger ( TestService . name ) ; onModuleInit ( ) { this . logger . log ( msg ) ; } } @ Controller ( '/' ) class TestController { @ Get ( '/' ) get ( ) { return { } ; } } const instance = pino ( stream ) ; await new TestCase ( new PlatformAdapter ( ) , { controllers : [ TestController ] , providers : [ TestService ] , } ) . forRoot ( { pinoHttp : { logger : instance } } , true ) . run ( ) ; const logs = LogsContainer . from ( stream ) ; expect ( logs . some ( ( v ) => v . msg === msg ) ) . toBeTruthy ( ) ; } ) ; } ) ; } } ) ;