Skip to content
Email SDK
Esc
↑↓navigate↵open⌘Jpreview
On this page

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

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.

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.

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.

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.

Last updated on