import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; import request from "supertest"; import express, { Request, Response, NextFunction } from "express"; import { app } from "../index.js"; describe("Express 5 Migration Tests", () => { describe("Version Check", () => { it("should be running Express 5.x", () => { // This test will fail on Express 4 and pass on Express 5 const expressVersion = require('express/package.json').version; expect(expressVersion).toMatch(/^5\./); }); }); describe("Async Error Handling", () => { let testApp: express.Application; beforeEach(() => { testApp = express(); testApp.use(express.json()); }); it("should automatically catch async errors without explicit error handling (Express 5 feature)", async () => { // Express 5 automatically catches rejected promises in route handlers // This test verifies that behavior let errorHandlerCalled = false; testApp.get("/test-async-error", async (req: Request, res: Response) => { // Deliberately cause an async error without try/catch await new Promise((resolve, reject) => { setTimeout(() => reject(new Error("Async test error")), 1); }); res.json({ success: true }); }); // Add error handler testApp.use((err: any, req: Request, res: Response, next: NextFunction) => { errorHandlerCalled = true; res.status(500).json({ error: "Caught async error" }); }); const response = await request(testApp) .get("/test-async-error") .expect(500); expect(response.body).toEqual({ error: "Caught async error" }); expect(errorHandlerCalled).toBe(true); }); it("should handle async errors in middleware without explicit error handling", async () => { let errorHandlerCalled = false; // Async middleware that throws testApp.use(async (req: Request, res: Response, next: NextFunction) => { await new Promise((resolve, reject) => { setTimeout(() => reject(new Error("Middleware async error")), 1); }); next(); }); testApp.get("/test-middleware-error", (req: Request, res: Response) => { res.json({ success: true }); }); // Error handler testApp.use((err: any, req: Request, res: Response, next: NextFunction) => { errorHandlerCalled = true; res.status(500).json({ error: "Middleware error caught" }); }); const response = await request(testApp) .get("/test-middleware-error") .expect(500); expect(response.body).toEqual({ error: "Middleware error caught" }); expect(errorHandlerCalled).toBe(true); }); }); describe("Express Import Style", () => { it("should support default import syntax (Express 5)", () => { // Express 5 uses default export, Express 4 uses named export // This test verifies we can import express as default export const express = require('express'); expect(typeof express).toBe('function'); expect(typeof express.default).toBe('undefined'); // Should not need .default }); }); });