-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
211 lines (194 loc) · 6.13 KB
/
Copy pathjest.setup.js
File metadata and controls
211 lines (194 loc) · 6.13 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Optional: configure or set up a testing framework before each test.
// If you delete this file, remove `setupFilesAfterEnv` from `jest.config.js`
// React 19 compatibility: patch React.act before testing-library loads
// React 19 moved act to 'react' but testing-library may still look for it via react-dom/test-utils
import * as React from 'react';
// Check if we're on React 19+ where act is directly on React
if (typeof React.act === 'undefined') {
// For React 19, act should be imported from 'react' directly
// But if it's missing, we need to patch it
try {
const { act } = require('react');
if (act) {
React.act = act;
}
} catch {
// Fallback: create a minimal act implementation
React.act = (callback) => {
const result = callback();
if (result && typeof result.then === 'function') {
return result;
}
return Promise.resolve(result);
};
}
}
// Used for __tests__/testing-library.js
// Learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';
// Mock Next.js router
jest.mock('next/navigation', () => ({
useRouter: () => ({
push: jest.fn(),
replace: jest.fn(),
prefetch: jest.fn(),
back: jest.fn(),
forward: jest.fn(),
refresh: jest.fn(),
}),
usePathname: () => '/',
useSearchParams: () => new URLSearchParams(),
}));
// Mock Zustand store
jest.mock('@/lib/stores/app', () => ({
useAppStore: jest.fn(() => ({
user: null,
organization: null,
role: null,
setUser: jest.fn(),
setOrganization: jest.fn(),
setRole: jest.fn(),
})),
useOrgId: jest.fn(() => null),
}));
// Mock Supabase
jest.mock('@/lib/supabase/client', () => ({
createSupabaseClient: jest.fn(() => ({
auth: {
getUser: jest.fn(),
signInWithOAuth: jest.fn(),
signOut: jest.fn(),
},
from: jest.fn(() => ({
select: jest.fn(() => ({
eq: jest.fn(() => ({
single: jest.fn(),
maybeSingle: jest.fn(),
})),
})),
})),
})),
}));
// Mock environment variables
process.env.NEXT_PUBLIC_SUPABASE_URL = 'https://test.supabase.co';
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY = 'test-anon-key';
process.env.NEXT_PUBLIC_APP_URL = 'http://localhost:3000';
// Ensure TextEncoder/TextDecoder are available for crypto helpers
if (typeof TextEncoder === 'undefined') {
const { TextEncoder, TextDecoder } = require('util');
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
}
// Increase timeout for async tests
jest.setTimeout(10000);
// Global test utilities
global.ResizeObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}));
global.IntersectionObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}));
// Mock fetch for API tests — but ONLY when not running integration suites
// that need real network access (cross-org RLS isolation, real Supabase
// JWT round-trips, Rekor anchor verifier, etc.). Integration suites set
// RUN_INTEGRATION_TESTS=1 and the global stomp is skipped so the real
// fetch survives. Audit 2026-05-27: this used to be an unconditional
// stomp that crashed every integration test locally — fixed.
if (process.env.RUN_INTEGRATION_TESTS !== '1') {
global.fetch = jest.fn();
}
// Mock framer-motion to avoid animation issues in tests
jest.mock('framer-motion', () => {
const React = require('react');
// Filter out framer-motion specific props that aren't valid DOM attributes
const filterMotionProps = (props) => {
const motionProps = [
'initial',
'animate',
'exit',
'transition',
'variants',
'whileHover',
'whileTap',
'whileFocus',
'whileInView',
'whileDrag',
'layout',
'layoutId',
'onAnimationStart',
'onAnimationComplete',
'onViewportEnter',
'onViewportLeave',
'viewport',
'drag',
'dragConstraints',
'dragElastic',
'dragMomentum',
'dragTransition',
'dragPropagation',
];
const filtered = {};
Object.keys(props).forEach((key) => {
if (!motionProps.includes(key)) {
filtered[key] = props[key];
}
});
return filtered;
};
const createMotionComponent = (tag) =>
React.forwardRef(({ children, ...props }, ref) =>
React.createElement(tag, { ...filterMotionProps(props), ref }, children),
);
return {
motion: {
div: createMotionComponent('div'),
span: createMotionComponent('span'),
button: createMotionComponent('button'),
a: createMotionComponent('a'),
ul: createMotionComponent('ul'),
li: createMotionComponent('li'),
section: createMotionComponent('section'),
article: createMotionComponent('article'),
header: createMotionComponent('header'),
footer: createMotionComponent('footer'),
nav: createMotionComponent('nav'),
main: createMotionComponent('main'),
p: createMotionComponent('p'),
h1: createMotionComponent('h1'),
h2: createMotionComponent('h2'),
h3: createMotionComponent('h3'),
h4: createMotionComponent('h4'),
h5: createMotionComponent('h5'),
h6: createMotionComponent('h6'),
img: createMotionComponent('img'),
svg: createMotionComponent('svg'),
path: createMotionComponent('path'),
},
AnimatePresence: ({ children }) => children,
useMotionValue: () => ({ set: jest.fn(), get: jest.fn(() => 0) }),
useTransform: () => ({ set: jest.fn(), get: jest.fn(() => 0) }),
useSpring: () => ({ set: jest.fn(), get: jest.fn(() => 0) }),
useAnimation: () => ({
start: jest.fn(),
stop: jest.fn(),
}),
useInView: () => true,
useScroll: () => ({ scrollY: { get: jest.fn(() => 0) } }),
};
});
// Mock performance monitor
jest.mock('@/lib/monitoring/performance-monitor', () => ({
trackCustomMetric: jest.fn(),
CUSTOM_METRICS: {
CHECKLIST_LOAD: 'checklist_load',
ROADMAP_RENDER: 'roadmap_render',
FRAMEWORK_PROVISION: 'framework_provision',
CACHE_HIT_RATE: 'cache_hit_rate',
API_LATENCY: 'api_latency',
INDUSTRY_LOAD: 'industry_load',
},
}));