AssertInbox

Reliable email tests: patterns that stop flaky sign-up and login checks

Email tests are flaky for a small number of reasons, and all of them have a standard fix. This page collects the patterns in one place: unique inboxes, triggering the email before waiting for it, timeouts, cleanup and what to assert. Each section links to a complete guide for your framework. The examples use the AssertInbox REST API; get a key on the start page. Limits and error codes are on the limits page.

One unique tag per test and per worker

An address has the form <namespace>.<tag>@assertinbox.com. The namespace belongs to your key; the tag is yours to choose. Two tests that share a tag read each other's mail, and a test that reuses a tag from a previous run can read an old message. Build the tag from the test name, a timestamp and a random suffix, so parallel workers and retried runs never collide. A tag starts with a letter or digit, may then contain dots, dashes and underscores, and is at most 64 characters; it is matched in lower case.

const API = "https://assertinbox.com";
const KEY = process.env.ASSERTINBOX_KEY;
const NAMESPACE = process.env.ASSERTINBOX_NAMESPACE;
const auth = { Authorization: "Bearer " + KEY };

function newInbox(name) {
  const random = Math.random().toString(36).slice(2, 8);
  const tag = (name + "-" + Date.now() + "-" + random).toLowerCase();
  return { tag, email: NAMESPACE + "." + tag + "@assertinbox.com" };
}

async function lastId(tag) {
  const res = await fetch(API + "/v1/messages?tag=" + encodeURIComponent(tag), { headers: auth });
  if (!res.ok) throw new Error("AssertInbox returned HTTP " + res.status);
  const body = await res.json();
  return body.messages.length ? body.messages[0].id : undefined;
}

async function waitForEmail(tag, filters = {}) {
  const params = new URLSearchParams({ tag, timeout: "25", ...filters });
  for (let attempt = 0; attempt < 3; attempt++) {
    const res = await fetch(API + "/v1/wait?" + params, { headers: auth });
    if (res.status === 200) return res.json();
    if (res.status !== 408) throw new Error("AssertInbox returned HTTP " + res.status);
  }
  throw new Error("No matching email arrived for tag " + tag);
}

async function deleteEmails(tag) {
  await fetch(API + "/v1/messages?tag=" + encodeURIComponent(tag), { method: "DELETE", headers: auth });
}

module.exports = { newInbox, lastId, waitForEmail, deleteEmails };

Save this as inbox.js. It needs Node 18 or newer for the built-in fetch. The guides for Playwright, Cypress, WebdriverIO and Puppeteer use the same shape, and the client files wrap it for Node.js and Python.

Trigger, then wait with after or since

The order matters. GET /v1/wait returns the newest message that matches your filters at the moment of the call. If an older matching message already exists, the call returns it at once, and the test asserts on the wrong email. The fix is to mark a point in time before you trigger the email, and ask only for messages after that point.

With a unique tag per test the first email needs no anchor. For the second email at the same address, pass the id of the first as after. The password reset guide shows this with a "only the newest link works" test.

const { newInbox, lastId, waitForEmail, deleteEmails } = require("./inbox");

async function run(triggerSignUp, triggerResend) {
  const inbox = newInbox("signup");
  try {
    await triggerSignUp(inbox.email);
    const first = await waitForEmail(inbox.tag, { subject: "verify" });

    await triggerResend(inbox.email);
    const second = await waitForEmail(inbox.tag, { subject: "verify", after: first.id });
    return { first, second, newestId: await lastId(inbox.tag) };
  } finally {
    await deleteEmails(inbox.tag);
  }
}

module.exports = { run };

Timeouts and 408 retries

A wait lasts at most 25 seconds. When nothing arrives in time the API answers 408 with the error code timeout. That is normal, not a failure of the service: a slow mail provider, a queue in your app or a cold start can all take longer than 25 seconds. Retry a few times and only then fail, as the helper above does. Give the test itself a budget that covers the retries, for example 120 seconds for a flow that waits up to three times. In CI, set a job timeout as well, so a stuck run ends. The GitHub Actions guide covers secrets, sharding and timeouts.

Treat other statuses differently. 401 means a wrong key, 403 a disabled key and 429 a used-up daily request budget; none of them gets better with a retry. If a test fails with 408 every time, print the subject and sender of the messages in the inbox with GET /v1/messages. The mail usually arrived and a filter excluded it.

Cleanup

Messages are deleted by the service after 24 hours, so cleanup is about hygiene, not storage. Delete a test's messages in its teardown with DELETE /v1/messages?tag=.... That keeps a rerun of the same test from seeing earlier mail even if you reuse a fixed tag for a long-lived test user, and it keeps your inbox listing short when you debug. Put the call in a finally block or an afterEach hook so that it also runs when the test fails. Do not clean up before the assertion that needs the message, and never delete a whole namespace between parallel workers, because that removes their mail too.

What to assert

A message arriving proves little. Assert the properties that your users depend on:

When no browser is needed, assert over HTTP only. That is faster and less flaky; see sign-up email tests in Node and Selenium with Python or pytest and Playwright for Python stacks. For agents that read email through tools instead of code, see email for AI coding agents.

Checklist for flaky email tests

  1. Does every test build its own tag from a name, a timestamp and a random suffix?
  2. Do you call wait after the action that sends the email, never before?
  3. For a second email at the same address, is after set to the first message's id?
  4. Is there a subject filter when the app sends more than one kind of email?
  5. Does the helper retry on 408 and fail fast on other statuses?
  6. Is the test timeout longer than the total wait, with a job timeout around it?
  7. Are messages deleted in teardown, also when the test fails?
  8. Does the test assert sender, subject, code format and link host?
  9. Does the app under test use a real mail transport in this environment, and is its base URL setting correct?
  10. Is the key stored as a secret, and is the daily request budget enough for the number of runs?

Next steps

Pick the guide for your stack on the guides page, read the endpoint reference in the docs, and get a key on the start page.

Free beta, run by AI agents

This service is in free beta and is operated by AI agents under human supervision. Messages are deleted after 24 hours.