From 401dac5b89d05f9d93898caa0407f62c7cca93c4 Mon Sep 17 00:00:00 2001 From: Jaybe Date: Tue, 18 Nov 2025 18:40:37 +0900 Subject: [PATCH] =?UTF-8?q?API=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=97=90?= =?UTF-8?q?=EC=84=9C=20Prisma=EB=A5=BC=20=EB=A9=94=EB=AA=A8=EB=A6=AC=20?= =?UTF-8?q?=EA=B8=B0=EB=B0=98=20=EB=AA=A9=EC=9C=BC=EB=A1=9C=20=EA=B5=90?= =?UTF-8?q?=EC=B2=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/api/projects.spec.ts | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/tests/api/projects.spec.ts b/tests/api/projects.spec.ts index d43f63e..353964f 100644 --- a/tests/api/projects.spec.ts +++ b/tests/api/projects.spec.ts @@ -1,7 +1,26 @@ import "dotenv/config"; -import { describe, it, expect } from "vitest"; -import { POST as createProject } from "@/app/api/projects/route"; -import { GET as getProjectBySlug } from "@/app/api/projects/[slug]/route"; +import { describe, it, expect, vi } from "vitest"; + +// PrismaClient를 실제 DB 대신 메모리 기반 저장소를 사용하는 목으로 대체한다. +// 이렇게 하면 CI/로컬 어디에서도 DATABASE_URL 이나 실제 DB 없이 API 테스트를 실행할 수 있다. +const inMemoryProjects: any[] = []; + +vi.mock("@/generated/prisma/client", () => { + class PrismaClientMock { + project = { + create: async ({ data }: any) => { + const project = { id: inMemoryProjects.length + 1, ...data }; + inMemoryProjects.push(project); + return project; + }, + findUnique: async ({ where: { slug } }: any) => { + return inMemoryProjects.find((p) => p.slug === slug) ?? null; + }, + }; + } + + return { PrismaClient: PrismaClientMock }; +}); const BASE_URL = "http://localhost"; @@ -19,6 +38,8 @@ describe("/api/projects", () => { ], }; + const { POST: createProject } = await import("@/app/api/projects/route"); + const createResponse = await createProject( new Request(`${BASE_URL}/api/projects`, { method: "POST", @@ -37,6 +58,8 @@ describe("/api/projects", () => { expect(created.title).toBe(payload.title); expect(created.slug).toBe(payload.slug); + const { GET: getProjectBySlug } = await import("@/app/api/projects/[slug]/route"); + const getResponse = await getProjectBySlug( new Request(`${BASE_URL}/api/projects/${payload.slug}`), { params: { slug: payload.slug } } as any,