From 737c821cc680dca6c2494b0ec64dfdba8671cedc Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Thu, 22 Jan 2026 15:45:37 -0800 Subject: [PATCH] test(organization): fix flaky tests (#7560) --- .../plugins/organization/organization.test.ts | 48 ++++++++++++------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/packages/better-auth/src/plugins/organization/organization.test.ts b/packages/better-auth/src/plugins/organization/organization.test.ts index f07719d1e7..f50f2550e5 100644 --- a/packages/better-auth/src/plugins/organization/organization.test.ts +++ b/packages/better-auth/src/plugins/organization/organization.test.ts @@ -984,7 +984,8 @@ describe("organization", async (it) => { "user4@email.com", ]; - for (const user of users) { + // Create all users in parallel + const userPromises = users.map(async (user) => { const newUser = await auth.api.signUpEmail({ body: { email: user, @@ -992,19 +993,28 @@ describe("organization", async (it) => { name: user, }, }); + return { user, newUser }; + }); + + const createdUsers = await Promise.all(userPromises); + + // Add all members to organization in parallel + const memberPromises = createdUsers.map(async ({ newUser }) => { const session = await auth.api.getSession({ headers: new Headers({ Authorization: `Bearer ${newUser?.token}`, }), }); - await auth.api.addMember({ + return auth.api.addMember({ body: { organizationId: org?.id, userId: session?.user.id!, role: "admin", }, }); - } + }); + + await Promise.all(memberPromises); const userOverLimit = { email: "shouldthrowerror@email.com", @@ -1128,14 +1138,25 @@ describe("organization", async (it) => { headers: headers2, }); + // Create both users in parallel to save time + const [newUser, secondUser] = await Promise.all([ + auth.api.signUpEmail({ + body: { + email: "user1@email.com", + password: "password", + name: "user1", + }, + }), + auth.api.signUpEmail({ + body: { + email: "user2@email.com", + password: "password", + name: "user2", + }, + }), + ]); + // Add 1 member, now count = 2 (creator + 1) - const newUser = await auth.api.signUpEmail({ - body: { - email: "user1@email.com", - password: "password", - name: "user1", - }, - }); const session = await auth.api.getSession({ headers: new Headers({ Authorization: `Bearer ${newUser?.token}`, @@ -1150,13 +1171,6 @@ describe("organization", async (it) => { }); // Try to add a second member, should fail since limit is 2 - const secondUser = await auth.api.signUpEmail({ - body: { - email: "user2@email.com", - password: "password", - name: "user2", - }, - }); const session2 = await auth.api.getSession({ headers: new Headers({ Authorization: `Bearer ${secondUser?.token}`,