-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from kauandotnet/hotfix/nest-di-base-exception
fix: use nest di to provide applicationRef
- Loading branch information
Showing
6 changed files
with
94 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/nestjs-logger/src/__fixture__/app.controller.fixture.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
packages/nestjs-logger/src/logger-exception.filter.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters