-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.ts
More file actions
123 lines (108 loc) · 3.19 KB
/
jest.setup.ts
File metadata and controls
123 lines (108 loc) · 3.19 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
import '@testing-library/jest-dom';
// Mock environment variables
process.env.GEMINI_API_KEY = 'test-api-key-mock';
// Mock next/navigation
jest.mock('next/navigation', () => ({
useRouter: () => ({
push: jest.fn(),
replace: jest.fn(),
prefetch: jest.fn(),
}),
useSearchParams: () => ({
get: jest.fn(),
}),
usePathname: () => '/',
}));
// Mock window.matchMedia
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
// Mock crypto.randomUUID
Object.defineProperty(global, 'crypto', {
value: {
randomUUID: () => 'test-uuid-' + Math.random().toString(36).substring(7),
},
});
// Mock TextEncoder and TextDecoder for jsPDF
if (typeof global.TextEncoder === 'undefined') {
const { TextEncoder, TextDecoder } = require('util');
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
}
// Mock scrollIntoView
Element.prototype.scrollIntoView = jest.fn();
// Mock lucide-react icons as React components
jest.mock('lucide-react', () => {
const React = require('react');
return new Proxy({}, {
get: (target, prop) => {
return React.forwardRef((props: any, ref: any) =>
React.createElement('svg', { ...props, ref, 'data-lucide': prop.toString() })
);
}
});
});
// Mock pdfjs-dist to avoid import.meta errors
jest.mock('pdfjs-dist', () => ({
getDocument: jest.fn(() => ({
promise: Promise.resolve({
numPages: 1,
getPage: jest.fn(() =>
Promise.resolve({
getTextContent: jest.fn(() =>
Promise.resolve({
items: [{ str: 'Mocked PDF content' }],
})
),
})
),
}),
})),
GlobalWorkerOptions: {
workerSrc: '',
},
version: '4.0.0',
}));
// Suppress console errors and warnings in tests
const originalError = console.error;
const originalWarn = console.warn;
// Create a jest spy that filters expected errors
console.error = jest.fn((...args: any[]) => {
const errorMessage = args[0]?.toString() || '';
// Suppress React error boundary errors that are expected in tests
if (
errorMessage.includes('Error: Uncaught') ||
errorMessage.includes('The above error occurred') ||
errorMessage.includes('React will try to recreate') ||
errorMessage.includes('Consider adding an error boundary') ||
errorMessage.includes('Not implemented: navigation') ||
errorMessage.includes('ErrorBoundary caught an error')
) {
return; // Suppress these specific errors
}
// Allow other errors through for debugging
originalError.apply(console, args);
});
// Create a jest spy that filters expected warnings
console.warn = jest.fn((...args: any[]) => {
const warnMessage = args[0]?.toString() || '';
// Suppress jsdom warnings
if (
warnMessage.includes('Not implemented') ||
warnMessage.includes('navigation')
) {
return; // Suppress jsdom warnings
}
// Allow other warnings through for debugging
originalWarn.apply(console, args);
});