Testing Utilities
The @memberstack/dom/testing subpath provides mock factories, a mock SDK instance, and custom test matchers for unit testing your Memberstack integration. No API calls needed.
All testing utilities work with both Jest and Vitest. The custom matchers extend the expect API in both frameworks.
Import
Testing utilities are available from the @memberstack/dom/testing subpath. No additional packages to install.
import {
createMockMemberstack,
mockMember,
mockPlan,
mockPlanConnection,
mockCard,
mockPrice,
mockApp,
mockContentGroup,
memberstackMatchers
} from '@memberstack/dom/testing';Mock Factories
Mock factories create realistic test data with sensible defaults. Pass an overrides object to customize any fields.
mockMember(overrides?)
Creates a mock Member object with all fields populated.
import { mockMember } from '@memberstack/dom/testing';
// Default member
const member = mockMember();
// {
// id: "mem_k3x9q2ab", // random on every call: "mem_" + 8 characters
// verified: true,
// profileImage: "",
// auth: { email: "test@example.com", hasPassword: true, providers: [] },
// loginRedirect: "/",
// stripeCustomerId: "",
// createdAt: "2025-01-01T00:00:00.000Z", // dynamic: new Date().toISOString()
// metaData: {},
// customFields: {},
// permissions: [],
// planConnections: []
// }
// With overrides
const verifiedMember = mockMember({
verified: true,
auth: { email: 'pro@example.com', hasPassword: true, providers: [] },
customFields: { 'first-name': 'Jane' }
});mockPlan(overrides?)
Creates a mock Plan object.
import { mockPlan } from '@memberstack/dom/testing';
const plan = mockPlan();
// { id: "pln_<random>", name: "Test Plan", description: "A test plan", priority: 0, status: "ACTIVE",
// redirects: { afterLogin: "/", afterLogout: "/", afterSignup: "/" }, prices: [] }
const proPlan = mockPlan({
name: 'Pro',
prices: [mockPrice({ amount: 29, name: 'Monthly' })] // amount is a number
});mockPlanConnection(overrides?)
Creates a mock PlanConnection representing a member's subscription to a plan.
import { mockPlanConnection } from '@memberstack/dom/testing';
const connection = mockPlanConnection();
// {
// id: "conn_<random>",
// active: true,
// status: "ACTIVE",
// planId: "pln_<random>", // random too: pass planId to match one of your plans
// type: "FREE",
// payment: null
// }
// Member with an active plan
const memberWithPlan = mockMember({
planConnections: [
mockPlanConnection({ planId: 'pln_pro-plan' })
]
});Other Factories
import { mockPrice, mockCard, mockApp, mockContentGroup } from '@memberstack/dom/testing';
// Mock a price tier
// The real API returns interval.type as "YEARLY" | "MONTHLY" | "WEEKLY" | "DAILY".
// (The factory's own default is "month", which the real API never returns.)
const price = mockPrice({ amount: 49, name: 'Annual', interval: { type: 'YEARLY', count: 1 } });
// Mock a saved card
const card = mockCard({ brand: 'visa', last4: '4242' });
// Mock app configuration
const app = mockApp({ name: 'My App', mode: 'sandbox' });
// Mock a content group (gated content)
const group = mockContentGroup({ name: 'Premium Content', key: 'premium' });createMockMemberstack(options?)
Creates an in-memory mock of the most commonly used SDK methods (listed below). Async methods return promises and update the mock's internal state. Methods that are not listed are not defined on the mock; add your own with the overrides option, e.g. createMockMemberstack({ overrides: { getMemberJSON: async () => ({ data: {} }) } }) (in TypeScript the option is typed to the listed methods only, so cast the options and the returned instance, e.g. as any). Several of the mocked methods behave differently from the real SDK; the table below notes the main differences.
import { createMockMemberstack, mockMember, mockPlan } from '@memberstack/dom/testing';
const mockMs = createMockMemberstack({
// Pre-set a logged-in member
member: mockMember({
auth: { email: 'test@example.com', hasPassword: true, providers: [] }
}),
// Available plans
plans: [
mockPlan({ id: 'pln_free', name: 'Free' }),
mockPlan({ id: 'pln_pro', name: 'Pro' })
]
});
// Same call and return shape as the real SDK
const { data } = await mockMs.getCurrentMember();
console.log(data.auth.email); // "test@example.com"
// Login/logout update internal state
await mockMs.logout();
const { data: afterLogout } = await mockMs.getCurrentMember();
console.log(afterLogout); // null
await mockMs.loginMemberEmailPassword({
email: 'test@example.com',
password: 'any-password'
});
const { data: afterLogin } = await mockMs.getCurrentMember();
console.log(afterLogin.auth.email); // "test@example.com"Available Mock Methods
The mock instance supports these methods:
| Method | Behavior |
|---|---|
getCurrentMember() | Returns the current mock member or null |
loginMemberEmailPassword() | Replaces the current member with a new default mock member that has the given email. Any password is accepted, and the member passed in options is not restored; use _setMember() to log in a specific fixture |
signupMemberEmailPassword() | Creates and sets a mock member |
logout() | Clears the current member. Differs from the real SDK: resolves { data: {} } (real: { data: { id, redirect } }) |
updateMember() | Updates custom fields on the mock member. Differs from the real SDK: any key is stored (real: only keys that are custom fields), and when logged out it throws Error("Not logged in") (real: rejects with the string "Unauthorized") |
getPlans() | Returns the configured mock plans |
getPlan() | Returns a specific plan by ID. Differs from the real SDK: an unknown ID resolves { data: null } (the real SDK rejects with code plan-not-found) |
getApp() | Returns the mock app configuration |
getRestrictedUrlGroups() | Returns mock content groups |
getMemberCards() | Returns the configured mock cards. Mock only: the real SDK has no getMemberCards() method, so do not call it from app code |
onAuthChange(callback) | Subscribes to auth changes. Differs from the real SDK in three ways: the mock calls callback({ member }) (real: callback(member)), calls it once immediately on subscribe (real: does not), and returns an unsubscribe function (real: returns { unsubscribe }) |
Test Helpers
The mock instance also includes test-only helpers for manipulating state directly:
// Directly set the current member (without triggering login flow)
mockMs._setMember(mockMember({ verified: true }));
// Inspect the internal mock state
const state = mockMs._getMockState();
console.log(state.member); // Current member
console.log(state.plans); // Available plansCustom Test Matchers
Extend Jest or Vitest with Memberstack-specific assertions for cleaner, more readable tests.
import { memberstackMatchers, mockMember, mockPlanConnection } from '@memberstack/dom/testing';
// Register matchers (do this once, e.g., in a setup file)
expect.extend(memberstackMatchers);
// toBeLoggedIn() - asserts the value is a member object (not null, with auth.email)
const member = mockMember();
expect(member).toBeLoggedIn();
expect(null).not.toBeLoggedIn();
// toHavePlan(planId) - asserts the member has a plan connection for planId
// with active: true (status is not checked)
const memberWithPlan = mockMember({
planConnections: [mockPlanConnection({ planId: 'pln_pro' })]
});
expect(memberWithPlan).toHavePlan('pln_pro');
expect(memberWithPlan).not.toHavePlan('pln_enterprise');
// toBeVerified() - asserts member's email is verified
const verifiedMember = mockMember({ verified: true });
expect(verifiedMember).toBeVerified();Register the matchers once in your test setup file (e.g., vitest.setup.ts or jest.setup.js) so they're available in all tests.
Full Test Example
Here's a complete example testing a component that uses Memberstack for authentication and plan checking.
// auth.test.ts
import { describe, it, expect, beforeEach } from 'vitest';
import {
createMockMemberstack,
mockMember,
mockPlan,
mockPlanConnection,
memberstackMatchers
} from '@memberstack/dom/testing';
expect.extend(memberstackMatchers);
describe('Authentication Flow', () => {
let mockMs;
beforeEach(() => {
mockMs = createMockMemberstack({
plans: [
mockPlan({ id: 'pln_free', name: 'Free' }),
mockPlan({ id: 'pln_pro', name: 'Pro' })
]
});
});
it('should start logged out', async () => {
const { data } = await mockMs.getCurrentMember();
expect(data).not.toBeLoggedIn();
});
it('should login and return member', async () => {
mockMs._setMember(mockMember({
auth: { email: 'user@test.com', hasPassword: true, providers: [] }
}));
const { data } = await mockMs.getCurrentMember();
expect(data).toBeLoggedIn();
expect(data.auth.email).toBe('user@test.com');
});
it('should check plan access', async () => {
const member = mockMember({
planConnections: [
mockPlanConnection({ planId: 'pln_pro', status: 'ACTIVE' })
]
});
mockMs._setMember(member);
const { data } = await mockMs.getCurrentMember();
expect(data).toHavePlan('pln_pro');
expect(data).not.toHavePlan('pln_enterprise');
});
it('should logout and clear member', async () => {
mockMs._setMember(mockMember());
await mockMs.logout();
const { data } = await mockMs.getCurrentMember();
expect(data).not.toBeLoggedIn();
});
it('should list available plans', async () => {
const { data } = await mockMs.getPlans();
expect(data).toHaveLength(2);
expect(data[0].name).toBe('Free');
expect(data[1].name).toBe('Pro');
});
});Using with Dependency Injection
For the mock to work with your app code, pass the Memberstack instance as a dependency rather than importing it directly. Here's a pattern that works with most frameworks.
// memberstack.ts - your app's Memberstack wrapper
let instance = null;
export function setMemberstack(ms) {
instance = ms;
}
export function getMemberstack() {
return instance;
}
// main.ts - production code
import memberstack from '@memberstack/dom';
import { setMemberstack } from './memberstack';
const ms = await memberstack.init({ publicKey: 'pk_...' });
setMemberstack(ms);
// test.ts - test code
import { createMockMemberstack } from '@memberstack/dom/testing';
import { setMemberstack } from './memberstack';
beforeEach(() => {
setMemberstack(createMockMemberstack({ /* options */ }));
});