@ingeze/api-error
Version:
A TypeScript library for handling HTTP errors in Express, NestJS, and Fastify APIs.
38 lines (37 loc) • 1.24 kB
JavaScript
import { beforeEach, describe } from "node:test";
import { ErrorHandler } from "src/errors/error-handler";
import { expressErrorMiddleware } from "src/handlers/express";
describe("expressHandlerMiddleware with mocks", () => {
let req;
let res = {
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis()
};
let next;
beforeEach(() => {
req = {};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis()
};
next = jest.fn();
});
it("handles custom ErrorHandler", () => {
const err = new ErrorHandler("Custom error", 400, "CUSTOM_ERROR", { id: 1, info: "Custom error" });
expressErrorMiddleware(err, req, res, next);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith(err.toJSON());
});
it("handles generic error", () => {
const err = new Error("Something went wrong");
expressErrorMiddleware(err, req, res, next);
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({
success: false,
statusCode: 500,
type: "INTERNAL_SERVER_ERROR",
message: "An unexpected error occurred"
});
});
});
//# sourceMappingURL=express.test.js.map