-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjest.setup.ts
More file actions
154 lines (136 loc) · 4.94 KB
/
Copy pathjest.setup.ts
File metadata and controls
154 lines (136 loc) · 4.94 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
/**
* Global Jest Setup File
*
* 1. Captures console logs for test reports
* 2. Suppresses expected SDK validation errors to reduce console noise during tests.
*/
import * as fs from 'fs';
import * as path from 'path';
import {
getLastCapturedRequest,
clearCapturedRequests,
} from './test/utils/request-capture-plugin';
import {
installAssertionTracker,
clearAssertions,
getAssertions,
} from './test/utils/assertion-tracker';
// Store captured console logs
interface ConsoleLog {
type: 'log' | 'warn' | 'error' | 'info' | 'debug';
message: string;
timestamp: string;
testFile?: string;
}
declare global {
var __CONSOLE_LOGS__: ConsoleLog[];
var __CURRENT_TEST_FILE__: string;
}
// Initialize global console log storage
global.__CONSOLE_LOGS__ = [];
global.__CURRENT_TEST_FILE__ = '';
// Store original console methods
const originalConsole = {
log: console.log,
warn: console.warn,
error: console.error,
info: console.info,
debug: console.debug
};
// List of expected SDK validation errors to suppress
const expectedErrors = [
'Invalid key:', // From query.search() validation
'Invalid value (expected string or number):', // From query.equalTo() validation
'Invalid argument. Provide a string or an array', // From entry/entries.includeReference() validation (ErrorMessages.INVALID_ARGUMENT_STRING_OR_ARRAY)
'Invalid fieldUid:', // From asset query validation
];
// Helper to capture and optionally forward console output
function captureConsole(type: 'log' | 'warn' | 'error' | 'info' | 'debug') {
return (...args: any[]) => {
const message = args.map(arg =>
typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg)
).join(' ');
// Store the log
global.__CONSOLE_LOGS__.push({
type,
message,
timestamp: new Date().toISOString(),
testFile: global.__CURRENT_TEST_FILE__
});
// For errors, check if it's expected (suppress if so)
if (type === 'error') {
const isExpectedError = expectedErrors.some(pattern => message.includes(pattern));
if (!isExpectedError) {
originalConsole[type].apply(console, args);
}
} else {
// Forward other logs normally
originalConsole[type].apply(console, args);
}
};
}
// Override console methods to capture logs
console.log = captureConsole('log');
console.warn = captureConsole('warn');
console.error = captureConsole('error');
console.info = captureConsole('info');
console.debug = captureConsole('debug');
// ---------------------------------------------------------------------------
// Rich per-test HTTP context (cURL / SDK method / request+response / status).
// Active only when ENABLE_HTTP_CAPTURE=true (the request-capture plugin is
// attached to the stack instance under the same flag). Each test's last
// captured HTTP call is appended to a JSONL sidecar that the custom
// rich-html-reporter reads at run-end to build the single-file HTML report.
// ---------------------------------------------------------------------------
const HTTP_CAPTURE_ENABLED = process.env.ENABLE_HTTP_CAPTURE === 'true';
const CAPTURES_FILE = path.resolve(__dirname, 'test-results', 'http-captures.jsonl');
if (HTTP_CAPTURE_ENABLED) {
beforeEach(() => {
// Install inside beforeEach so it runs AFTER the spec's `import { expect } from
// '@jest/globals'` has resolved the shared module object (idempotent via a guard).
// Records every assertion (expected/actual/pass) without changing any test.
installAssertionTracker();
clearCapturedRequests();
clearAssertions();
});
afterEach(() => {
try {
const cap = getLastCapturedRequest();
const assertions = getAssertions();
if (!cap && assertions.length === 0) return;
const state: any = (expect as any).getState();
const rec = {
testPath: state.testPath,
testName: state.currentTestName,
capture: cap || null,
assertions,
};
const dir = path.dirname(CAPTURES_FILE);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
fs.appendFileSync(CAPTURES_FILE, JSON.stringify(rec) + '\n');
} catch {
// never let reporting break a test
}
});
}
// After all tests complete, write logs to file
afterAll(() => {
const logsPath = path.resolve(__dirname, 'test-results', 'console-logs.json');
const logsDir = path.dirname(logsPath);
if (!fs.existsSync(logsDir)) {
fs.mkdirSync(logsDir, { recursive: true });
}
// Append to existing logs (in case of multiple test files)
let existingLogs: ConsoleLog[] = [];
if (fs.existsSync(logsPath)) {
try {
existingLogs = JSON.parse(fs.readFileSync(logsPath, 'utf8'));
} catch {
existingLogs = [];
}
}
const allLogs = [...existingLogs, ...global.__CONSOLE_LOGS__];
fs.writeFileSync(logsPath, JSON.stringify(allLogs, null, 2));
// Clear for next file
global.__CONSOLE_LOGS__ = [];
});