Command Palette
Search for a command to run...
For Next.js App Router API routes with Vitest, test the route handler functions directly:
typescriptimport { describe, it, expect, vi } from 'vitest'; import { GET, POST } from './route'; // Mock Prisma vi.mock('@/lib/db/prisma', () => ({ db: { conversation: { findMany: vi.fn(), create: vi.fn(), }, }, })); describe('GET /api/conversations', () => { it('should return conversations list', async () => { const mockData = [ { id: '1', title: 'Test Conv', platform: 'chatgpt' }, ]; const { db } = await import('@/lib/db/prisma'); vi.mocked(db.conversation.findMany).mockResolvedValue(mockData); const request = new Request('http://localhost:3000/api/conversations'); const response = await GET(request); const body = await response.json(); expect(response.status).toBe(200); expect(body.data).toEqual(mockData); }); it('should handle errors gracefully', async () => { const { db } = await import('@/lib/db/prisma'); vi.mocked(db.conversation.findMany).mockRejectedValue( new Error('DB connection failed') ); const request = new Request('http://localhost:3000/api/conversations'); const response = await GET(request); expect(response.status).toBe(500); }); });
Key patterns:
vi.mockRequest/Response objects