UNPKG

@ingeze/api-error

Version:

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

43 lines (42 loc) 1.24 kB
import { ErrorHandler } from "src/errors"; import { fastifyErrorMiddleware } from "src/handlers/fastify"; describe("Error instanceof ErrorHandler", () => { let req; let reply = { code: jest.fn().mockReturnThis(), send: jest.fn().mockReturnThis() }; beforeEach(() => { req = {}; reply = { code: jest.fn().mockReturnThis(), send: jest.fn().mockReturnThis() }; }); it("custom error", () => { const err = new ErrorHandler("Custom error", 400, "CUSTOM_ERROR", { id: 1, info: "Custom error" }); fastifyErrorMiddleware(err, req, reply); expect(reply.code).toHaveBeenCalledWith(err.statusCode); expect(reply.send).toHaveBeenCalledWith({ result: err.toJSON(), data: null, pagination: null }); }); it("generic error", () => { const err = new Error("Unexpected error"); fastifyErrorMiddleware(err, req, reply); expect(reply.code).toHaveBeenCalledWith(500); expect(reply.send).toHaveBeenCalledWith({ result: { success: false, type: "INTERNAL_SERVER_ERROR", statusCode: 500, message: "Unexpected error" }, data: null, pagination: null }); }); }); //# sourceMappingURL=fastify.test.js.map