Test email sending without sending real email
Test email sending in Node.js and TypeScript without sending real email: capture messages in memory, prove retries and fallback, or route SMTP to Mailpit.
To test email sending in a Node.js or TypeScript app without sending real email, swap the provider adapter for memoryAdapter() from @opencoredev/email-sdk/testing. It never calls a provider and keeps every message your code sends, so a test can check the recipients, subject, and body. Use failingAdapter() to test retries and fallback, and point the SMTP adapter at a local catcher such as Mailpit when you need to see the rendered MIME message.
| What you want to prove | Use | Network |
|---|---|---|
| Your code sends the right message | memoryAdapter() |
None |
| Retries and fallback behave | failingAdapter() + memoryAdapter() |
None |
| Hooks and plugins run in order | capturePlugin() + memoryAdapter() |
None |
| The real MIME message renders | smtp() pointed at Mailpit |
Local only |
“None” assumes telemetry is off: the client sends anonymous usage telemetry unless you pass telemetry: false, set EMAIL_SDK_TELEMETRY=0 or DO_NOT_TRACK=1, or run with NODE_ENV=test (see Keep telemetry off). Delivery to real inboxes still needs a real send from your own provider account.
Capture a successful send
import { expect, test } from "bun:test";
import { createEmailClient } from "@opencoredev/email-sdk";
import { memoryAdapter } from "@opencoredev/email-sdk/testing";
test("sends the welcome email", async () => {
const memory = memoryAdapter();
const email = createEmailClient({ adapters: [memory], telemetry: false });
await email.send({
from: "Acme <hello@acme.com>",
to: "user@example.com",
subject: "Welcome",
text: "Your account is ready.",
});
expect(memory.raw?.sent).toHaveLength(1);
expect(memory.raw?.sent[0]?.message.subject).toBe("Welcome");
});
memory.raw?.clear() resets the store. Outside a test runner, read the same array: memory.raw?.sent holds one { message, response } entry per send, in order, and the quickstart prints the first captured message from a plain script. raw is optional on the shared adapter type, so TypeScript needs the ?..
Prove fallback behavior
Fallback advances automatically only after a not_sent failure.
import { EmailAdapterError } from "@opencoredev/email-sdk";
import { failingAdapter, memoryAdapter } from "@opencoredev/email-sdk/testing";
const primary = failingAdapter(
"primary",
new EmailAdapterError("Rejected before acceptance", {
adapter: "primary",
retryable: false,
delivery: "not_sent",
}),
);
const backup = memoryAdapter("backup");
const email = createEmailClient({
adapters: [primary, backup],
fallback: { adapters: ["backup"] },
});
const result = await email.send(message);
expect(result.adapter).toBe("backup");
Use delivery: "unknown" to prove the default route stops instead of risking a duplicate.
Prove retry counts
maxAttempts is the total call count. Set delay: () => 0 in tests.
const email = createEmailClient({
adapters: [primary],
retry: {
maxAttempts: 3,
delay: () => 0,
},
});
Capture lifecycle events
Use capturePlugin() when a test needs ordered beforeSend, retry, afterSend, and error records.
Inspect the real message with a local SMTP catcher
The memory adapter stores the message object, not the MIME a provider would receive. To look at headers, encodings, and attachments as a mail client sees them, run a local catcher such as Mailpit and send through the SMTP adapter. Mailpit accepts mail on port 1025 and shows it in a web UI on port 8025. With telemetry: false, nothing leaves your machine.
import { createEmailClient } from "@opencoredev/email-sdk";
import { smtp } from "@opencoredev/email-sdk/smtp";
export const email = createEmailClient({
adapters: [smtp({ host: "127.0.0.1", port: 1025 })],
telemetry: false,
});
With no auth, the adapter doesn’t require TLS, so it can talk to a local catcher that has none. This repository runs the same setup in its own SMTP check. The Mailpit and MailHog guide covers reading captured messages through Mailpit’s REST API and testing SMTP errors with its Chaos triggers.
Keep telemetry off
The repository test preload sets EMAIL_SDK_TELEMETRY=0. Application tests can also construct clients with telemetry: false.
Telemetry is also off when NODE_ENV is test at the moment the SDK first sets up telemetry, which happens when the first client without telemetry: false is created; the SDK reads it only that once. Set it before creating any such client. bun test sets it to test unless NODE_ENV is already set; node --test doesn’t set it at all. Passing telemetry: false turns SDK telemetry off whatever NODE_ENV is.
Questions
Does memoryAdapter() send anything?
No. Its send pushes the message and a result with an id like mem_1 into memory.raw.sent and returns. It never calls a provider. The client’s anonymous telemetry is separate; turn it off with telemetry: false.
Does it work with Vitest or Jest?
Yes. The adapter is plain TypeScript with no test-runner dependency. The examples import { expect, test } from bun:test; import the same names from vitest or @jest/globals instead. With node:test, import test from node:test, check results with assert from node:assert/strict, and pass telemetry: false. Save that version as plain JavaScript, such as src/email.test.mjs, and run node --test src/email.test.mjs.
How do I test that fallback does not cause duplicates?
Make the primary adapter fail with delivery: "unknown". With the default onUnknownDelivery: "stop", the send throws instead of trying the backup, and memory.raw.sent stays empty. See retries and fallback.
Can I keep the same client code in tests and production?
Yes. Build the client from a list of adapters and pass memoryAdapter() in tests and your provider adapter in production. The send calls don’t change.
Capture plugin
Inspect complete lifecycle events through a typed client property.