-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
124 lines (109 loc) · 2.76 KB
/
Copy pathjest.setup.js
File metadata and controls
124 lines (109 loc) · 2.76 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
// Global Jest Setup for AntBase
const { TextEncoder, TextDecoder } = require('util');
// Polyfills for Node.js environment
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
// Mock console methods in test environment
if (process.env.NODE_ENV === 'test') {
// Suppress console.log in tests unless explicitly needed
const originalConsoleLog = console.log;
console.log = (...args) => {
if (process.env.DEBUG_TESTS === 'true') {
originalConsoleLog(...args);
}
};
}
// Global test timeout
jest.setTimeout(30000);
// Mock environment variables
process.env.NODE_ENV = 'test';
process.env.JWT_SECRET = 'test-jwt-secret';
process.env.DATABASE_URL = 'postgresql://antbase_user:antbase_dev_password@localhost:5432/antbase_test';
process.env.REDIS_URL = 'redis://localhost:6379/15';
// Global beforeEach and afterEach hooks
beforeEach(() => {
// Reset all mocks before each test
jest.clearAllMocks();
});
afterEach(() => {
// Cleanup after each test
jest.clearAllTimers();
});
// Global error handler for unhandled promises
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
// Fail the test on unhandled promise rejection
process.exit(1);
});
// Mock fetch globally
global.fetch = require('jest-fetch-mock');
// Mock WebSocket
global.WebSocket = class MockWebSocket {
constructor(url) {
this.url = url;
this.readyState = 1; // OPEN
this.send = jest.fn();
this.close = jest.fn();
this.addEventListener = jest.fn();
this.removeEventListener = jest.fn();
}
};
// Mock localStorage
const localStorageMock = (() => {
let store = {};
return {
getItem: (key) => store[key] || null,
setItem: (key, value) => {
store[key] = value.toString();
},
removeItem: (key) => {
delete store[key];
},
clear: () => {
store = {};
},
};
})();
Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
});
// Mock matchMedia
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
// Mock IntersectionObserver
global.IntersectionObserver = class IntersectionObserver {
constructor() {}
observe() {
return null;
}
unobserve() {
return null;
}
disconnect() {
return null;
}
};
// Mock ResizeObserver
global.ResizeObserver = class ResizeObserver {
constructor() {}
observe() {
return null;
}
unobserve() {
return null;
}
disconnect() {
return null;
}
};