EmailProvidersMock

Mock

Use the Mock provider for testing email sending with @visulima/email

Mock Provider

The Mock provider is an in-memory email provider designed for testing. It stores sent emails in memory and allows you to inspect them, simulate failures, and add artificial delays.

Runtime Support: Universal (Node.js, Deno, Bun, Cloudflare Workers)

Setup

import { createMail } from "@visulima/email";
import { mockProvider } from "@visulima/email/providers/mock";

const mock = mockProvider();
const mail = createMail(mock);

Configuration

The MockConfig interface extends BaseConfig with the following options:

OptionTypeRequiredDefaultDescription
simulateFailurebooleanNofalseWhen true, sendEmail always returns an error
failureRatenumberNo0Failure rate (0-1) for random failures
delaynumberNo0Delay in milliseconds before resolving
randomDelayRange{ min: number; max: number }No-Random delay range in milliseconds
defaultResponseReceiptNo-Default response to return for send operations
debugbooleanNofalseEnable debug logging
loggerConsoleNo-Custom logger instance

Basic Usage

import { MailMessage } from "@visulima/email";

const message = new MailMessage()
    .to("user@example.com")
    .from("sender@example.com")
    .subject("Test email")
    .html("<h1>Hello World</h1>");

const result = await mail.send(message);

if (result.success) {
    console.log("Mock email sent:", result.data?.messageId);
}

Testing Scenarios

Simulate Failures

const mock = mockProvider({
    simulateFailure: true,
});

const mail = createMail(mock);
const result = await mail.send(message);
// result.success will be false

Random Failure Rate

const mock = mockProvider({
    failureRate: 0.3, // 30% of sends will fail
});

Artificial Delays

// Fixed delay
const mock = mockProvider({
    delay: 500, // 500ms delay
});

// Random delay range
const mock = mockProvider({
    randomDelayRange: {
        min: 100,
        max: 1000,
    },
});

Supported Features

FeatureSupported
AttachmentsYes
Batch SendingNo
Custom HeadersYes
HTMLYes
Reply-ToYes
SchedulingNo
TaggingYes
TemplatesNo
TrackingNo
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues