route.test.js 1007 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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 json = await response.json();
  26. expect(destroySession).toHaveBeenCalledTimes(1);
  27. expect(response.status).toBe(500);
  28. expect(json).toEqual({ error: "Internal server error" });
  29. });
  30. });