{"library":"next-test-api-route-handler","title":"Next.js API Route Handler Tester","description":"next-test-api-route-handler (NTARH) is a robust utility for confidently unit and integration testing Next.js API routes and handlers. It accurately emulates the Next.js runtime environment, ensuring that `NextRequest` and `NextApiResponse` objects behave as they would in a real Next.js application, including Next.js's patching of the global `fetch` function and correct handling of App Router helper functions like `headers()` and `cookies()`. The current stable version is 5.0.4. The library maintains a rapid release cadence, often issuing updates to ensure compatibility with new Next.js versions, including `canary` releases. Its key differentiator is the deep emulation of Next.js internals, which removes the need for manual mocking with tools like `express` or `node-mocks-http`, and it is automatically tested against various Next.js and Node.js versions.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install next-test-api-route-handler"],"cli":null},"imports":["import { test } from 'next-test-api-route-handler';","import { appHandler } from 'next-test-api-route-handler';","import { pagesHandler } from 'next-test-api-route-handler';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { test } from 'next-test-api-route-handler';\nimport { NextResponse } from 'next/server';\n\n// Simulate your actual App Router API handler (e.g., in src/app/api/hello/route.ts)\nexport async function GET() {\n  return NextResponse.json({ message: 'Hello from API!' });\n}\n\ndescribe('App Router GET /api/hello', () => {\n  it('should return a 200 status and a JSON message', async () => {\n    await test({\n      url: '/api/hello',\n      appHandler: GET, // Pass the handler function directly\n      async test({ fetch }) {\n        const res = await fetch({ method: 'GET' });\n        expect(res.status).toBe(200);\n        const data = await res.json();\n        expect(data).toEqual({ message: 'Hello from API!' });\n      },\n    });\n  });\n\n  it('should allow modifying the request', async () => {\n    await test({\n      url: '/api/hello',\n      appHandler: GET,\n      requestPatcher: (request) => {\n        request.headers.set('X-Test-Header', 'Value');\n        return request;\n      },\n      async test({ fetch }) {\n        const res = await fetch({ method: 'GET' });\n        expect(res.status).toBe(200);\n        // In a real handler, you'd access this header via request.headers.get('X-Test-Header')\n      },\n    });\n  });\n});","lang":"typescript","description":"Demonstrates how to test an App Router GET handler using `next-test-api-route-handler`, including passing the handler and patching the request object.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}