import * as globalAgent from 'global-agent' ; import { HttpService } from '@nestjs/axios' ; import { Test , TestingModule } from '@nestjs/testing' ; import { LoggerService } from '@fc/logger' ; import { HttpProxyService } from './http-proxy.service' ; jest . mock ( 'global-agent' ) ; const globalAny = global ; describe ( 'HttpProxyService' , ( ) => { let service ; const loggerMock = { setContext : jest . fn ( ) , debug : jest . fn ( ) , } ; const httpMock = { axiosRef : { defaults : { proxy : 'shouldNotExist' } , } , } ; const bootstrapMock = jest . spyOn ( globalAgent , 'bootstrap' ) ; beforeEach ( async ( ) => { const module = await Test . createTestingModule ( { providers : [ HttpProxyService , LoggerService , HttpService ] , } ) . overrideProvider ( LoggerService ) . useValue ( loggerMock ) . overrideProvider ( HttpService ) . useValue ( httpMock ) . compile ( ) ; service = module . get < HttpProxyService > ( HttpProxyService ) ; jest . resetAllMocks ( ) ; bootstrapMock . mockImplementation ( ( ) => { global [ 'GLOBAL_AGENT' ] = 'mockValue' ; } ) ; httpMock . axiosRef . defaults . proxy = 'shouldNotExist' ; } ) ; afterEach ( ( ) => { bootstrapMock . mockRestore ( ) ; } ) ; it ( 'should be defined' , ( ) => { expect ( service ) . toBeDefined ( ) ; } ) ; describe ( 'onModuleInit' , ( ) => { it ( 'should call bootstrap from global-agent' , ( ) => { service . onModuleInit ( ) ; expect ( bootstrapMock ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'should call bootstrap from global-agent' , ( ) => { service . onModuleInit ( ) ; expect ( globalAny . GLOBAL_AGENT ) . toBeDefined ( ) ; } ) ; it ( 'should shutdown proxy for axios' , ( ) => { service . onModuleInit ( ) ; const { axiosRef : { defaults : { proxy } , } , } = httpMock ; expect ( proxy ) . toBe ( false ) ; } ) ; } ) ; } ) ;