From 9bb2186f7a869acd6f856bf70eeb56ebcc40f237 Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Mon, 13 Apr 2026 19:11:04 +0200 Subject: [PATCH] test: update contact api test to mock nodemailer --- tests/api-contact.test.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/api-contact.test.ts b/tests/api-contact.test.ts index 4cd06fa..3eeeff3 100644 --- a/tests/api-contact.test.ts +++ b/tests/api-contact.test.ts @@ -6,6 +6,16 @@ const { mockCreate, mockSendEmail } = vi.hoisted(() => ({ mockSendEmail: vi.fn(), })); +// Mock Nodemailer +const mockSendMail = vi.fn(); +vi.mock("nodemailer", () => ({ + default: { + createTransport: vi.fn().mockReturnValue({ + sendMail: mockSendMail, + }), + }, +})); + vi.mock("payload", () => ({ getPayload: vi.fn().mockResolvedValue({ create: mockCreate, @@ -101,7 +111,7 @@ describe("Contact API Integration", () => { // But it actually does NOTHING internally expect(mockCreate).not.toHaveBeenCalled(); - expect(mockSendEmail).not.toHaveBeenCalled(); + expect(mockSendMail).not.toHaveBeenCalled(); }); it("should successfully save to Payload and send emails", async () => { @@ -140,10 +150,10 @@ describe("Contact API Integration", () => { }); // 2. Verify Email Sending - // Note: sendEmail is called twice (Notification + User Confirmation) - expect(mockSendEmail).toHaveBeenCalledTimes(2); + // Note: sendMail is called twice (Notification + User Confirmation) + expect(mockSendMail).toHaveBeenCalledTimes(2); - expect(mockSendEmail).toHaveBeenNthCalledWith( + expect(mockSendMail).toHaveBeenNthCalledWith( 1, expect.objectContaining({ subject: "Kontaktanfrage von Jane Doe", @@ -151,7 +161,7 @@ describe("Contact API Integration", () => { }), ); - expect(mockSendEmail).toHaveBeenNthCalledWith( + expect(mockSendMail).toHaveBeenNthCalledWith( 2, expect.objectContaining({ to: "jane@example.com",