Skip to content

Commit 3aa9fab

Browse files
committed
more global mocks
1 parent c19a4c3 commit 3aa9fab

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

src/app/__tests__/page.test.tsx

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
11
import { render, screen } from "@testing-library/react";
2-
import { beforeEach, expect, test, vi } from "vitest";
2+
import { expect, test } from "vitest";
33
import Home from "../page";
44

5-
// Mock auth session retrieval for this test
6-
vi.mock("@/lib/auth/auth", () => ({
7-
auth: {
8-
api: {
9-
getSession: vi.fn(() =>
10-
Promise.resolve({
11-
user: { email: "[email protected]", name: "Test User" },
12-
}),
13-
),
14-
},
15-
},
16-
}));
17-
18-
beforeEach(() => vi.clearAllMocks());
19-
205
test("Home page renders welcome heading and link to catalog when user is logged in", async () => {
216
render(await Home());
227

vitest.setup.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,26 @@ vi.mock("next/navigation", () => ({
1616
redirect: vi.fn(),
1717
}));
1818

19-
// Note: Do not globally mock the entire auth module; unit tests under src/lib/auth
20-
// validate real exports like encrypt/decrypt. Mock getSession per test instead.
19+
// Global auth server mock with default authenticated session
20+
// Uses importActual to preserve real exports (encrypt, decrypt, etc.) for unit tests
21+
// Individual tests can override getSession return value if needed
22+
vi.mock("@/lib/auth/auth", async (importOriginal) => {
23+
const actual = await importOriginal<typeof import("@/lib/auth/auth")>();
24+
return {
25+
...actual,
26+
auth: {
27+
...actual.auth,
28+
api: {
29+
...actual.auth.api,
30+
getSession: vi.fn(() =>
31+
Promise.resolve({
32+
user: { email: "[email protected]", name: "Test User" },
33+
}),
34+
),
35+
},
36+
},
37+
};
38+
});
2139

2240
// Common UI/runtime mocks
2341
vi.mock("next/image", () => ({

0 commit comments

Comments
 (0)