Back to KB
Difficulty
Intermediate
Read Time
9 min

How to Test Better Auth Email Verification with Playwright

By Codcompass Team··9 min read

End-to-End Validation of Better Auth Email Flows: A Playwright Integration Strategy

Current Situation Analysis

Modern TypeScript authentication frameworks like Better Auth abstract away the complexity of email delivery, token generation, and session management. While this accelerates development, it creates a critical blind spot in automated testing pipelines. Email-dependent flows—verification, magic links, OTPs, and password resets—rely on external delivery mechanisms that are notoriously difficult to orchestrate in CI environments.

Teams typically resort to one of two suboptimal strategies:

  1. Mocking the Email Provider: Tests simulate the email send but bypass the actual token lifecycle. This leaves integration bugs undetected, such as malformed URLs, expired tokens, or plugin misconfigurations.
  2. Shared Inboxes with Regex Parsing: Tests use a static email address and scrape the raw HTML body for links or codes. This approach is brittle, prone to race conditions in parallel runs, and requires fragile regular expressions that break with minor template changes.

The industry overlooks the fact that email flows are stateful. A verification link is a single-use token; an OTP has a strict expiration window. Testing these flows requires not just catching the email, but validating the token's integrity and the application's response to consumption. Without isolated, real-time email capture, E2E suites for authentication remain incomplete, leaving security-critical paths unverified.

WOW Moment: Key Findings

The shift from static inboxes to disposable, edge-captured mailboxes fundamentally changes the reliability and speed of auth testing. By leveraging a service that provisions unique inboxes and parses payloads at the network edge, teams can achieve deterministic results without Docker containers or SMTP servers.

The following comparison highlights the operational differences between common testing strategies:

ApproachSetup ComplexityParallel SafetyToken ValidationFlakiness RiskMaintenance Overhead
MockingLowHighNoneLowHigh (Mock drift)
Shared SMTP + RegexHighLowFullHighHigh (Regex updates)
Disposable Edge InboxesLowHighFullLowLow

Why this matters: Disposable edge inboxes eliminate the "email black hole." The testing client intercepts messages before they reach a traditional mailbox, extracts structured data (OTPs and URLs), and returns them to the test runner. This enables true parallel execution where each worker operates on a unique identity, while still validating the complete token lifecycle from generation to consumption.

Core Solution

Implementing a robust testing strategy for Better Auth requires three components: a configured authentication instance, a disposable email client, and a structured test harness that isolates state.

1. Dependencies and Initialization

Install the testing framework and the disposable email client. No API keys or environment variables are required for the email service.

npm install @playwright/test zerodrop-client
npx playwright install chromium

2. Better Auth Configuration

Configure Better Auth with the necessary plugins. The email callbacks should delegate to your production provider (e.g., Resend, Postmark) during tests, as the disposable inbox will catch the outbound traffic.

// src/lib/auth-config.ts
import { betterAuth } from "better-auth";
import { magicLink } from "better-auth/plugins";
import { emailOTP } from "better-auth/plugins";

export function createAuthConfig() {
  return betterAuth({
    emailAndPassword: {
      enabled: true,
      sendResetPassword: async ({ user, url }) => {
        // Integration with your email provider
        await deliverMessage({
          to: user.email,
          subject: "Password Reset Request",
          content: url,
        });
      },
    },
    emailVe

🎉 Mid-Year Sale — Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register — Start Free Trial

7-day free trial · Cancel anytime · 30-day money-back