-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
63 lines (57 loc) · 1.45 KB
/
jest.setup.js
File metadata and controls
63 lines (57 loc) · 1.45 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
// Setup file for Jest
import '@testing-library/jest-dom';
import 'jest-canvas-mock';
// Only setup browser-specific mocks if we're in a browser environment
if (typeof window !== 'undefined') {
// Mock localStorage
const localStorageMock = (function() {
let store = {};
return {
getItem(key) {
return store[key] || null;
},
setItem(key, value) {
store[key] = value.toString();
},
removeItem(key) {
delete store[key];
},
clear() {
store = {};
}
};
})();
Object.defineProperty(window, 'localStorage', {
value: localStorageMock
});
// Mock next/router
jest.mock('next/router', () => ({
useRouter() {
return {
route: '/',
pathname: '',
query: {},
asPath: '',
push: jest.fn(),
replace: jest.fn(),
events: {
on: jest.fn(),
off: jest.fn()
}
};
}
}));
}
// Suppress console.error, console.warn, and stack traces during tests
global.console.error = jest.fn();
global.console.warn = jest.fn();
// Reduce stack trace noise
Error.stackTraceLimit = 3;
// Override Error serialization for cleaner test output
const originalPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (err, stack) => {
if (process.env.NODE_ENV === 'test') {
return `${err.message}`;
}
return originalPrepareStackTrace ? originalPrepareStackTrace(err, stack) : undefined;
};