-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
92 lines (82 loc) · 2.88 KB
/
vitest.setup.ts
File metadata and controls
92 lines (82 loc) · 2.88 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
import '@testing-library/jest-dom'
import React from 'react'
import { vi } from 'vitest'
// Mock the framer-motion
vi.mock('framer-motion', () => ({
motion: {
div: ({ children, ...props }: React.PropsWithChildren<Record<string, unknown>>) =>
React.createElement('div', props, children),
},
AnimatePresence: ({ children }: React.PropsWithChildren) => children,
}))
// Mock the Prisma Mongo client
vi.mock('.prisma-mongo/client', () => {
const mockPrismaMongoClient = vi.fn(() => ({
$disconnect: vi.fn().mockResolvedValue(undefined),
cards: {
findUnique: vi.fn().mockResolvedValue({ id: 'mock-card-id' }),
},
notablePlayers: {
findUnique: vi.fn().mockResolvedValue({ id: 'mock-player-id' }),
},
// Add other methods as needed
}))
return {
PrismaClient: mockPrismaMongoClient,
}
})
// Mock any other dependencies as needed
vi.mock('@/lib/hooks/useTransformRes', () => ({
useTransformRes: () => (params: Record<string, unknown>) => params,
}))
// Create a helper for mocking fetch responses
interface MockFetchOptions {
status?: number
statusText?: string
headers?: Record<string, string>
}
// Helper function to create fetch responses
global.createFetchResponse = (data: unknown, options: MockFetchOptions = {}) => {
return {
ok: options.status ? options.status >= 200 && options.status < 300 : true,
status: options.status || 200,
statusText: options.statusText || 'OK',
headers: new Headers(options.headers || { 'Content-Type': 'application/json' }),
json: () => Promise.resolve(data),
text: () => Promise.resolve(JSON.stringify(data)),
redirected: false,
type: 'basic',
url: '',
clone: function () {
return this
},
body: null,
bodyUsed: false,
arrayBuffer: () => Promise.resolve(new ArrayBuffer(0)),
blob: () => Promise.resolve(new Blob([])),
formData: () => Promise.resolve(new FormData()),
} as Response
}
// Add global fetch mock helper
global.mockFetch = (response: unknown, options: MockFetchOptions = {}) => {
global.fetch = vi.fn().mockResolvedValue(createFetchResponse(response, options))
}
// Add global fetch error mock helper
global.mockFetchError = (errorMessage: string) => {
global.fetch = vi.fn().mockRejectedValue(new Error(errorMessage))
}
// Add global fetch network error mock helper
global.mockFetchNetworkError = () => {
global.fetch = vi.fn().mockRejectedValue(new TypeError('Network request failed'))
}
// Add type definitions for the global helpers
declare global {
// eslint-disable-next-line no-var
var createFetchResponse: (data: unknown, options?: MockFetchOptions) => Response
// eslint-disable-next-line no-var
var mockFetch: (response: unknown, options?: MockFetchOptions) => void
// eslint-disable-next-line no-var
var mockFetchError: (errorMessage: string) => void
// eslint-disable-next-line no-var
var mockFetchNetworkError: () => void
}