-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
194 lines (177 loc) · 4.72 KB
/
Copy pathvitest.setup.ts
File metadata and controls
194 lines (177 loc) · 4.72 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// vitest.setup.ts
// Setup file for Vitest tests
import '@testing-library/jest-dom/vitest'
import { vi } from 'vitest'
// Mock framer-motion globally — all motion.* elements are handled via Proxy.
// Individual test files should NOT re-mock framer-motion.
vi.mock('framer-motion', () => {
const { createElement, forwardRef } = require('react')
// Complete set of framer-motion props that must not reach the DOM
const motionPropNames = new Set([
'layoutId',
'initial',
'animate',
'exit',
'transition',
'whileHover',
'whileTap',
'whileInView',
'whileFocus',
'whileDrag',
'viewport',
'variants',
'layout',
'drag',
'dragConstraints',
'dragElastic',
'dragMomentum',
'dragSnapToOrigin',
'dragTransition',
'onDragStart',
'onDrag',
'onDragEnd',
'onAnimationStart',
'onAnimationComplete',
'onLayoutAnimationStart',
'onLayoutAnimationComplete',
'custom',
'inherit',
'onUpdate',
'onBeforeLayoutMeasure',
'transformTemplate',
'onViewportEnter',
'onViewportLeave',
'layoutScroll',
'layoutDependency',
'layoutRoot',
'onHoverStart',
'onHoverEnd',
'onTapStart',
'onTap',
'onTapCancel',
'onPanStart',
'onPan',
'onPanEnd',
'dragPropagation',
'dragListener',
])
/**
* Creates a ref-forwarding component that renders the given HTML tag,
* stripping all framer-motion specific props to avoid React DOM warnings.
*/
const forward = (tag: string) => {
const Component = forwardRef(
(props: Record<string, unknown>, ref: unknown) => {
const filtered: Record<string, unknown> = { ref }
for (const [key, value] of Object.entries(props)) {
if (key !== 'children' && !motionPropNames.has(key)) {
filtered[key] = value
}
}
return createElement(tag, filtered, props.children as React.ReactNode)
},
)
Component.displayName = `Motion${tag.charAt(0).toUpperCase() + tag.slice(1)}`
return Component
}
const cache = new Map<string, ReturnType<typeof forward>>()
// Proxy dynamically generates a forwarding component for any motion.* element
const motionProxy = new Proxy(
{ create: (Component: unknown) => Component },
{
get: (target, prop: string | symbol) => {
if (typeof prop !== 'string') return Reflect.get(target, prop)
if (prop in target) return target[prop as keyof typeof target]
if (!cache.has(prop)) cache.set(prop, forward(prop))
return cache.get(prop)
},
},
)
return {
motion: motionProxy,
AnimatePresence: ({ children }: { children: React.ReactNode }) => children,
useInView: () => true,
useAnimation: () => ({ start: vi.fn(), stop: vi.fn() }),
useMotionValue: <T>(initial: T) => ({
get: () => initial as T,
set: vi.fn(),
onChange: vi.fn(),
}),
}
})
// Mock next/image to render simple img for tests
vi.mock('next/image', () => {
const React = require('react')
interface ImageProps {
alt: string
blurDataURL?: string
fill?: boolean
height?: number
loading?: 'lazy' | 'eager'
placeholder?: string
priority?: boolean
quality?: number
src: string
unoptimized?: boolean
width?: number
[key: string]: unknown
}
const Image = ({
src,
alt,
// Next.js specific props that should not be passed to native img
fill,
priority,
quality,
placeholder,
blurDataURL,
unoptimized,
loading,
...rest
}: ImageProps) => {
// Suppress unused variable warnings for Next.js-specific props
void fill
void priority
void quality
void placeholder
void blurDataURL
void unoptimized
// Only pass loading if it's a valid HTML attribute value
const imgProps: React.ImgHTMLAttributes<HTMLImageElement> = {
src,
alt,
...rest,
}
if (loading) {
imgProps.loading = loading
}
return React.createElement('img', imgProps)
}
Image.displayName = 'MockedImage'
return {
__esModule: true,
default: Image,
}
})
// Mock CSS modules
vi.mock('**/*.css', () => ({}))
// Mock static assets
vi.mock('**/*.(png|jpg|jpeg|gif|svg)', () => 'test-file-stub')
// Mock next/navigation globally — provides sensible defaults.
// Individual test files can override with their own vi.mock('next/navigation', ...)
// or use vi.mocked() to customize per-test.
vi.mock('next/navigation', () => ({
useRouter: () => ({
push: vi.fn(),
replace: vi.fn(),
refresh: vi.fn(),
back: vi.fn(),
forward: vi.fn(),
prefetch: vi.fn(),
}),
usePathname: () => '/en',
useSearchParams: () => new URLSearchParams(),
useParams: () => ({}),
redirect: vi.fn(),
notFound: vi.fn(),
}))