Skip to content
Engineering

How to test transactional email without sending to real users

Build a repeatable email test suite that cannot accidentally contact customers.

THE SHORT ANSWER

Test rendering, API behavior, and event handling separately using synthetic recipients and a local provider fixture. Simulated acceptance can verify application behavior, but authentication, network delivery, and inbox placement need distinct controlled checks.

Before you start #

For
Developers building local and CI email tests.
Bring
Node.js 22+, cURL and synthetic data. Keep provider credentials outside the test environment.
Scope
The local fixture checks an API contract. It cannot prove real delivery or email-client rendering.

Choose the evidence you need from each test #

This separation makes failures explainable. If a template fixture fails, you should not need to troubleshoot DNS. If a webhook replay fails, a successful email preview is irrelevant. Avoid using a single “email test passed” badge for several unrelated claims.

LayerTest locallyWhat it does not establish
Template renderingVariables, escaping, links, plain text, layoutRendering in every email client
API integrationRequest shape, errors, stable keys, status lookupProvider acceptance or real delivery
Event handlingSignature checks, duplicates, retries, orderingThat a real provider sent the fixture
Controlled deliveryAuthentication and observations in owned test mailboxesInbox placement for all recipients

Render realistic fixtures, including awkward values #

Create fixtures with long names, apostrophes, non-Latin text, missing optional fields, and very long URLs. Escape text and attribute values in the correct context. Verify the plain-text alternative alongside HTML.

Snapshot tests can catch unintended changes, but avoid treating any snapshot as automatically correct. Assert that there are no unresolved placeholders, the main action has a useful accessible name, and links use the expected scheme and host.

Use synthetic identities and sample domains. Never copy a production customer list into a test fixture, even if you believe sending is disabled. Keeping real recipient data out of the test path removes an entire class of mistakes.

Synthetic fixtureCheck
Name containing <, >, quotes and an ampersandIt remains text; it cannot add markup or attributes.
URL with a lookalike host or an unexpected schemeThe application rejects it before template rendering.
A missing required link or unresolved {{variable}}Rendering fails visibly; no incomplete message is queued.
Long non-Latin name and long fallback URLEssential content remains readable in both HTML and plain text.

Use the downloadable template fixtures →

Run a local API that cannot send mail #

Download email-mock-server.mjs and run it with Node.js 22 or later. It listens only on the loopback interface, stores messages in memory, and implements a small acceptance and lookup contract. It contains no SMTP connection or provider SDK.

The fixture deliberately stays queued: it does not pretend a receiving server accepted anything. Restarting it clears its state. It is suitable for local integration exercises, not application persistence or production delivery.

This fixture checksNeeds a different test
202, stable IDs, changed-payload conflicts and lookupAuthentication, authorization and durable storage.
An explicitly simulated queued recordSMTP, DNS, receiving-server acceptance and inbox location.
A local process that can be stopped and restartedProvider rate limits, TLS failures and ambiguous remote acceptance.
Local fixture · two terminalsshell
node email-mock-server.mjs

# In another terminal:
curl http://127.0.0.1:3027/api/v1/emails \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: local-receipt-42" \
  -d '{"to":"reader@example.test","subject":"Receipt","text":"A simulated receipt"}'

# Repeat unchanged: same message ID.
# Change the text with the same key: HTTP 409.

Test the failure path as carefully as success #

Use a controllable clock and a seeded or injected jitter source for timing tests. Assert that no attempt runs before the persisted next-attempt time, that a restart preserves the budget, and that expired content stops the operation. Check bounds and state transitions instead of waiting through real backoff delays or expecting one random schedule.

  • Validation rejection: the application shows an actionable error without recording a send as accepted.
  • Lost response: the operation remains recoverable with the original key and payload.
  • Rate limiting: the worker respects a delay and does not block the user request indefinitely.
  • Storage unavailable: the system does not claim to have saved a message or callback.
  • Duplicate callback: one event is stored once, even if the sender sees two successful responses.
  • Out-of-order callback: an older observation does not erase a newer one.
  • Expired content: support views handle missing retained bodies while preserving permitted event metadata.

Run the lost-response idempotency lab →

Replay signed webhook events →

Make each test fail when its protection is removed #

A passing HTTP assertion can miss a duplicate side effect. Count the durable records and the transport calls as well as checking the response. Use a deliberate local mutation—such as removing the payload comparison—to check that the corresponding test can detect the bug. Do this only in a disposable test copy.

The PostgreSQL lab below exercises eight checks with no mail transport. It includes a held row lock, competing claims, a simulated lost provider acknowledgement, and a stale worker. Its reconnect check proves visibility from another database connection; it does not simulate a killed process or database failover.

Injected faultAssertion that mattersInsufficient assertion
Lost response after simulated acceptanceOne logical operation and one simulated provider acceptance.The retry returned 202.
Changed payload under the same event keyConflict, with the original payload unchanged.The outbox still has one row.
Expired lease while outcome is unresolvedNo automatic resend; stale completion updates zero rows.Only one worker held the first lock.
Order transaction rolls backNeither order nor outbox row remains.No provider call occurred yet.

Run the PostgreSQL outbox lab →

Download outbox-lab.mjs →

Put a hard boundary around automated tests #

Inject the transport into business logic rather than constructing a live provider client everywhere. Tests can supply a fixture transport, while the production entry point supplies the authenticated provider transport.

Keep live credentials out of CI jobs that exercise email behavior. Limit outbound network access where your test environment supports it. A test-only environment variable is useful, but it should not be the only barrier between a fixture and a customer mailbox.

When running a local Stampwing workspace, keep simulated mode explicit. Test keys and Live keys have different effects. The public resources do not require a Stampwing account and the downloadable fixture never sends email.

Add a separate, deliberate delivery check #

After local checks pass, an operator can use owned test mailboxes to observe a real message with a configured provider. Record the sender identity, time, provider events, authentication results, and observed folder. Make this a deliberate operation rather than a side effect of the test suite.

Keep the conclusion narrow. A successful message to one controlled mailbox is evidence about that message and time. It does not validate every recipient, guarantee future inbox placement, or turn simulated test results into live measurements.

Inspect authentication with read-only DNS commands →

Define a small, repeatable test run #

  1. Start the loopback fixture with no provider credentials and submit one synthetic message.
  2. Repeat the identical request with the original idempotency key. Assert that the message ID is unchanged.
  3. Change the payload while keeping the key. Assert a conflict rather than another accepted message.
  4. Stop the fixture and verify your app reports a recoverable failure without claiming delivery.
  5. Render each template with long and non-ASCII values. Check the HTML and text versions for unresolved placeholders and unexpected links.

Choose a synthetic template to render →

Sources and further reading

Written for Stampwing with AI assistance and checked against the linked documentation. Examples are educational; simulated results are labelled. How these resources are maintained.