AssertInbox

Test sign-up emails without a browser, in Node

You do not need a browser to check that your app sends the right sign-up email. If the app has a sign-up API, a test can call it, read the email through AssertInbox and assert on the subject, the sender, the code and the link. This guide does that with node:test and the global fetch in Node 18 or newer. There are no test frameworks or packages to install, and the tests run in a few seconds, which makes them a good fast check in CI before the slower browser tests.

How it works

Mail sent to <namespace>.<tag>@assertinbox.com is stored for your key. GET /v1/wait long-polls for up to 25 seconds and returns the newest matching message as JSON: from, subject, text, html, otp, magic_link and links. Messages are deleted after 24 hours.

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 for the tests: 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 listed in the limits page.

The test file

Save this as signup-email.test.js and run it with node --test. Set APP_URL to your app and change the sign-up request body to match its API. The expected sender and host come from environment variables with defaults you should replace.

const { test, after } = require("node:test");
const assert = require("node:assert");

const API = "https://assertinbox.com";
const KEY = process.env.ASSERTINBOX_KEY;
const NAMESPACE = process.env.ASSERTINBOX_NAMESPACE;
const APP = process.env.APP_URL || "http://localhost:3000";
const EXPECTED_SENDER = process.env.EXPECTED_SENDER || "noreply@example.com";
const EXPECTED_HOST = process.env.EXPECTED_HOST || new URL(APP).host;

const tags = [];

function newInbox() {
  const random = Math.random().toString(36).slice(2, 8);
  const tag = ("api-" + Date.now() + "-" + random).toLowerCase();
  tags.push(tag);
  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 signUp(email) {
  const res = await fetch(APP + "/api/signup", {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ email, password: "correct-Horse-battery-1" }),
  });
  assert.ok(res.status === 200 || res.status === 201, "sign-up returned " + res.status);
}

after(async () => {
  for (const tag of tags) {
    await fetch(API + "/v1/messages?tag=" + encodeURIComponent(tag), {
      method: "DELETE",
      headers: { Authorization: "Bearer " + KEY },
    });
  }
});

test("sign-up sends one verification email with the right subject and sender", { timeout: 90000 }, async () => {
  const inbox = newInbox();
  await signUp(inbox.email);

  const mail = await waitForEmail(inbox.tag);
  assert.match(mail.subject, /verify|confirm/i);
  assert.ok(mail.from.toLowerCase().includes(EXPECTED_SENDER), "sender was " + mail.from);
});

test("the email carries a numeric code", { timeout: 90000 }, async () => {
  const inbox = newInbox();
  await signUp(inbox.email);

  const mail = await waitForEmail(inbox.tag);
  assert.match(mail.otp || "", /^[0-9]{6}$/, "otp was " + mail.otp);
});

test("the verification link points at our host over https", { timeout: 90000 }, async () => {
  const inbox = newInbox();
  await signUp(inbox.email);

  const mail = await waitForEmail(inbox.tag);
  assert.ok(mail.magic_link, "no verification link found");
  const link = new URL(mail.magic_link);
  assert.strictEqual(link.host, EXPECTED_HOST);
  assert.ok(link.protocol === "https:" || link.hostname === "localhost");
});

Run it with node --test. Node starts each test file in its own process and runs the tests inside a file one after another, so the three tests use three different tags and never share an inbox.

What to assert, and what not to

Assert the facts your users depend on: the message arrives, the subject names the action, the sender is your own address, the code has the length your form expects, and the link goes to your own host and not to a staging or localhost address. Avoid asserting the full body text, because copy changes all the time and a test that breaks on every wording edit gets deleted. If you want to check a specific phrase, match a short stable fragment with a regular expression.

The otp and magic_link fields are best-effort extractions from the text and HTML parts. If a message has several links, links lists all of them and magic_link is the one that looks like a verification link. When your email has an unusual layout, assert on links and pick the link with your own rule.

Run it in CI

Store ASSERTINBOX_KEY and ASSERTINBOX_NAMESPACE as secrets in your CI provider and pass them as environment variables to the step that runs node --test. Never print the key. Because each test waits only as long as the email takes, the whole file usually finishes in a few seconds against a healthy app. When two emails are expected from one action, wait for the first, then pass its id as after to wait for the second: waitForEmail(tag, { after: first.id }).

Troubleshooting

Related guides: Playwright email verification, testing password reset emails. The Node client file can replace the helper functions above.