UNPKG

@ingeze/api-error

Version:

A TypeScript library for handling HTTP errors in Express, NestJS, and Fastify APIs.

83 lines (82 loc) 2.44 kB
import { NestErrorFilter, NestGenericErrorFilter } from "src/handlers/nest"; import { ErrorHandler } from "src/errors/error-handler"; import { HttpException } from "@nestjs/common"; describe("NestErrorFilter", () => { let filter; let mockResponse; let mockHost; beforeEach(() => { filter = new NestErrorFilter(); mockResponse = { status: jest.fn().mockReturnThis(), json: jest.fn() }; mockHost = { switchToHttp: () => ({ getResponse: () => mockResponse }) }; }); it("should handle ErrorHandler and respond with its status and JSON", () => { const error = new ErrorHandler("Custom error", 418); jest.spyOn(error, "toJSON").mockReturnValue({ success: false, type: "CUSTOM_ERROR", statusCode: 418, message: "Custom error" }); filter.catch(error, mockHost); expect(mockResponse.status).toHaveBeenCalledWith(418); expect(mockResponse.json).toHaveBeenCalledWith({ success: false, type: "CUSTOM_ERROR", statusCode: 418, message: "Custom error" }); }); }); describe("NestGenericErrorFilter", () => { let filter; let mockResponse; let mockHost; beforeEach(() => { filter = new NestGenericErrorFilter(); mockResponse = { status: jest.fn().mockReturnThis(), json: jest.fn() }; mockHost = { switchToHttp: () => ({ getResponse: () => mockResponse }) }; }); it("should handle HttpException and respond with its status and message", () => { class CustomHttpException extends HttpException { constructor() { super("Http error occurred", 400); } } const exception = new CustomHttpException(); filter.catch(exception, mockHost); expect(mockResponse.status).toHaveBeenCalledWith(400); expect(mockResponse.json).toHaveBeenCalledWith({ success: false, type: "HTTP_EXCEPTION", statusCode: 400, message: "Http error occurred" }); }); it("should handle generic Error and respond with 500", () => { const exception = new Error("Some error"); filter.catch(exception, mockHost); expect(mockResponse.status).toHaveBeenCalledWith(500); expect(mockResponse.json).toHaveBeenCalledWith({ success: false, type: "INTERNAL_SERVER_ERROR", statusCode: 500, message: "An unexpected error occurred" }); }); }); //# sourceMappingURL=nest.test.js.map