Vitest + plop.email
Add email verification to your Vitest test suites. Fast, ESM-native testing with Vite's speed.
Vitest DocumentationFeatures
Vite-Powered
Instant test startup with Vite's transformation pipeline.
ESM Native
First-class ES modules support, no CommonJS workarounds.
Jest Compatible
Familiar API with Jest-compatible matchers and mocks.
TypeScript Ready
Out-of-the-box TypeScript support without configuration.
Setup
# Install Vitest
npm install -D vitest
# Add to package.json
{
"scripts": {
"test": "vitest",
"test:run": "vitest run"
}
}
# Create vitest.config.ts
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
environment: 'node',
},
});Basic Email Test
Test email sending in your application with Vitest.
Basic Email Testtypescript
import { describe, it, expect } from 'vitest';
import { sendWelcomeEmail } from '../src/email';
describe('Email Service', () => {
it('sends welcome email with correct content', async () => {
const testEmail = `vitest+${Date.now()}@in.plop.email`;
// Send email from your app
await sendWelcomeEmail({
to: testEmail,
name: 'Test User',
});
// Fetch from plop
const response = await fetch(
`https://api.plop.email/v1/messages/latest?to=${testEmail}`,
{ headers: { Authorization: `Bearer ${process.env.PLOP_API_KEY}` } }
);
const email = await response.json();
// Assert
expect(email.subject).toBe('Welcome to Our App!');
expect(email.htmlContent).toContain('Test User');
});
});Email Test Helper
Create a reusable email testing utility.
Email Test Helpertypescript
// test/helpers/plop.ts
export async function fetchEmail(to: string, options?: {
timeout?: number;
subject?: RegExp;
}) {
const { timeout = 10000, subject } = options ?? {};
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
const response = await fetch(
`https://api.plop.email/v1/messages/latest?to=${to}`,
{ headers: { Authorization: `Bearer ${process.env.PLOP_API_KEY}` } }
);
const email = await response.json();
if (!email.error) {
if (subject && !subject.test(email.subject)) {
await new Promise(r => setTimeout(r, 1000));
continue;
}
return email;
}
await new Promise(r => setTimeout(r, 1000));
}
throw new Error(`Timeout waiting for email to ${to}`);
}
// Usage in tests
import { fetchEmail } from './helpers/plop';
it('receives password reset email', async () => {
const testEmail = `reset+${Date.now()}@in.plop.email`;
await requestPasswordReset(testEmail);
const email = await fetchEmail(testEmail, {
subject: /reset/i,
timeout: 15000,
});
expect(email.htmlContent).toContain('Reset your password');
});Tips
- Use vi.setSystemTime() for testing email timestamps
- Create a beforeEach hook to generate unique test emails
- Use test.concurrent for parallel email tests with unique addresses
- Configure testTimeout for longer email delivery tests
Related Integrations
Related Use Cases
Start testing with Vitest
Add email verification to your Vitest tests in minutes.