Email testing in Jest and Node.js
Mocking your mailer proves your code called send(). A real inbox proves the email arrived, with the right subject, link and code. Here's how to add that to Jest.
The helper
For integration tests that run in CI, private inboxes from the Business API work best. Set QI_KEY and QI_NAMESPACE, then:
// inbox.js - private test inboxes with the Business API (Node 18+)
const BASE = 'https://business.quickinbox.net';
const NS = process.env.QI_NAMESPACE; // e.g. "acme7x"
const auth = { headers: { 'X-API-Key': process.env.QI_KEY } };
// Any tag works at once: acme7x.<tag>@quickinbox.net
export function newAddress(prefix = 'test') {
const tag = `${prefix}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`;
return { tag, address: `${NS}.${tag}@quickinbox.net` };
}
export async function waitForEmail(tag, { timeoutMs = 60_000, subject = '' } = {}) {
const since = Math.floor(Date.now() / 1000) - 60;
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const res = await fetch(`${BASE}/api/business/mail?namespace=${NS}&since=${since}&limit=50`, auth);
if (res.ok) {
const { messages } = await res.json();
const hit = messages.find((m) => m.to === `${NS}.${tag}` && m.subject.includes(subject));
if (hit) return hit; // id, to, from, subject, text, html, spam, receivedAt
}
await new Promise((r) => setTimeout(r, 2000));
}
throw new Error(`No email for ${tag} within ${timeoutMs} ms`);
}Because any tag on your namespace is a new address, you don't need a create call, and parallel Jest workers never share an inbox.
A test for a welcome email
// welcome.test.js
import { newAddress, waitForEmail } from './inbox.js';
import { createUser } from '../src/users.js'; // your code that sends the email
jest.setTimeout(90_000);
test('sends a welcome email with a working dashboard link', async () => {
const { tag, address } = newAddress('welcome');
await createUser({ email: address, name: 'Test User' });
const email = await waitForEmail(tag, { subject: 'Welcome' });
expect(email.subject).toBe('Welcome to Acme, Test User');
expect(email.html).toContain('https://app.example.com/dashboard');
expect(email.html).not.toContain('localhost');
});A test for a reset code
test('password reset email has a 6-digit code', async () => {
const { tag, address } = newAddress('reset');
await requestPasswordReset(address);
const email = await waitForEmail(tag, { subject: 'Reset' });
expect(email.text).toMatch(/\b\d{6}\b/);
});Keep unit tests fast
Keep mocks for unit tests that run on every save. Run these real-inbox tests as a separate Jest project or on CI only, for example with jest --selectProjects integration.
Check deliverability too
Every message on a Business namespace comes with a spam score and SPF, DKIM and DMARC results. Add expect(email.spam.score).toBeGreaterThan(70) to catch a broken sender setup. See testing email templates.
Related guides
- Email testing in Python with pytest
- How to test magic link login
- How to test email OTP and 2FA codes
- How to test emails in GitHub Actions
- Email testing API overview
Get private inboxes: the QuickInbox Business API has a free plan with 5,000 emails a month, and paid plans from $9. Create a free account or read the API docs.