Skip to content

Commit

Permalink
Merge pull request #129 from kauandotnet/hotfix/nest-di-base-exception
Browse files Browse the repository at this point in the history
fix: use nest di to provide applicationRef
  • Loading branch information
MrMaz authored Nov 28, 2023
2 parents aaaf650 + f3e304d commit 10b8b26
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 43 deletions.
3 changes: 2 additions & 1 deletion packages/nestjs-logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Controller, Get } from '@nestjs/common';

@Controller()
export class AppControllerFixture {
@Get('throw')
throwError() {
throw new Error('Intentional Error');
}
}
9 changes: 9 additions & 0 deletions packages/nestjs-logger/src/__fixture__/app.module.fixture.ts
Original file line number Diff line number Diff line change
@@ -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 {}
63 changes: 44 additions & 19 deletions packages/nestjs-logger/src/logger-exception.filter.spec.ts
Original file line number Diff line number Diff line change
@@ -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<HttpServer>();
loggerService = mock<LoggerService>();
argumentsHost = mock<ArgumentsHost>();
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>(LoggerService);
loggerExceptionFilter = module.get<LoggerExceptionFilter>(
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<ArgumentsHost>());

expect(loggerExceptionFilter).toBeDefined();
expect(spyLoggerServiceException).toBeCalledTimes(1);
});

afterEach(async () => {
await app.close();
});
});
14 changes: 3 additions & 11 deletions packages/nestjs-logger/src/logger-exception.filter.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -29,10 +23,8 @@ export class LoggerExceptionFilter extends BaseExceptionFilter {
constructor(
@Inject(LoggerService)
private loggerService: LoggerService,
@Inject(HttpAdapterHost)
applicationRef?: HttpServer,
) {
super(applicationRef);
super();
}

/**
Expand Down
39 changes: 27 additions & 12 deletions packages/nestjs-logger/src/logger.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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>(LoggerService);
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
});
});
});
});

0 comments on commit 10b8b26

Please sign in to comment.