Jest + plop.email

Add email verification to your Jest test suites. Perfect for API and integration tests.

Jest Documentation

Features

Async/Await

Clean async syntax for email fetching and assertions.

Custom Matchers

Create expect.toHaveReceivedEmail() custom matchers.

Setup/Teardown

Use beforeAll/afterAll for mailbox setup and cleanup.

Snapshot Testing

Snapshot email content for regression detection.

Setup

# Install Jest and fetch
npm install jest @types/jest ts-jest --save-dev

# Create a plop client helper
// src/test/plop-client.ts
export const plopClient = {
  baseUrl: 'https://api.plop.email/v1',
  apiKey: process.env.PLOP_API_KEY,

  async getLatest(to: string) {
    const res = await fetch(`${this.baseUrl}/messages/latest?to=${to}`, {
      headers: { Authorization: `Bearer ${this.apiKey}` },
    });
    return res.json();
  },
};

Basic Integration Test

Test email sending in your application.

Basic Integration Testtypescript
import { plopClient } from './plop-client';
import { sendWelcomeEmail } from '../src/email';

describe('Email Service', () => {
  it('sends welcome email with correct content', async () => {
    const testEmail = `jest+${Date.now()}@in.plop.email`;

    // Trigger email from your application
    await sendWelcomeEmail({
      to: testEmail,
      name: 'Test User',
    });

    // Wait for delivery
    await new Promise(r => setTimeout(r, 2000));

    // Fetch and verify
    const email = await plopClient.getLatest(testEmail);

    expect(email.subject).toBe('Welcome to Our App!');
    expect(email.htmlContent).toContain('Test User');
    expect(email.htmlContent).toContain('Get Started');
  });
});

Custom Jest Matcher

Create a reusable email assertion matcher.

Custom Jest Matchertypescript
// test/matchers/email.ts
import { plopClient } from '../plop-client';

expect.extend({
  async toHaveReceivedEmail(address: string, expected: {
    subject?: string | RegExp;
    bodyContains?: string[];
  }) {
    const email = await plopClient.getLatest(address);

    if (!email || email.error) {
      return {
        pass: false,
        message: () => `Expected ${address} to have received an email`,
      };
    }

    if (expected.subject) {
      const subjectMatch = expected.subject instanceof RegExp
        ? expected.subject.test(email.subject)
        : email.subject.includes(expected.subject);

      if (!subjectMatch) {
        return {
          pass: false,
          message: () => `Expected subject "${email.subject}" to match ${expected.subject}`,
        };
      }
    }

    return { pass: true, message: () => '' };
  },
});

// Usage
await expect(testEmail).toHaveReceivedEmail({
  subject: /Welcome/,
  bodyContains: ['Get Started', 'Dashboard'],
});

Tips

  • Use jest.setTimeout() for longer email delivery times
  • Create a shared test utility file for email helpers
  • Mock the email service in unit tests, use plop for integration tests
  • Use describe.each() for testing multiple email templates

Related Integrations

Start testing with Jest

Add email verification to your Jest tests in minutes.