Skip to content

Commit 9c826df

Browse files
author
Rajat
committed
feat: update session handling to use auth.api.getSession and configure Jest for better-auth modules.
1 parent a33d10f commit 9c826df

5 files changed

Lines changed: 40 additions & 10 deletions

File tree

apps/web/app/(with-contexts)/layout-with-context.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import { Theme } from "@courselit/page-models";
2222
import { ThemeProvider as NextThemesProvider } from "@components/next-theme-provider";
2323
import { defaultState } from "@components/default-state";
2424
import { getUserProfile } from "./helpers";
25+
import { auth } from "@/auth";
26+
27+
type BetterAuthSession = Awaited<ReturnType<typeof auth.api.getSession>> | null;
2528

2629
function LayoutContent({
2730
address,
@@ -36,7 +39,7 @@ function LayoutContent({
3639
siteinfo: SiteInfo;
3740
theme: Theme;
3841
config: ServerConfig;
39-
session: Session | null;
42+
session: BetterAuthSession;
4043
}) {
4144
const [profile, setProfile] = useState(defaultState.profile);
4245
const [theme, setTheme] = useState(initialTheme);
@@ -103,7 +106,7 @@ export default function Layout(props: {
103106
siteinfo: SiteInfo;
104107
theme: Theme;
105108
config: ServerConfig;
106-
session: Session | null;
109+
session: BetterAuthSession;
107110
}) {
108111
return (
109112
<Suspense fallback={null}>

apps/web/app/api/payment/initiate/__tests__/integration.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,28 @@ jest.mock("@models/Community");
2929
jest.mock("@models/PaymentPlan");
3030
jest.mock("@models/Membership");
3131
jest.mock("@models/Invoice");
32-
jest.mock("@/auth");
32+
jest.mock("@/auth", () => ({
33+
auth: {
34+
api: {
35+
getSession: jest.fn(),
36+
},
37+
},
38+
}));
3339
jest.mock("@/payments-new");
3440
jest.mock("../../helpers");
3541
jest.mock("@/graphql/users/logic");
3642
jest.mock("@/graphql/paymentplans/logic");
43+
jest.mock("better-auth", () => ({
44+
betterAuth: jest.fn(),
45+
APIError: class extends Error {},
46+
}));
47+
jest.mock("better-auth/plugins", () => ({
48+
customSession: jest.fn(),
49+
emailOTP: jest.fn(),
50+
}));
51+
jest.mock("better-auth/adapters", () => ({
52+
createAdapterFactory: jest.fn(),
53+
}));
3754

3855
describe("Payment Initiate Integration Tests - Included Products", () => {
3956
const mockDomainId = new mongoose.Types.ObjectId(
@@ -158,7 +175,7 @@ describe("Payment Initiate Integration Tests - Included Products", () => {
158175
} as unknown as NextRequest;
159176

160177
// Mock auth
161-
(auth as jest.Mock).mockResolvedValue({
178+
(auth.api.getSession as unknown as jest.Mock).mockResolvedValue({
162179
user: {
163180
email: "test@test.com",
164181
},

apps/web/app/api/payment/initiate/__tests__/route.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ jest.mock("@models/Course");
2020
jest.mock("@models/PaymentPlan");
2121
jest.mock("@models/Invoice");
2222
jest.mock("@models/Community");
23-
jest.mock("@/auth");
23+
jest.mock("@/auth", () => ({
24+
auth: {
25+
api: {
26+
getSession: jest.fn(),
27+
},
28+
},
29+
}));
2430
jest.mock("../../helpers");
2531
jest.mock("@/graphql/users/logic");
2632
jest.mock("@/payments-new");
@@ -68,7 +74,7 @@ describe("Payment Initiate Route", () => {
6874
},
6975
} as unknown as NextRequest;
7076

71-
(auth as jest.Mock).mockResolvedValue({
77+
(auth.api.getSession as jest.Mock).mockResolvedValue({
7278
user: {
7379
email: "test@test.com",
7480
},
@@ -119,7 +125,7 @@ describe("Payment Initiate Route", () => {
119125
});
120126

121127
it("returns 401 if user is not authenticated", async () => {
122-
(auth as jest.Mock).mockResolvedValue(null);
128+
(auth.api.getSession as jest.Mock).mockResolvedValue(null);
123129

124130
const response = await POST(mockRequest);
125131
expect(response.status).toBe(401);

apps/web/jest.server.config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const config: Config = {
1313
slugify: "<rootDir>/__mocks__/slugify.ts",
1414
"@models/(.*)": "<rootDir>/models/$1",
1515
"@/auth": "<rootDir>/auth.ts",
16+
"@/ba-multitenant-adapter": "<rootDir>/ba-multitenant-adapter.ts",
1617
"@/payments-new": "<rootDir>/payments-new",
1718
"@/graphql/(.*)": "<rootDir>/graphql/$1",
1819
"@/config/(.*)": "<rootDir>/config/$1",
@@ -26,11 +27,13 @@ const config: Config = {
2627
"\\.(css|less|scss|sass)$": "identity-obj-proxy",
2728
},
2829
transform: {
29-
"^.+\\.(ts|tsx)$": [
30+
"^.+\\.(ts|tsx|mjs)$": [
3031
"ts-jest",
3132
{
33+
useESM: true,
3234
tsconfig: {
3335
jsx: "react-jsx",
36+
allowJs: true,
3437
},
3538
},
3639
],
@@ -40,11 +43,12 @@ const config: Config = {
4043
"**/api/**/__tests__/**/*.test.ts",
4144
],
4245
testPathIgnorePatterns: [
43-
"/node_modules/",
46+
"/node_modules/(?!better-auth)/",
4447
"/.next/",
4548
// Exclude component tests - they should run in the regular config
4649
".*/components/.*/__tests__/.*\\.test\\.(tsx|ts)$",
4750
],
51+
transformIgnorePatterns: ["node_modules/(?!better-auth)"],
4852
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json"],
4953
};
5054

apps/web/next-env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="next" />
22
/// <reference types="next/image-types/global" />
3-
import "./.next/dev/types/routes.d.ts";
3+
import "./.next/types/routes.d.ts";
44

55
// NOTE: This file should not be edited
66
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.

0 commit comments

Comments
 (0)