|
| 1 | +import { Test, TestingModule } from '@nestjs/testing' |
| 2 | +import { BadRequestException, HttpStatus } from '@nestjs/common' |
| 3 | + |
| 4 | +import { ResponseFilter } from './response.exception-filter' |
| 5 | +import { mockArgumentsHost, mockLogger } from '../test/__mocks__/common.mocks' |
| 6 | + |
| 7 | +describe('Response exception filter suite', () => { |
| 8 | + let exceptionFilter: ResponseFilter |
| 9 | + |
| 10 | + describe('when stacktrace enabled', () => { |
| 11 | + beforeEach(async () => { |
| 12 | + jest.clearAllMocks() |
| 13 | + const module: TestingModule = await Test.createTestingModule({ |
| 14 | + providers: [ |
| 15 | + { |
| 16 | + provide: ResponseFilter, |
| 17 | + useValue: new ResponseFilter(), |
| 18 | + }, |
| 19 | + ], |
| 20 | + }) |
| 21 | + .setLogger(mockLogger) |
| 22 | + .compile() |
| 23 | + |
| 24 | + exceptionFilter = module.get<ResponseFilter>(ResponseFilter) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should be defined', () => { |
| 28 | + expect(exceptionFilter).toBeDefined() |
| 29 | + }) |
| 30 | + |
| 31 | + it('should have stack in error object', () => { |
| 32 | + const response = exceptionFilter.catch( |
| 33 | + new Error('random error'), |
| 34 | + mockArgumentsHost, |
| 35 | + ) |
| 36 | + expect(response).toBeDefined() |
| 37 | + expect(response.data).toBeUndefined() |
| 38 | + expect(response.error).toBeDefined() |
| 39 | + expect(response.error.stack).toBeDefined() |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + describe('when stacktrace disabled | stacktrace does not matter', () => { |
| 44 | + beforeEach(async () => { |
| 45 | + jest.clearAllMocks() |
| 46 | + const module: TestingModule = await Test.createTestingModule({ |
| 47 | + providers: [ |
| 48 | + { |
| 49 | + provide: ResponseFilter, |
| 50 | + useValue: new ResponseFilter({ stack: false }), |
| 51 | + }, |
| 52 | + ], |
| 53 | + }) |
| 54 | + .setLogger(mockLogger) |
| 55 | + .compile() |
| 56 | + |
| 57 | + exceptionFilter = module.get<ResponseFilter>(ResponseFilter) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should be defined', () => { |
| 61 | + expect(exceptionFilter).toBeDefined() |
| 62 | + }) |
| 63 | + |
| 64 | + it('should not have stack in error object', () => { |
| 65 | + const response = exceptionFilter.catch( |
| 66 | + new Error('random error'), |
| 67 | + mockArgumentsHost, |
| 68 | + ) |
| 69 | + expect(response).toBeDefined() |
| 70 | + expect(response.data).toBeUndefined() |
| 71 | + expect(response.error).toBeDefined() |
| 72 | + expect(response.error.stack).toBeUndefined() |
| 73 | + }) |
| 74 | + |
| 75 | + it('should set status to INTERNAL_SERVER_ERROR if error is not instanceof HttpException', () => { |
| 76 | + const response = exceptionFilter.catch( |
| 77 | + new Error('I am not an HttpException'), |
| 78 | + mockArgumentsHost, |
| 79 | + ) |
| 80 | + expect(response).toBeDefined() |
| 81 | + expect(response.data).toBeUndefined() |
| 82 | + expect(response.error).toBeDefined() |
| 83 | + expect(response.error.code).toEqual( |
| 84 | + HttpStatus.INTERNAL_SERVER_ERROR, |
| 85 | + ) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should set given status when error is instanceof HttpException', () => { |
| 89 | + const response = exceptionFilter.catch( |
| 90 | + new BadRequestException( |
| 91 | + 'I am an HttpException with status code 400', |
| 92 | + ), |
| 93 | + mockArgumentsHost, |
| 94 | + ) |
| 95 | + expect(response).toBeDefined() |
| 96 | + expect(response.data).toBeUndefined() |
| 97 | + expect(response.error).toBeDefined() |
| 98 | + expect(response.error.code).toEqual(HttpStatus.BAD_REQUEST) |
| 99 | + }) |
| 100 | + }) |
| 101 | +}) |
0 commit comments