Every app with teams has someone inviting someone else. The invitation is the person's first contact with the product, and it goes through three places: the inviter's screen, an inbox and the invited person's browser. That's why it usually goes untested, and breaks without anyone noticing: the email doesn't go out, the link lands on the wrong page, or the person joins a team that isn't theirs.
With a real inbox your test can read, the whole flow becomes an ordinary test. This guide uses Playwright and the saci.cc npm package, but the ideas apply to any tool.
What's worth testing
Seeing the email arrive isn't enough. A good invitation test checks that:
- The invite reaches the address that was typed, saying who invited them and to which team. Nobody clicks an email from a sender they don't know.
- The link opens the accept page with the email already filled in, and the account created there joins the right team with the right role.
- The link works only once.
- Resending or canceling an invite turns off the old link.
- An invite for one address can't be used by an account with another address. This is the one that becomes a security hole: a forwarded or leaked link must not let a stranger into the team.
Set up the key and the inboxes
Create a free key on your account page, put it in SACI_API_KEY and install the client:
npm install --save-dev saci.cc Each invited person is a new inbox, made with createInbox(). That way each inbox gets a single invite, and tests running in parallel don't get in each other's way. Inboxes created with your key are private, which matters here: an invite link gives access to a team.
The inviter is already signed in. In Playwright, that's usually a storageState saved by a setup project, so the test's page is already the team owner's account.
The full test
First, a function that sends the invite through your app's screen and returns the email. It notes the time before clicking, to skip emails that were already in the inbox:
import { test, expect, type Page } from "@playwright/test"; import { createInbox, type Inbox } from "saci.cc"; async function invite(page: Page, inbox: Inbox) { const invitedAt = new Date(); await page.goto("/settings/team"); await page.getByLabel("Email").fill(inbox.address); await page.getByRole("button", { name: "Invite" }).click(); return inbox.waitForEmail({ subject: "invited you", after: invitedAt }); } The test invites a new address, checks the email, opens the link in a browser with no session and creates the account:
test("an invited person joins the team", async ({ page, browser }) => { test.setTimeout(90_000); const inbox = await createInbox(); const email = await invite(page, inbox); expect(email.text).toContain("Ana Souza"); // who invited expect(email.text).toContain("Acme"); // the team const link = email.links.find((url) => url.includes("/invite/")); expect(link).toMatch(/^https:/); // Someone else, in another browser, signed out const guest = await (await browser.newContext()).newPage(); await guest.goto(link!); await expect(guest.getByLabel("Email")).toHaveValue(inbox.address); await guest.getByLabel("Password").fill("invited-password-123"); await guest.getByRole("button", { name: "Join Acme" }).click(); await expect(guest.getByRole("heading", { name: "Acme" })).toBeVisible(); // The inviter sees them on the team await page.reload(); await expect(page.getByRole("row", { name: inbox.address })).toContainText("Member"); }); browser.newContext() is what makes the test count: without it, the link would open with the inviter's session, and the test would pass even with accepting broken.
Pick the link by its path
saci.cc finds the link to confirm an address, sign in or reset a password on its own, and returns it in the verificationLink field. A button like "Accept invitation" or "Join the team" is none of those, so don't count on that field for invites. Pick the link by its path, as in the test above, or with waitForLink inside invite():
const link = await inbox.waitForLink({ match: "/invite/", after: invitedAt }); Invitation emails usually have other links, like the logo, the privacy policy and "I don't know this person". Without match, waitForLink returns the verificationLink or, without one, the first link in the email, which is rarely the invite.
The link works only once
At the end of the previous test, open the same link in a new session. The app should refuse it:
const again = await (await browser.newContext()).newPage(); await again.goto(link!); await expect(again.getByText("This invite was already used")).toBeVisible(); Resending and canceling
When an invite is resent, the old link should stop working. Otherwise each resend leaves one more valid link out there:
const first = (await invite(page, inbox)).links.find((url) => url.includes("/invite/"))!; const resentAt = new Date(); await page.getByRole("row", { name: inbox.address }).getByRole("button", { name: "Resend" }).click(); const second = await inbox.waitForLink({ match: "/invite/", after: resentAt }); expect(second).not.toBe(first); const guest = await (await browser.newContext()).newPage(); await guest.goto(first); await expect(guest.getByText("This invite is no longer valid")).toBeVisible(); Canceling is the same test with the "Cancel" button and no second email.
Someone else's invite
Here the test needs two inboxes: the invited person's and someone who got hold of the link. The second person creates their own account and tries to use the invite:
test("an invite only works for the invited address", async ({ page, browser }) => { test.setTimeout(120_000); const [invited, other] = await Promise.all([createInbox(), createInbox()]); const link = (await invite(page, invited)).links.find((url) => url.includes("/invite/"))!; const intruder = await (await browser.newContext()).newPage(); await signUp(intruder, other.address, "another-password-456"); // your sign-up await intruder.goto(link); await expect(intruder.getByText("This invite is for another email")).toBeVisible(); await page.reload(); await expect(page.getByRole("row", { name: other.address })).toHaveCount(0); }); If your sign-up sends a verification code, signUp calls other.waitForCode() along the way, as in the Playwright guide.
Some apps invite with an open link that anyone can use until the seats run out. The saci.cc Team plan invite works that way. Then the test is a different one: the link expires on time, stops working when the team is full and turns off when a new one is made.
In short
- One inbox per invited person, made with
createInbox()and your key. - Open the link in a
browser.newContext(), so you don't accept with the inviter's session. - Pick the link by its path, with
links.findorwaitForLink({ match }):verificationLinkdoesn't cover invites. - Also test what must not happen: a reused link, an old link after a resend, an invite accepted by another address.
I build saci.cc, temporary email inboxes for end-to-end tests and AI agents. The npm package is saci.cc and the client is open source on GitHub.
