route.test.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { describe, it, expect, vi, beforeEach } from "vitest";
  2. // 1) Mock for destroySession
  3. vi.mock("@/lib/auth/session", () => ({
  4. destroySession: vi.fn(),
  5. }));
  6. // 2) Import after mock
  7. import { destroySession } from "@/lib/auth/session";
  8. import { GET } from "./route";
  9. describe("GET /api/auth/logout", () => {
  10. beforeEach(() => {
  11. vi.clearAllMocks();
  12. });
  13. it("calls destroySession and returns ok: true", async () => {
  14. const response = await GET();
  15. const json = await response.json();
  16. expect(destroySession).toHaveBeenCalledTimes(1);
  17. expect(response.status).toBe(200);
  18. expect(json).toEqual({ ok: true });
  19. });
  20. it("returns 500 when destroySession throws an error", async () => {
  21. destroySession.mockImplementation(() => {
  22. throw new Error("boom");
  23. });
  24. const response = await GET();
  25. const body = await response.json();
  26. expect(destroySession).toHaveBeenCalledTimes(1);
  27. expect(response.status).toBe(500);
  28. expect(body).toEqual({
  29. error: {
  30. message: "Internal server error",
  31. code: "INTERNAL_SERVER_ERROR",
  32. },
  33. });
  34. });
  35. });