-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
82 lines (70 loc) · 2.37 KB
/
vitest.setup.ts
File metadata and controls
82 lines (70 loc) · 2.37 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
import "@testing-library/jest-dom"
import "dotenv/config"
import path from "path"
import dotenv from "dotenv"
import { beforeAll, vi } from "vitest"
// Import vi for mocking if needed
// Determine the correct path to the .env.test file relative to the project root
const envPath = path.resolve(process.cwd(), ".env.test")
console.log(`Attempting to load environment variables from: ${envPath}`) // Debugging line
// Load environment variables from .env.test specifically for tests
const result = dotenv.config({ path: envPath })
if (result.error) {
console.error("Error loading .env.test file:", result.error) // Debugging line
} else {
console.log(".env.test file loaded successfully.") // Debugging line
// console.log('DATABASE_URL loaded:', process.env.DATABASE_URL); // Optional: Check if variable is loaded
}
console.log("Current NODE_ENV:", process.env.NODE_ENV)
console.log("DATABASE_URL exists:", !!process.env.DATABASE_URL)
// --- Add other global test setups below ---
// Example: Mocking matchMedia for testing hooks like useIsMobile or other browser APIs
beforeAll(() => {
global.ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
}
Object.defineProperty(window, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn()
}))
})
// Mock other browser APIs if necessary for your tests
// e.g., localStorage, sessionStorage, etc.
})
// You can add other global mocks or configurations here
vi.mock("next/navigation", () => {
const actual = vi.importActual("next/navigation")
return {
...actual,
useRouter: vi.fn(() => ({
push: vi.fn(),
replace: vi.fn()
})),
usePathname: vi.fn(() => "/"),
useSearchParams: vi.fn(() => new URLSearchParams()),
redirect: vi.fn((path) => {
console.log(`Mock redirect to: ${path}`)
}),
permanentRedirect: vi.fn((path) => {
console.log(`Mock permanent redirect to: ${path}`)
})
}
})
vi.mock("next/cache", () => ({
cacheLife: vi.fn(),
cacheTag: vi.fn(),
revalidateTag: vi.fn(),
revalidatePath: vi.fn(),
unstable_cache: vi.fn((fn) => fn),
unstable_noStore: vi.fn()
}))