-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
70 lines (59 loc) · 1.89 KB
/
jest.setup.js
File metadata and controls
70 lines (59 loc) · 1.89 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
// Global Jest setup using Jest module mocking for loggers
// Track all log calls for test assertions
const testLogCalls = {
log: [],
warn: [],
error: [],
debug: [],
group: [],
groupEnd: []
};
// Mock the loggers module before any imports
jest.doMock('./src/loggers', () => {
const originalModule = jest.requireActual('./src/loggers');
return {
...originalModule,
consoleLog: (level, ...args) => {
testLogCalls[level].push(args);
},
setLoggers: originalModule.setLoggers,
getLoggers: originalModule.getLoggers
};
});
// Make test log calls available globally for test assertions
global.getTestLogCalls = () => ({ ...testLogCalls });
global.clearTestLogCalls = () => {
Object.keys(testLogCalls).forEach(level => {
testLogCalls[level].length = 0;
});
};
// Helper function for tests to assert log calls
global.expectLogCall = (level, ...expectedArgs) => {
const calls = testLogCalls[level];
const matchingCall = calls.find(call => {
if (call.length !== expectedArgs.length) {
return false;
}
return call.every((arg, index) => {
if (typeof expectedArgs[index] === 'object' && expectedArgs[index] && expectedArgs[index].asymmetricMatch) {
// Handle jest matchers like expect.any(Error)
return expectedArgs[index].asymmetricMatch(arg);
}
// Deep equality check for arrays and objects
if (Array.isArray(expectedArgs[index]) && Array.isArray(arg)) {
return JSON.stringify(arg) === JSON.stringify(expectedArgs[index]);
}
return arg === expectedArgs[index];
});
});
if (!matchingCall) {
throw new Error(`Expected ${level} call with args: ${JSON.stringify(expectedArgs)}\nActual calls: ${JSON.stringify(calls)}`);
}
return true;
};
// Clear log calls before each test
beforeEach(() => {
global.clearTestLogCalls();
});
// Global test timeout
jest.setTimeout(10000);