-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
116 lines (104 loc) · 2.94 KB
/
vitest.setup.ts
File metadata and controls
116 lines (104 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { TextDecoder, TextEncoder } from "node:util";
import * as testingLibraryMatchers from "@testing-library/jest-dom/matchers";
import { expect, vi } from "vitest";
import "@testing-library/jest-dom/vitest";
import failOnConsole from "vitest-fail-on-console";
expect.extend(testingLibraryMatchers);
// Polyfill TextEncoder/TextDecoder for jsdom environment
global.TextEncoder = TextEncoder;
// @ts-expect-error - TextDecoder types are compatible
global.TextDecoder = TextDecoder;
// Fail tests that log errors or warnings to console
failOnConsole({
shouldFailOnDebug: false,
shouldFailOnError: true,
shouldFailOnInfo: false,
shouldFailOnLog: false,
shouldFailOnWarn: true,
});
// Global mocks used across test files
vi.mock("next/headers", () => ({
headers: vi.fn(() => Promise.resolve(new Headers())),
}));
vi.mock("next/navigation", () => ({
redirect: vi.fn(),
useRouter: vi.fn(() => ({
push: vi.fn(),
replace: vi.fn(),
refresh: vi.fn(),
back: vi.fn(),
forward: vi.fn(),
prefetch: vi.fn(),
})),
}));
// Global auth server mock with default authenticated session
// Uses importActual to preserve real exports for unit tests
// Individual tests can override getSession/getAccessToken return values if needed
vi.mock("@/lib/auth/auth", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/lib/auth/auth")>();
return {
...actual,
auth: {
...actual.auth,
api: {
...actual.auth.api,
getSession: vi.fn(() =>
Promise.resolve({
user: {
id: "mock-user-id",
email: "test@example.com",
name: "Test User",
},
}),
),
getAccessToken: vi.fn(() =>
Promise.resolve({ accessToken: "mock-test-token" }),
),
},
},
};
});
// Common UI/runtime mocks
vi.mock("next/image", () => ({
default: () => null,
}));
vi.mock("sonner", () => ({
Toaster: () => null,
toast: {
error: vi.fn(),
success: vi.fn(),
info: vi.fn(),
warning: vi.fn(),
},
}));
export const mockSetTheme = vi.fn();
vi.mock("next-themes", () => ({
useTheme: () => ({
theme: "system",
setTheme: mockSetTheme,
}),
ThemeProvider: ({ children }: { children: React.ReactNode }) => children,
}));
// Auth client baseline mock; individual tests can customize return values
vi.mock("@/lib/auth/auth-client", () => ({
authClient: {
signIn: {
oauth2: vi.fn(),
},
signOut: vi.fn().mockResolvedValue({ data: null, error: null }),
},
signIn: {
oauth2: vi.fn(),
},
signOut: vi.fn().mockResolvedValue({ data: null, error: null }),
useSession: vi.fn(),
}));
import { cleanup } from "@testing-library/react";
// Reset mocks between test cases globally
import { afterEach } from "vitest";
afterEach(() => {
// Clear calls/instances, but keep hoisted module mock implementations intact
vi.clearAllMocks();
// Clean up DOM after each test
cleanup();
});