Cypress

How to test magic link authentication in Cypress without mocking

Use a PostMX inbox to capture the link your app sends, then continue the Cypress flow with the real URL instead of a stubbed response.

The shorter path is the better test: send the mail, read the link, visit it.

Why not mock it

Mocking the link hides the part that usually breaks

Magic-link tests often fail in the gap between "the app says it sent the email" and "the user receives a valid URL." Using a live inbox keeps that gap visible, so template changes, routing mistakes, or expired links surface in the test suite instead of in production.

Test the contract, not the shortcut

If the user will click a link that came by email, the test should wait for that link to arrive and then drive the browser with it. That is the behavior you want to trust.

Minimal Cypress flow

import { PostMX } from "postmx";

const postmx = new PostMX(Cypress.env("POSTMX_API_KEY"));

it("logs in with a magic link", () => {
  cy.then(() => postmx.createTemporaryInbox({ label: "cypress-magic-link" }))
    .then((inbox) => {
      cy.visit("/login");
      cy.get('input[type="email"]').type(inbox.email_address);
      cy.contains("Send link").click();

      return cy.then(() => postmx.waitForMessage(inbox.id))
        .then((email) => {
          expect(email.links[0]?.url).to.be.ok;
          cy.visit(email.links[0].url);
        });
    });
});

Related links

Use the rest of the site when you need more context

These pages cover the adjacent flows that usually sit next to magic links in a real product.

Product page Magic links

Magic link testing

A shorter product overview for teams that want the workflow in one screen.

Open page
Workflow OTP

OTP email testing

See the adjacent flow for verification codes when the passwordless login path becomes a one-time code.

Open page
Guide Inbox API

Getting started with PostMX

Read the broader onboarding guide if you want to understand the product from the first import onward.

Open article

Start building today

Keep Cypress honest with a real inbox

Capture the link from the email your app actually sent, then continue the test with the URL the user would click.