How to test password reset emails
Password reset is the email flow most likely to break quietly: the link expires, a second request invalidates the first, or the wrong message is read. This guide tests it end to end in Playwright Test (plain JavaScript). The test requests a reset, waits for the right email with the subject and after filters, follows the reset link, sets a new password and logs in with it. Two more tests cover an old link and a link that no longer works.
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, links and magic_link. Messages are deleted after 24 hours.
A reset test often sees more than one email at the same address: a welcome message from sign-up, then the reset email, then a second reset email. Two filters keep the test on the right one. subject is a case-insensitive substring match. after takes a message id and returns only a message that arrived later than that one.
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 helper
Save this as tests/inbox.js. It needs Node 18 or newer for the built-in fetch. waitForEmail accepts the filters, and latestId lists the messages for a tag (newest first) and returns the id of the newest, or undefined when there is none. Use it to mark "everything before this point" before you trigger a new email.
const API = "https://assertinbox.com";
const KEY = process.env.ASSERTINBOX_KEY;
const NAMESPACE = process.env.ASSERTINBOX_NAMESPACE;
function headers() {
return { Authorization: "Bearer " + KEY };
}
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: headers() });
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 latestId(tag) {
const res = await fetch(API + "/v1/messages?tag=" + encodeURIComponent(tag), { headers: headers() });
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 deleteEmails(tag) {
await fetch(API + "/v1/messages?tag=" + encodeURIComponent(tag), { method: "DELETE", headers: headers() });
}
module.exports = { newInbox, waitForEmail, latestId, deleteEmails };
The reset tests
Save this as tests/password-reset.spec.js. Replace the URLs, labels and button names with those of your app. The test account is created through the app's own sign-up page, with an address that belongs to this test only.
const { test, expect } = require("@playwright/test");
const { newInbox, waitForEmail, latestId, deleteEmails } = require("./inbox");
const APP = process.env.APP_URL || "http://localhost:3000";
const OLD_PASSWORD = "old-Password-123";
const NEW_PASSWORD = "new-Password-456";
async function signUp(page, email) {
await page.goto(APP + "/signup");
await page.getByLabel("Email").fill(email);
await page.getByLabel("Password").fill(OLD_PASSWORD);
await page.getByRole("button", { name: "Sign up" }).click();
}
async function requestReset(page, email) {
await page.goto(APP + "/forgot-password");
await page.getByLabel("Email").fill(email);
await page.getByRole("button", { name: "Send reset link" }).click();
}
test.describe("password reset", () => {
test.setTimeout(120000);
let inbox;
test.beforeEach(async ({ page }) => {
inbox = newInbox("reset");
await signUp(page, inbox.email);
});
test.afterEach(async () => {
await deleteEmails(inbox.tag);
});
test("reset link sets a new password", async ({ page }) => {
const before = await latestId(inbox.tag);
await requestReset(page, inbox.email);
const mail = await waitForEmail(inbox.tag, { subject: "reset", ...(before ? { after: before } : {}) });
expect(mail.magic_link).toBeTruthy();
await page.goto(mail.magic_link);
await page.getByLabel("New password").fill(NEW_PASSWORD);
await page.getByRole("button", { name: "Set password" }).click();
await page.goto(APP + "/login");
await page.getByLabel("Email").fill(inbox.email);
await page.getByLabel("Password").fill(NEW_PASSWORD);
await page.getByRole("button", { name: "Log in" }).click();
await expect(page.getByText("Welcome")).toBeVisible();
});
test("only the newest reset link works", async ({ page }) => {
const before = await latestId(inbox.tag);
await requestReset(page, inbox.email);
const first = await waitForEmail(inbox.tag, { subject: "reset", ...(before ? { after: before } : {}) });
await requestReset(page, inbox.email);
const second = await waitForEmail(inbox.tag, { subject: "reset", after: first.id });
expect(second.id).not.toBe(first.id);
await page.goto(first.magic_link);
await expect(page.getByText(/expired|invalid/i)).toBeVisible();
await page.goto(second.magic_link);
await expect(page.getByLabel("New password")).toBeVisible();
});
});
Why the filters matter
Without after, the second request in the "newest link" test would return the first email at once, because it is still the newest message for the tag when the call starts. With after set to the first message's id, the call keeps waiting until a message newer than that one arrives. The subject filter protects the first step in the same way: if your app also sends a welcome email at sign-up, the test skips it and waits for the reset email. If your reset subject does not contain "reset", change the filter to a word that it does contain.
Expired links
You usually cannot wait an hour in a test. Most apps let you configure a short reset lifetime in a test environment, for example a few seconds. Set that, request a reset, wait for the email, wait past the lifetime, and then open the link and assert the error message. The email still arrives within a second or two, so the lifetime must be longer than the time the test needs to receive it. If you cannot shorten the lifetime, test expiry at the unit level in your app and keep this end-to-end test for the delivery and the happy path.
Troubleshooting
- The wait times out. The subject filter may not match. Remove it once and print
mail.subject, then pick a stable word. - The test reads the sign-up email. Check that
beforeholds the sign-up message id. It is empty only if no sign-up email was sent yet; add awaitForEmailcall after sign-up if your app sends one. - HTTP 429. The key used its 2,000 requests for the day.
Retry-Aftergives the seconds until 00:00 UTC. - Parallel workers. Each test builds its own tag from the time and a random suffix, so workers never share an inbox.
Related guides: Playwright email verification, Cypress OTP login. The wait filters are described in the docs.