MailFlatDocs
Documentation/Test frameworks & CI/Jest

Email testing with Jest

Open an inbox in beforeEach, delete it in afterEach, and give Jest a timeout long enough for real mail to arrive.

How it fits together

Nothing special is needed beyond one thing people forget: Jest's default five second timeout is shorter than email delivery. Raise it on the tests that wait for mail.

The test

The third argument to test is the per-test timeout, in milliseconds.
JavaScript
// signup.test.js
import { MailFlat } from "@mailflat/sdk";
 
const mf = new MailFlat({ apiKey: process.env.MAILFLAT_API_KEY });
let inbox;
 
beforeEach(async () => {
inbox = await mf.create({
prefix: `jest-${Date.now().toString(36)}`,
retentionHours: 2,
});
});
 
afterEach(async () => {
await inbox.delete();
});
 
test(
"signup sends a verification code",
async () => {
await api.post("/signup", { email: inbox.address });
 
const otp = await inbox.waitForOtp({ timeout: 60_000 });
expect(otp).toMatch(/^\d{6}$/);
},
70_000, // longer than the waitForOtp timeout
);

Or raise it for the whole file

JavaScript
jest.setTimeout(70_000);

Worth knowing

Always set the Jest timeout HIGHER than the waitForOtp timeout, or Jest kills the test first and you lose the useful error message.
Jest runs test files in parallel workers, so one inbox per test file is the minimum isolation.