AssertInbox

Run email tests in CircleCI

Email tests are easy to run on a laptop and easy to break in CI: the key leaks into a log, two pipelines read each other's messages, or a parallel container waits for a mail that another one consumed. This guide sets up Playwright Test in CircleCI with the AssertInbox API key stored as a project environment variable, a tag that is unique for every pipeline and every parallel container, and a job that fails with a readable message. It uses a complete .circleci/config.yml, a helper and a spec.

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, including id, subject, otp and magic_link. Messages are deleted after 24 hours. The tag is the isolation boundary: if every job builds its tags from the pipeline number and their own node index, no two jobs can read the same mail.

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.

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

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.

Store the key as an environment variable

In your project, open Project Settings, then Environment Variables, and add ASSERTINBOX_KEY with the api_key value. CircleCI shows only the last characters of a stored value afterwards. The namespace is not secret, since it is only the first part of an address, so add it as ASSERTINBOX_NAMESPACE. Add APP_URL too if your tests run against a staging URL. If several projects share one key, you can put the variables in a context instead and attach the context to the workflow job; use a context when you want to restrict who can run jobs that read the key.

Pipelines started from forked pull requests do not receive project variables by default. Decide whether those pipelines should skip the email tests; do not work around it by printing the key.

The pipeline

Save this as .circleci/config.yml. It uses the Playwright image, so no browser install step is needed; keep the image version in line with the @playwright/test version in your package.json. parallelism: 4 starts four containers for the job, and each one receives CIRCLE_NODE_INDEX (counting from 0) and CIRCLE_NODE_TOTAL. Playwright's --shard option counts from 1, so the command adds one. RUN_TAG combines the pipeline number with the node index, so a re-run and every parallel container get their own tags.

version: 2.1

jobs:
  e2e:
    docker:
      - image: mcr.microsoft.com/playwright:v1.49.0-jammy
    parallelism: 4
    steps:
      - checkout
      - run:
          name: Install dependencies
          command: npm ci
      - run:
          name: Run email tests
          command: |
            export RUN_TAG="cci-${CIRCLE_PIPELINE_NUMBER}-${CIRCLE_NODE_INDEX}"
            npx playwright test --shard=$((CIRCLE_NODE_INDEX + 1))/${CIRCLE_NODE_TOTAL}
      - store_artifacts:
          path: playwright-report
      - store_artifacts:
          path: test-results

workflows:
  email-tests:
    jobs:
      - e2e

The job reads ASSERTINBOX_KEY, ASSERTINBOX_NAMESPACE and APP_URL from the project settings. A re-run of the same pipeline from the CircleCI page keeps the pipeline number, but the helper adds a counter and a random suffix to every tag, so a re-run does not read mail from the first attempt. If you prefer CIRCLE_BUILD_NUM, it works the same way and changes with every job run. The stored artifacts contain the Playwright report and traces, which show the page state when a test fails. They do not contain the key, as long as your tests never print it.

The helper and the test

Save the helper as tests/inbox.js. It needs Node 18 or newer for the built-in fetch. The tag combines RUN_TAG with a counter and a random suffix.

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

if (!KEY || !NAMESPACE) {
  throw new Error("Set ASSERTINBOX_KEY and ASSERTINBOX_NAMESPACE");
}

let counter = 0;

function newInbox(label) {
  const base = process.env.RUN_TAG || "local-" + Date.now();
  const tag = (base + "-" + label + "-" + ++counter + "-" + Math.random().toString(36).slice(2, 6)).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 + " for tag " + tag);
  }
  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: { Authorization: "Bearer " + KEY },
  });
}

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

Save this as tests/signup.spec.js. Replace the page, labels and subject with those of your app.

const { test, expect } = require("@playwright/test");
const { newInbox, waitForEmail, deleteEmails } = require("./inbox");

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

test("sign-up with an emailed code", async ({ page }) => {
  const inbox = newInbox("signup");
  try {
    await page.goto(APP + "/signup");
    await page.fill("input[name=email]", inbox.email);
    await page.click("button[type=submit]");

    const mail = await waitForEmail(inbox.tag, { subject: "verify" });
    expect(mail.otp).toMatch(/^[0-9]{4,8}$/);

    await page.fill("input[name=code]", mail.otp);
    await page.click("button[type=submit]");
    await expect(page.getByText("Email verified")).toBeVisible();
  } finally {
    await deleteEmails(inbox.tag);
  }
});

Keeping the key out of the logs

Parallel jobs and the daily allowance

Each key has 2,000 API requests per UTC day, and a wait counts as one request however long it polls. A few parallel jobs that each run a few dozen email tests stay far below that, but a pipeline that runs on every push of a busy repository can add up. If you want to separate pipelines from local runs, create a second key for CI. If a job fails with HTTP 429, the Retry-After header gives the seconds until 00:00 UTC.

Troubleshooting

Next steps

For unique tags, the trigger-then-wait order, retries and cleanup in one place, read Reliable email tests. More stacks are on the guides page.