Project Files
src / __tests__ / smtp.test.ts
import { describe, it, expect } from "vitest";
import nodemailer from "nodemailer";
import { sendEmail } from "../smtp";
// Uses Ethereal — a free fake SMTP service that accepts mail without delivering it.
// No credentials needed: nodemailer.createTestAccount() provisions a throw-away account.
describe("sendEmail (Ethereal SMTP)", () => {
it("sends a plain email and returns a messageId", async () => {
const testAccount = await nodemailer.createTestAccount();
const messageId = await sendEmail(
{
host: testAccount.smtp.host,
port: testAccount.smtp.port,
user: testAccount.user,
password: testAccount.pass,
},
{
from: testAccount.user,
to: testAccount.user,
subject: "Test email from email-plugin",
body: "Hello from the email plugin test suite.",
}
);
expect(typeof messageId).toBe("string");
expect(messageId.length).toBeGreaterThan(0);
console.log("Preview URL:", nodemailer.getTestMessageUrl({ messageId, envelope: { from: testAccount.user, to: [testAccount.user] } } as any));
}, 15_000);
it("sends a reply with In-Reply-To header", async () => {
const testAccount = await nodemailer.createTestAccount();
const messageId = await sendEmail(
{
host: testAccount.smtp.host,
port: testAccount.smtp.port,
user: testAccount.user,
password: testAccount.pass,
},
{
from: testAccount.user,
to: testAccount.user,
subject: "Re: Original subject",
body: "This is a reply.",
inReplyTo: "<original-message-id@test.com>",
references: "<original-message-id@test.com>",
}
);
expect(messageId).toBeTruthy();
}, 15_000);
it("sends with CC", async () => {
const testAccount = await nodemailer.createTestAccount();
const messageId = await sendEmail(
{
host: testAccount.smtp.host,
port: testAccount.smtp.port,
user: testAccount.user,
password: testAccount.pass,
},
{
from: testAccount.user,
to: testAccount.user,
cc: testAccount.user,
subject: "CC test",
body: "Testing CC field.",
}
);
expect(messageId).toBeTruthy();
}, 15_000);
it("throws on bad credentials", async () => {
await expect(
sendEmail(
{ host: "smtp.ethereal.email", port: 587, user: "bad@user.com", password: "wrongpass" },
{ from: "bad@user.com", to: "test@test.com", subject: "fail", body: "fail" }
)
).rejects.toThrow();
}, 15_000);
});