-
Notifications
You must be signed in to change notification settings - Fork 33
feat: enhance unit tests #2117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
feat: enhance unit tests #2117
Changes from all commits
0aa61ff
17e846b
410050c
e6ade2d
6d51ace
0d96363
e729eb1
14a8439
4f60d29
b6cb3ad
1252925
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,300 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Copyright 2025 LY Corporation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* LY Corporation licenses this file to you under the Apache License, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* version 2.0 (the "License"); you may not use this file except in compliance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* with the License. You may obtain a copy of the License at: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* https://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* License for the specific language governing permissions and limitations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* under the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import type { ArgumentsHost } from '@nestjs/common'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { HttpException, HttpStatus } from '@nestjs/common'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { Test } from '@nestjs/testing'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import type { FastifyReply, FastifyRequest } from 'fastify'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { HttpExceptionFilter } from './http-exception.filter'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
describe('HttpExceptionFilter', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let filter: HttpExceptionFilter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let mockRequest: FastifyRequest; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let mockResponse: FastifyReply; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let mockArgumentsHost: ArgumentsHost; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
beforeEach(async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const module = await Test.createTestingModule({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
providers: [HttpExceptionFilter], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}).compile(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter = module.get<HttpExceptionFilter>(HttpExceptionFilter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Mock FastifyRequest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mockRequest = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
url: '/test-endpoint', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
method: 'GET', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
headers: {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
query: {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
params: {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
body: {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} as FastifyRequest; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Mock FastifyReply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mockResponse = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
status: jest.fn().mockReturnThis(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
send: jest.fn().mockReturnThis(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} as unknown as FastifyReply; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Mock ArgumentsHost | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mockArgumentsHost = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
switchToHttp: jest.fn().mockReturnValue({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
getRequest: jest.fn().mockReturnValue(mockRequest), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
getResponse: jest.fn().mockReturnValue(mockResponse), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} as unknown as ArgumentsHost; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
afterEach(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
jest.clearAllMocks(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
describe('catch', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it('should handle string exception response', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exception = new HttpException( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'Test error message', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HttpStatus.BAD_REQUEST, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter.catch(exception, mockArgumentsHost); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.status).toHaveBeenCalledWith(HttpStatus.BAD_REQUEST); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.send).toHaveBeenCalledWith({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
response: 'Test error message', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
path: '/test-endpoint', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it('should handle object exception response', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exceptionResponse = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Validation failed', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error: 'Bad Request', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusCode: HttpStatus.BAD_REQUEST, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exception = new HttpException( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
exceptionResponse, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HttpStatus.BAD_REQUEST, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter.catch(exception, mockArgumentsHost); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.status).toHaveBeenCalledWith(HttpStatus.BAD_REQUEST); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.send).toHaveBeenCalledWith({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Validation failed', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error: 'Bad Request', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusCode: HttpStatus.BAD_REQUEST, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
path: '/test-endpoint', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it('should handle different HTTP status codes', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const statusCodes = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HttpStatus.UNAUTHORIZED, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HttpStatus.FORBIDDEN, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HttpStatus.NOT_FOUND, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HttpStatus.INTERNAL_SERVER_ERROR, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusCodes.forEach((statusCode) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exception = new HttpException('Test error', statusCode); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter.catch(exception, mockArgumentsHost); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.status).toHaveBeenCalledWith(statusCode); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.send).toHaveBeenCalledWith({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
response: 'Test error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
path: '/test-endpoint', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+111
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Account for Each call to expect(mockResponse.send).toHaveBeenCalledWith({
response: 'Test error',
+ statusCode,
path: '/test-endpoint',
}); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it('should handle complex object exception response', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exceptionResponse = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: ['Email is required', 'Password is too short'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error: 'Validation Error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusCode: HttpStatus.UNPROCESSABLE_ENTITY, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
details: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
field: 'email', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
value: '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exception = new HttpException( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
exceptionResponse, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HttpStatus.UNPROCESSABLE_ENTITY, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter.catch(exception, mockArgumentsHost); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.status).toHaveBeenCalledWith( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HttpStatus.UNPROCESSABLE_ENTITY, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.send).toHaveBeenCalledWith({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: ['Email is required', 'Password is too short'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error: 'Validation Error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusCode: HttpStatus.UNPROCESSABLE_ENTITY, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
path: '/test-endpoint', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
details: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
field: 'email', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
value: '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it('should handle empty string exception response', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exception = new HttpException('', HttpStatus.NO_CONTENT); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter.catch(exception, mockArgumentsHost); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.status).toHaveBeenCalledWith(HttpStatus.NO_CONTENT); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.send).toHaveBeenCalledWith({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
response: '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
path: '/test-endpoint', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+161
to
+165
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include Even when the payload is an empty string, the filter still adds the numeric expect(mockResponse.send).toHaveBeenCalledWith({
response: '',
+ statusCode: HttpStatus.NO_CONTENT,
path: '/test-endpoint',
}); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it('should handle null exception response', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exception = new HttpException(null as any, HttpStatus.NO_CONTENT); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter.catch(exception, mockArgumentsHost); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.status).toHaveBeenCalledWith(HttpStatus.NO_CONTENT); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.send).toHaveBeenCalledWith({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusCode: HttpStatus.NO_CONTENT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
path: '/test-endpoint', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+171
to
+177
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing When the exception payload is expect(mockResponse.send).toHaveBeenCalledWith({
- statusCode: HttpStatus.NO_CONTENT,
+ response: null,
+ statusCode: HttpStatus.NO_CONTENT,
path: '/test-endpoint',
}); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it('should handle undefined exception response', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exception = new HttpException( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
undefined as any, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HttpStatus.NO_CONTENT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter.catch(exception, mockArgumentsHost); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.status).toHaveBeenCalledWith(HttpStatus.NO_CONTENT); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.send).toHaveBeenCalledWith({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusCode: HttpStatus.NO_CONTENT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
path: '/test-endpoint', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+188
to
+192
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Align undefined-payload assertion with filter output The filter treats expect(mockResponse.send).toHaveBeenCalledWith({
- statusCode: HttpStatus.NO_CONTENT,
+ response: undefined,
+ statusCode: HttpStatus.NO_CONTENT,
path: '/test-endpoint',
}); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it('should handle different request URLs', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const urls = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'/api/users', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'/api/projects/123', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'/api/auth/login', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'/api/feedback?page=1&limit=10', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
urls.forEach((url) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Object.assign(mockRequest, { url }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exception = new HttpException( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'Test error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HttpStatus.BAD_REQUEST, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter.catch(exception, mockArgumentsHost); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.send).toHaveBeenCalledWith({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
response: 'Test error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
path: url, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+212
to
+215
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reassert Even when the request URL changes, the filter keeps attaching the HTTP status code to the payload. The shared expectation inside this loop should include the expect(mockResponse.send).toHaveBeenCalledWith({
response: 'Test error',
+ statusCode: HttpStatus.BAD_REQUEST,
path: url,
}); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it('should handle nested object exception response', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exceptionResponse = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Complex error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error: 'Internal Server Error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusCode: HttpStatus.INTERNAL_SERVER_ERROR, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
nested: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
level1: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
level2: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
value: 'deep nested value', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exception = new HttpException( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
exceptionResponse, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HttpStatus.INTERNAL_SERVER_ERROR, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter.catch(exception, mockArgumentsHost); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.status).toHaveBeenCalledWith( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HttpStatus.INTERNAL_SERVER_ERROR, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.send).toHaveBeenCalledWith({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Complex error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error: 'Internal Server Error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusCode: HttpStatus.INTERNAL_SERVER_ERROR, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
path: '/test-endpoint', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
nested: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
level1: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
level2: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
value: 'deep nested value', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it('should handle array exception response', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exceptionResponse = ['Error 1', 'Error 2', 'Error 3']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exception = new HttpException( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
exceptionResponse, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HttpStatus.BAD_REQUEST, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter.catch(exception, mockArgumentsHost); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.status).toHaveBeenCalledWith(HttpStatus.BAD_REQUEST); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.send).toHaveBeenCalledWith({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
0: 'Error 1', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1: 'Error 2', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2: 'Error 3', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusCode: HttpStatus.BAD_REQUEST, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
path: '/test-endpoint', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+257
to
+274
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix expectation for array responses
Apply this diff: - expect(mockResponse.send).toHaveBeenCalledWith({
- 0: 'Error 1',
- 1: 'Error 2',
- 2: 'Error 3',
- statusCode: HttpStatus.BAD_REQUEST,
- path: '/test-endpoint',
- });
+ expect(mockResponse.send).toHaveBeenCalledWith({
+ response: {
+ 0: 'Error 1',
+ 1: 'Error 2',
+ 2: 'Error 3',
+ },
+ statusCode: HttpStatus.BAD_REQUEST,
+ path: '/test-endpoint',
+ }); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it('should handle boolean exception response', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exception = new HttpException(true as any, HttpStatus.OK); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter.catch(exception, mockArgumentsHost); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.status).toHaveBeenCalledWith(HttpStatus.OK); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.send).toHaveBeenCalledWith({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusCode: HttpStatus.OK, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
path: '/test-endpoint', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
it('should handle number exception response', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const exception = new HttpException(42 as any, HttpStatus.OK); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter.catch(exception, mockArgumentsHost); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.status).toHaveBeenCalledWith(HttpStatus.OK); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expect(mockResponse.send).toHaveBeenCalledWith({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusCode: HttpStatus.OK, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
path: '/test-endpoint', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restore expected
statusCode
in string-response assertionsHttpExceptionFilter
always appends the derivedstatusCode
alongsideresponse
andpath
. The underlying filter (see Line 57 ofhttp-exception.filter.ts
) pushesstatusCode
onto the payload for primitive responses, so the test will fail without asserting it. Please update the expectation accordingly.📝 Committable suggestion
🤖 Prompt for AI Agents