diff --git a/packages/nestjs-logger/package.json b/packages/nestjs-logger/package.json index 32c5d405e..828831462 100644 --- a/packages/nestjs-logger/package.json +++ b/packages/nestjs-logger/package.json @@ -22,7 +22,8 @@ "devDependencies": { "@nestjs/testing": "^9.0.0", "@sentry/types": "^6.13.3", - "jest-mock-extended": "^2.0.4" + "jest-mock-extended": "^2.0.4", + "supertest": "^6.3.3" }, "peerDependencies": { "class-validator": "*", diff --git a/packages/nestjs-logger/src/__fixture__/app.controller.fixture.ts b/packages/nestjs-logger/src/__fixture__/app.controller.fixture.ts new file mode 100644 index 000000000..0069ea7f2 --- /dev/null +++ b/packages/nestjs-logger/src/__fixture__/app.controller.fixture.ts @@ -0,0 +1,9 @@ +import { Controller, Get } from '@nestjs/common'; + +@Controller() +export class AppControllerFixture { + @Get('throw') + throwError() { + throw new Error('Intentional Error'); + } +} diff --git a/packages/nestjs-logger/src/__fixture__/app.module.fixture.ts b/packages/nestjs-logger/src/__fixture__/app.module.fixture.ts new file mode 100644 index 000000000..f2206c4e3 --- /dev/null +++ b/packages/nestjs-logger/src/__fixture__/app.module.fixture.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { AppControllerFixture } from './app.controller.fixture'; +import { LoggerModule } from '../logger.module'; + +@Module({ + controllers: [AppControllerFixture], + imports: [LoggerModule.register({})], +}) +export class AppModuleFixture {} diff --git a/packages/nestjs-logger/src/logger-exception.filter.spec.ts b/packages/nestjs-logger/src/logger-exception.filter.spec.ts index 4d09c90df..eeff2b181 100644 --- a/packages/nestjs-logger/src/logger-exception.filter.spec.ts +++ b/packages/nestjs-logger/src/logger-exception.filter.spec.ts @@ -1,40 +1,65 @@ -import { mock } from 'jest-mock-extended'; - -import { ArgumentsHost, HttpServer } from '@nestjs/common'; - +import { ArgumentsHost, INestApplication } from '@nestjs/common'; +import { TestingModule, Test } from '@nestjs/testing'; import { LoggerExceptionFilter } from './logger-exception.filter'; import { LoggerService } from './logger.service'; +import { mock } from 'jest-mock-extended'; +import { HttpAdapterHost } from '@nestjs/core'; +import { LoggerTransportService } from './logger-transport.service'; describe('LoggerExceptionFilter', () => { - let httpServer: HttpServer; + let app: INestApplication; let loggerService: LoggerService; - let argumentsHost: ArgumentsHost; let loggerExceptionFilter: LoggerExceptionFilter; - let spyLoggerServiceException: jest.SpyInstance; beforeEach(async () => { - httpServer = mock(); - loggerService = mock(); - argumentsHost = mock(); - spyLoggerServiceException = jest.spyOn(loggerService, 'exception'); + const module: TestingModule = await Test.createTestingModule({ + providers: [ + LoggerService, + LoggerExceptionFilter, + { + provide: LoggerTransportService, + useValue: { + addTransport: jest.fn(), + log: jest.fn(), + }, + }, + { + provide: HttpAdapterHost, + useValue: { + httpAdapter: { + isHeadersSent: jest.fn(), + reply: jest.fn(), + end: jest.fn(), + }, + }, + }, + ], + }).compile(); + + app = module.createNestApplication(); + loggerService = module.get(LoggerService); + loggerExceptionFilter = module.get( + LoggerExceptionFilter, + ); + + await app.init(); }); describe('IsDefined', () => { it('was LoggerExceptionFilter defined', async () => { - expect(httpServer).toBeDefined(); expect(loggerService).toBeDefined(); - expect(argumentsHost).toBeDefined(); + expect(loggerExceptionFilter).toBeDefined(); }); }); it('LoggerExceptionFilter.catch', async () => { - loggerExceptionFilter = new LoggerExceptionFilter( - loggerService, - httpServer, - ); - loggerExceptionFilter.catch(new Error(), argumentsHost); + const spyLoggerServiceException = jest.spyOn(loggerService, 'exception'); + loggerExceptionFilter.catch(new Error(), mock()); - expect(loggerExceptionFilter).toBeDefined(); expect(spyLoggerServiceException).toBeCalledTimes(1); }); + + afterEach(async () => { + await app.close(); + }); }); diff --git a/packages/nestjs-logger/src/logger-exception.filter.ts b/packages/nestjs-logger/src/logger-exception.filter.ts index b07a5c04a..541d24179 100644 --- a/packages/nestjs-logger/src/logger-exception.filter.ts +++ b/packages/nestjs-logger/src/logger-exception.filter.ts @@ -1,11 +1,5 @@ -import { - ArgumentsHost, - Catch, - HttpServer, - Inject, - Injectable, -} from '@nestjs/common'; -import { BaseExceptionFilter, HttpAdapterHost } from '@nestjs/core'; +import { ArgumentsHost, Catch, Inject, Injectable } from '@nestjs/common'; +import { BaseExceptionFilter } from '@nestjs/core'; import { LoggerService } from './logger.service'; @@ -29,10 +23,8 @@ export class LoggerExceptionFilter extends BaseExceptionFilter { constructor( @Inject(LoggerService) private loggerService: LoggerService, - @Inject(HttpAdapterHost) - applicationRef?: HttpServer, ) { - super(applicationRef); + super(); } /** diff --git a/packages/nestjs-logger/src/logger.service.spec.ts b/packages/nestjs-logger/src/logger.service.spec.ts index 1fc253a15..1b04f7c70 100644 --- a/packages/nestjs-logger/src/logger.service.spec.ts +++ b/packages/nestjs-logger/src/logger.service.spec.ts @@ -1,9 +1,15 @@ -import { ConsoleLogger, NotFoundException } from '@nestjs/common'; +import { + ConsoleLogger, + INestApplication, + NotFoundException, +} from '@nestjs/common'; import { Test } from '@nestjs/testing'; import { LoggerTransportInterface } from './interfaces/logger-transport.interface'; import { LoggerTransportService } from './logger-transport.service'; import { LoggerService } from './logger.service'; +import { AppModuleFixture } from './__fixture__/app.module.fixture'; +import supertest from 'supertest'; describe('LoggerService', () => { let loggerService: LoggerService; @@ -17,19 +23,11 @@ describe('LoggerService', () => { let spyWarn: jest.SpyInstance; let errorMessage: string; let context: string; + let app: INestApplication; beforeEach(async () => { const moduleRef = await Test.createTestingModule({ - providers: [ - LoggerService, - { - provide: LoggerTransportService, - useValue: { - addTransport: jest.fn(), - log: jest.fn(), - }, - }, - ], + imports: [AppModuleFixture], }).compile(); loggerService = moduleRef.get(LoggerService); @@ -58,10 +56,15 @@ describe('LoggerService', () => { context = 'jest'; errorMessage = 'Error Message'; + + app = moduleRef.createNestApplication(); + + await app.init(); }); - afterEach(() => { + afterEach(async () => { jest.clearAllMocks(); + await app.close(); }); describe('IsDefined', () => { @@ -186,5 +189,17 @@ describe('LoggerService', () => { expect(spyLog).toHaveBeenCalledWith(errorMessage, context); expect(spyTransportLog).toBeCalledTimes(1); }); + + it('LoggerService.log', () => { + const spyLoggerService = jest.spyOn(loggerService, 'log'); + + return supertest(app.getHttpServer()) + .get('/throw') + .expect(500) + .then(() => { + expect(spyLoggerService).toHaveBeenCalledTimes(1); + expect(spyLoggerService).toThrowError(); + }); + }); }); });