Scratch space for learning atproto app development
1import express, { type Express } from "express"; 2import { StatusCodes } from "http-status-codes"; 3import request from "supertest"; 4 5import errorHandler from "#/common/middleware/errorHandler"; 6 7describe("Error Handler Middleware", () => { 8 let app: Express; 9 10 beforeAll(() => { 11 app = express(); 12 13 app.get("/error", () => { 14 throw new Error("Test error"); 15 }); 16 app.get("/next-error", (_req, _res, next) => { 17 const error = new Error("Error passed to next()"); 18 next(error); 19 }); 20 21 app.use(errorHandler()); 22 app.use("*", (req, res) => res.status(StatusCodes.NOT_FOUND).send("Not Found")); 23 }); 24 25 describe("Handling unknown routes", () => { 26 it("returns 404 for unknown routes", async () => { 27 const response = await request(app).get("/this-route-does-not-exist"); 28 expect(response.status).toBe(StatusCodes.NOT_FOUND); 29 }); 30 }); 31 32 describe("Handling thrown errors", () => { 33 it("handles thrown errors with a 500 status code", async () => { 34 const response = await request(app).get("/error"); 35 expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR); 36 }); 37 }); 38 39 describe("Handling errors passed to next()", () => { 40 it("handles errors passed to next() with a 500 status code", async () => { 41 const response = await request(app).get("/next-error"); 42 expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR); 43 }); 44 }); 45});