diff --git a/packages/better-auth/src/test-utils/test-instance.ts b/packages/better-auth/src/test-utils/test-instance.ts index 7b6d66c908..0d285ff32d 100644 --- a/packages/better-auth/src/test-utils/test-instance.ts +++ b/packages/better-auth/src/test-utils/test-instance.ts @@ -6,7 +6,7 @@ import type { } from "@better-auth/core"; import type { SuccessContext } from "@better-fetch/fetch"; import { sql } from "kysely"; -import { afterAll } from "vitest"; +import { onTestFinished } from "vitest"; import { betterAuth } from "../auth/full"; import { createAuthClient } from "../client"; import { parseSetCookieHeader, setCookieToHeader } from "../cookies"; @@ -16,20 +16,11 @@ import { bearer } from "../plugins"; import type { Session, User } from "../types"; import { getBaseURL } from "../utils/url"; -const cleanupSet = new Set(); - type CurrentUserContext = { headers: Headers; }; const currentUserContextStorage = new AsyncLocalStorage(); -afterAll(async () => { - for (const cleanup of cleanupSet) { - await cleanup(); - cleanupSet.delete(cleanup); - } -}); - export async function getTestInstance< O extends Partial, C extends BetterAuthClientOptions, @@ -189,7 +180,7 @@ export async function getTestInstance< return; } }; - cleanupSet.add(cleanup); + onTestFinished(cleanup); const customFetchImpl = async ( url: string | URL | Request,