[PR #7746] [MERGED] feat(test-utils): add test utilities plugin for integration and E2E testing #15778

Closed
opened 2026-04-13 10:13:09 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7746
Author: @janhesters
Created: 2/1/2026
Status: Merged
Merged: 2/11/2026
Merged by: @himself65

Base: canaryHead: feat/test-utils-plugin-improvements


📝 Commits (3)

  • 7a2287a feat(test-utils): add test utilities plugin for integration and E2E testing
  • b993431 test: update
  • a89ba05 test: fix

📊 Changes

10 files changed (+1173 additions, -0 deletions)

View changed files

docs/content/docs/plugins/test-utils.mdx (+343 -0)
📝 packages/better-auth/src/plugins/index.ts (+1 -0)
packages/better-auth/src/plugins/test-utils/auth-helpers.ts (+47 -0)
packages/better-auth/src/plugins/test-utils/cookie-builder.ts (+80 -0)
packages/better-auth/src/plugins/test-utils/db-helpers.ts (+77 -0)
packages/better-auth/src/plugins/test-utils/factories.ts (+54 -0)
packages/better-auth/src/plugins/test-utils/index.ts (+149 -0)
packages/better-auth/src/plugins/test-utils/otp-sink.ts (+19 -0)
packages/better-auth/src/plugins/test-utils/test-utils.test.ts (+348 -0)
packages/better-auth/src/plugins/test-utils/types.ts (+55 -0)

📄 Description

Summary

Adds a new testUtils() plugin that provides helpers for writing integration and E2E tests against Better Auth.

Features

  • Factories: createUser(), createOrganization() (with org plugin)
  • Database helpers: saveUser(), deleteUser(), saveOrganization(), deleteOrganization(), addMember()
  • Auth helpers: login(), getAuthHeaders(), getCookies()
  • OTP capture: getOTP() when captureOTP: true option is set

Usage

import { betterAuth } from "better-auth";
import { testUtils } from "better-auth/plugins";

export const auth = betterAuth({
  plugins: [testUtils({ captureOTP: true })],
});

// In tests:
const ctx = await auth.$context;
const test = ctx.test;

const user = test.createUser({ email: "test@example.com" });
await test.saveUser(user);
const { headers, cookies } = await test.login({ userId: user.id });

Documentation

Includes comprehensive documentation at docs/content/docs/plugins/test-utils.mdx with examples for:

  • Integration testing (Vitest)
  • E2E testing (Playwright)
  • OTP verification testing

Test plan

  • All 19 test cases pass (vitest packages/better-auth/src/plugins/test-utils --run)
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm format:check passes

Closes #5609

🤖 Generated with Claude Code


Summary by cubic

Adds a testUtils plugin to speed up integration and E2E testing in Better Auth. Helpers are exposed on auth.$context.test, with optional OTP capture for verification flows.

  • New Features
    • Factories: createUser; createOrganization (when organization plugin is enabled).
    • Database: saveUser/deleteUser; saveOrganization/deleteOrganization; addMember.
    • Auth: login (returns session, user, headers, cookies, token); getAuthHeaders; getCookies (signed session cookie, optional domain).
    • OTP: getOTP and clearOTPs when captureOTP: true.
    • Docs: added test-utils guide with Vitest, Playwright, and OTP examples.

Written for commit a89ba052a0. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/7746 **Author:** [@janhesters](https://github.com/janhesters) **Created:** 2/1/2026 **Status:** ✅ Merged **Merged:** 2/11/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `feat/test-utils-plugin-improvements` --- ### 📝 Commits (3) - [`7a2287a`](https://github.com/better-auth/better-auth/commit/7a2287aa122f6743b95e3efe95ef7652789ea05d) feat(test-utils): add test utilities plugin for integration and E2E testing - [`b993431`](https://github.com/better-auth/better-auth/commit/b993431945d4eb0b7323562a5e0ac7d225e19f74) test: update - [`a89ba05`](https://github.com/better-auth/better-auth/commit/a89ba052a0c8b59ac3f51d4d0fa9bcb31c518aab) test: fix ### 📊 Changes **10 files changed** (+1173 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `docs/content/docs/plugins/test-utils.mdx` (+343 -0) 📝 `packages/better-auth/src/plugins/index.ts` (+1 -0) ➕ `packages/better-auth/src/plugins/test-utils/auth-helpers.ts` (+47 -0) ➕ `packages/better-auth/src/plugins/test-utils/cookie-builder.ts` (+80 -0) ➕ `packages/better-auth/src/plugins/test-utils/db-helpers.ts` (+77 -0) ➕ `packages/better-auth/src/plugins/test-utils/factories.ts` (+54 -0) ➕ `packages/better-auth/src/plugins/test-utils/index.ts` (+149 -0) ➕ `packages/better-auth/src/plugins/test-utils/otp-sink.ts` (+19 -0) ➕ `packages/better-auth/src/plugins/test-utils/test-utils.test.ts` (+348 -0) ➕ `packages/better-auth/src/plugins/test-utils/types.ts` (+55 -0) </details> ### 📄 Description ## Summary Adds a new `testUtils()` plugin that provides helpers for writing integration and E2E tests against Better Auth. ### Features - **Factories**: `createUser()`, `createOrganization()` (with org plugin) - **Database helpers**: `saveUser()`, `deleteUser()`, `saveOrganization()`, `deleteOrganization()`, `addMember()` - **Auth helpers**: `login()`, `getAuthHeaders()`, `getCookies()` - **OTP capture**: `getOTP()` when `captureOTP: true` option is set ### Usage ```ts import { betterAuth } from "better-auth"; import { testUtils } from "better-auth/plugins"; export const auth = betterAuth({ plugins: [testUtils({ captureOTP: true })], }); // In tests: const ctx = await auth.$context; const test = ctx.test; const user = test.createUser({ email: "test@example.com" }); await test.saveUser(user); const { headers, cookies } = await test.login({ userId: user.id }); ``` ### Documentation Includes comprehensive documentation at `docs/content/docs/plugins/test-utils.mdx` with examples for: - Integration testing (Vitest) - E2E testing (Playwright) - OTP verification testing ## Test plan - [x] All 19 test cases pass (`vitest packages/better-auth/src/plugins/test-utils --run`) - [x] `pnpm typecheck` passes - [x] `pnpm lint` passes - [x] `pnpm format:check` passes Closes #5609 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds a testUtils plugin to speed up integration and E2E testing in Better Auth. Helpers are exposed on auth.$context.test, with optional OTP capture for verification flows. - **New Features** - Factories: createUser; createOrganization (when organization plugin is enabled). - Database: saveUser/deleteUser; saveOrganization/deleteOrganization; addMember. - Auth: login (returns session, user, headers, cookies, token); getAuthHeaders; getCookies (signed session cookie, optional domain). - OTP: getOTP and clearOTPs when captureOTP: true. - Docs: added test-utils guide with Vitest, Playwright, and OTP examples. <sup>Written for commit a89ba052a0c8b59ac3f51d4d0fa9bcb31c518aab. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 10:13:09 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#15778