AssertInbox

Puppeteer email verification tests

A sign-up flow that sends a verification email cannot be tested end to end until the test can read that email. This guide shows how to do it with Puppeteer and the test runner built into Node, node:test, in plain JavaScript. Each test gets its own address, fills in the sign-up form, waits for the email with one HTTP call, and then either types the one-time code or opens the magic link.

How it works

Mail sent to <namespace>.<tag>@assertinbox.com is stored for your key. GET /v1/wait holds the request open for up to 25 seconds and returns the newest matching message as JSON, with otp, magic_link and links already extracted. Messages are deleted after 24 hours. Because the test code runs in Node and not in the browser page, the key never reaches the page you are testing.

Get an API key

Get one in a click at /start, or with the request below. One request creates a key and your private namespace. No email address or card is needed. The key is shown once, so store it in your CI secrets.

curl -s -X POST https://assertinbox.com/v1/keys
# {"api_key":"ai_...","namespace":"k3x9a1bz","address_template":"k3x9a1bz.<tag>@assertinbox.com"}

Export both values: ASSERTINBOX_KEY is the api_key and ASSERTINBOX_NAMESPACE is the namespace. If the call returns 403 signups_closed, the beta is not open for new keys. Use inboxes only for services you own or control; see the terms. Limits and error codes are on the limits page.

The helper

Save this as test/inbox.js. It needs Node 18 or newer for the built-in fetch. A 408 means no email arrived within the wait, so the helper tries again a few times before it gives up.

const API = "https://assertinbox.com";
const KEY = process.env.ASSERTINBOX_KEY;
const NAMESPACE = process.env.ASSERTINBOX_NAMESPACE;

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

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: { Authorization: "Bearer " + KEY },
    });
    if (res.status === 200) return res.json();
    if (res.status !== 408) throw new Error("AssertInbox returned HTTP " + res.status);
  }
  throw new Error("No email arrived for tag " + tag);
}

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

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

The tests

Install Puppeteer with npm install --save-dev puppeteer and save this as test/signup.test.js. Replace the URL, the selectors and the success text with those of your app. Run it with node --test test/.

const { describe, it, before, after, beforeEach, afterEach } = require("node:test");
const assert = require("node:assert");
const puppeteer = require("puppeteer");
const { newInbox, waitForEmail, deleteEmails } = require("./inbox");

const APP = process.env.APP_URL || "http://localhost:3000";

describe("sign-up email verification", () => {
  let browser;
  let page;
  let inbox;

  before(async () => {
    browser = await puppeteer.launch({ headless: true });
  });

  after(async () => {
    await browser.close();
  });

  beforeEach(async () => {
    inbox = newInbox("pptr");
    page = await browser.newPage();
    await page.goto(APP + "/signup");
    await page.type("input[name=email]", inbox.email);
    await page.click("button[type=submit]");
  });

  afterEach(async () => {
    await page.close();
    await deleteEmails(inbox.tag);
  });

  it("verifies with the one-time code", async () => {
    const mail = await waitForEmail(inbox.tag);
    assert.match(mail.otp || "", /^[0-9]{4,8}$/, "subject was " + mail.subject);

    await page.waitForSelector("input[name=code]", { visible: true });
    await page.type("input[name=code]", mail.otp);
    await page.click("button[type=submit]");

    await page.waitForFunction(
      () => document.body.innerText.includes("Email verified"),
      { timeout: 15000 },
    );
  });

  it("verifies with the magic link", async () => {
    const mail = await waitForEmail(inbox.tag);
    assert.ok(mail.magic_link, "no verification link in the email");

    await page.goto(mail.magic_link);
    await page.waitForFunction(
      () => document.body.innerText.includes("Email verified"),
      { timeout: 15000 },
    );
  });
});

Every test builds its own address in beforeEach, so even if you run several test files at the same time, each one reads only its own inbox. Closing the page and deleting the tag in afterEach keeps stored mail to a minimum.

Waiting for the page and for the email

Puppeteer's waitForSelector and waitForFunction poll the page until a condition is true. The email wait is a different kind of wait: it is one request that the server holds open until a matching message arrives, so it returns as soon as the email does. Wait for the email first, then wait for the field you want to type into. If the browser times out before the email arrives, the real problem is usually that the email was never sent, and the 408 from the helper tells you so more clearly than a missing selector does.

When the app sends a second email

A "resend code" button sends another message to the same address, and /v1/wait returns the newest one for the tag, which might still be the first. Pass the id of the message you already read as after:

it("sends a new code on resend", async () => {
  const first = await waitForEmail(inbox.tag);
  await page.click("#resend");
  const second = await waitForEmail(inbox.tag, { after: first.id });
  assert.notStrictEqual(second.id, first.id);
});

Add this inside the same describe block. The from and subject filters work together with after when your app sends several kinds of email to one address.

Troubleshooting

Related guides: Playwright email verification, sign-up email tests in Node, WebdriverIO email OTP.