{"library":"reqresnext","title":"Express Middleware Testing Helper","description":"reqresnext is a minimalist utility designed for testing Express.js middleware. It provides mock implementations of the `req`, `res`, and `next` objects, allowing developers to isolate and test middleware functions without a full Express server setup. The current stable version is 1.7.0, released in February 2022. Releases are infrequent, primarily focusing on bug fixes and minor enhancements rather than new features. Its key differentiator is its small footprint and focused scope, aiming to provide just enough mocking capability for unit testing, contrasting with more comprehensive HTTP mocking libraries that might offer broader server-side simulation. It ships with robust TypeScript type definitions, making it well-suited for modern JavaScript and TypeScript projects that prioritize type safety in their testing environments.","language":"javascript","status":"maintenance","last_verified":"Thu Apr 23","install":{"commands":["npm install reqresnext"],"cli":null},"imports":["import { mockReq } from 'reqresnext'","import { mockRes } from 'reqresnext'","import { mockNext } from 'reqresnext'","import type { MockRequest } from 'reqresnext'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { mockReq, mockRes, mockNext } from 'reqresnext';\nimport { Request, Response, NextFunction } from 'express';\n\n// Example Express middleware\nconst authMiddleware = (req: Request, res: Response, next: NextFunction) => {\n  if (req.headers && req.headers.authorization === 'Bearer my-secret-token') {\n    // In a real app, you might decode a JWT and set req.user\n    (req as any).user = { id: 1, name: 'Test User' }; \n    next();\n  } else {\n    res.status(401).send('Unauthorized');\n  }\n};\n\n// Basic test using a common testing framework like Jest\ndescribe('authMiddleware', () => {\n  it('should allow access with a valid token', () => {\n    const req = mockReq({ headers: { authorization: 'Bearer my-secret-token' } });\n    const res = mockRes();\n    const next = mockNext();\n\n    authMiddleware(req as unknown as Request, res as unknown as Response, next);\n\n    expect(next).toHaveBeenCalledTimes(1);\n    expect((req as any).user).toEqual({ id: 1, name: 'Test User' });\n    expect(res.statusCode).toBe(200); // Default status if next() is called\n    expect(res.send).not.toHaveBeenCalled();\n  });\n\n  it('should deny access without a token', () => {\n    const req = mockReq(); // No authorization header\n    const res = mockRes();\n    const next = mockNext();\n\n    authMiddleware(req as unknown as Request, res as unknown as Response, next);\n\n    expect(next).not.toHaveBeenCalled();\n    expect(res.statusCode).toBe(401);\n    expect(res.send).toHaveBeenCalledWith('Unauthorized');\n  });\n});","lang":"typescript","description":"Demonstrates how to mock `req`, `res`, and `next` objects and unit test an Express middleware function using Jest expectations.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}