Skip to content

Commit 2581b47

Browse files
committed
Enhance Jest configuration and test setup for improved logging and reporting
- Added console log capture in Jest reports with JSON output. - Improved global fields tests to reflect changes in field names and added nested global field schema tests. - Enhanced HTML report generation to include console logs and improved styling for better readability.
1 parent 400f32c commit 2581b47

13 files changed

Lines changed: 840 additions & 49 deletions

.talismanrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,6 @@ fileignoreconfig:
6767
checksum: f7200cb6e3b9ff681439482faaf882781dfb5f6ab6fefd4c98203ba8bf30d5e6
6868
- filename: test/api/base-query-casting.specs.ts
6969
checksum: 9185df498914e2966d78d9d216acaaa910d43cd7ac9a5e9a26e7241ac9edc9b5
70+
- filename: test/reporting/generate-unified-report.js
71+
checksum: 9e7a4696561b790cb93f3be8406a70ec6fdc90a3f8bbb9739504495690158fe3
7072
version: "1.0"

jest.config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ export default {
4343
publicPath: "./reports/contentstack-delivery/html",
4444
filename: "index.html",
4545
expand: true,
46+
// Enable console log capture in reports
47+
enableMergeData: true,
48+
dataMergeLevel: 2,
4649
},
4750
],
4851
[
@@ -57,5 +60,12 @@ export default {
5760
titleTemplate: "{title}",
5861
},
5962
],
63+
// JSON reporter to capture console logs for unified report
64+
[
65+
"./test/reporting/jest-json-reporter.cjs",
66+
{
67+
outputPath: "test-results/jest-results.json",
68+
},
69+
],
6070
],
6171
};

jest.setup.ts

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
/**
22
* Global Jest Setup File
33
*
4-
* Suppresses expected SDK validation errors to reduce console noise during tests.
5-
* These errors are intentional - tests verify that the SDK handles invalid inputs gracefully.
4+
* 1. Captures console logs for test reports
5+
* 2. Suppresses expected SDK validation errors to reduce console noise during tests.
66
*/
7+
import * as fs from 'fs';
8+
import * as path from 'path';
79

8-
// Store the original console.error for genuine errors
9-
const originalConsoleError = console.error;
10+
// Store captured console logs
11+
interface ConsoleLog {
12+
type: 'log' | 'warn' | 'error' | 'info' | 'debug';
13+
message: string;
14+
timestamp: string;
15+
testFile?: string;
16+
}
17+
18+
declare global {
19+
var __CONSOLE_LOGS__: ConsoleLog[];
20+
var __CURRENT_TEST_FILE__: string;
21+
}
22+
23+
// Initialize global console log storage
24+
global.__CONSOLE_LOGS__ = [];
25+
global.__CURRENT_TEST_FILE__ = '';
26+
27+
// Store original console methods
28+
const originalConsole = {
29+
log: console.log,
30+
warn: console.warn,
31+
error: console.error,
32+
info: console.info,
33+
debug: console.debug
34+
};
1035

1136
// List of expected SDK validation errors to suppress
1237
const expectedErrors = [
@@ -16,17 +41,64 @@ const expectedErrors = [
1641
'Invalid fieldUid:', // From asset query validation
1742
];
1843

19-
// Override console.error globally to filter expected validation errors
20-
console.error = (...args: any[]) => {
21-
const message = args[0]?.toString() || '';
44+
// Helper to capture and optionally forward console output
45+
function captureConsole(type: 'log' | 'warn' | 'error' | 'info' | 'debug') {
46+
return (...args: any[]) => {
47+
const message = args.map(arg =>
48+
typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg)
49+
).join(' ');
50+
51+
// Store the log
52+
global.__CONSOLE_LOGS__.push({
53+
type,
54+
message,
55+
timestamp: new Date().toISOString(),
56+
testFile: global.__CURRENT_TEST_FILE__
57+
});
58+
59+
// For errors, check if it's expected (suppress if so)
60+
if (type === 'error') {
61+
const isExpectedError = expectedErrors.some(pattern => message.includes(pattern));
62+
if (!isExpectedError) {
63+
originalConsole[type].apply(console, args);
64+
}
65+
} else {
66+
// Forward other logs normally
67+
originalConsole[type].apply(console, args);
68+
}
69+
};
70+
}
71+
72+
// Override console methods to capture logs
73+
console.log = captureConsole('log');
74+
console.warn = captureConsole('warn');
75+
console.error = captureConsole('error');
76+
console.info = captureConsole('info');
77+
console.debug = captureConsole('debug');
78+
79+
// After all tests complete, write logs to file
80+
afterAll(() => {
81+
const logsPath = path.resolve(__dirname, 'test-results', 'console-logs.json');
82+
const logsDir = path.dirname(logsPath);
2283

23-
// Check if this is an expected SDK validation error
24-
const isExpectedError = expectedErrors.some(pattern => message.includes(pattern));
84+
if (!fs.existsSync(logsDir)) {
85+
fs.mkdirSync(logsDir, { recursive: true });
86+
}
2587

26-
// If not expected, show it (for genuine errors)
27-
if (!isExpectedError) {
28-
originalConsoleError.apply(console, args);
88+
// Append to existing logs (in case of multiple test files)
89+
let existingLogs: ConsoleLog[] = [];
90+
if (fs.existsSync(logsPath)) {
91+
try {
92+
existingLogs = JSON.parse(fs.readFileSync(logsPath, 'utf8'));
93+
} catch {
94+
existingLogs = [];
95+
}
2996
}
30-
// Otherwise, silently suppress it
31-
};
97+
98+
const allLogs = [...existingLogs, ...global.__CONSOLE_LOGS__];
99+
fs.writeFileSync(logsPath, JSON.stringify(allLogs, null, 2));
100+
101+
// Clear for next file
102+
global.__CONSOLE_LOGS__ = [];
103+
});
32104

test/api/complex-field-queries.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const stack = stackInstance();
77
// Content Type UIDs (use env vars with fallback defaults)
88
const COMPLEX_CT = process.env.COMPLEX_CONTENT_TYPE_UID || 'complex_content_type';
99
const MEDIUM_CT = process.env.MEDIUM_CONTENT_TYPE_UID || 'medium_content_type';
10-
const PRODUCT_CT = process.env.PRODUCT_CONTENT_TYPE_UID || 'product_content_type';
10+
const PRODUCT_CT = process.env.COMPLEX_BLOCKS_CONTENT_TYPE_UID || 'page_builder';
1111

1212
describe('Boolean Field Queries', () => {
1313
describe('Boolean field queries', () => {

test/api/entry-variants-comprehensive.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,8 @@ describe('Entry Variants Comprehensive Tests', () => {
508508
expect(result).toBeDefined();
509509

510510
if (result.variants && result.variants.length > 0) {
511-
const globalFields = ['seo', 'page_header', 'related_content', 'authors'];
511+
// Field names that exist in cybersecurity content type (which uses global fields)
512+
const globalFields = ['seo', 'search', 'content_block', 'video_experience'];
512513

513514
result.variants.forEach((variant: any, index: number) => {
514515
console.log(`Variant ${index + 1} global fields:`, {

test/api/global-fields-comprehensive.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { stackInstance } from '../utils/stack-instance';
33
import { TGlobalField } from './types';
44

55
const stack = stackInstance();
6-
const globalFieldUid = process.env.GLOBAL_FIELD_UID || 'seo_fields';
6+
// Use GLOBAL_FIELD_UID from env, fallback to 'seo' which exists in the test stack
7+
const globalFieldUid = process.env.SIMPLE_GLOBAL_FIELD_UID || process.env.GLOBAL_FIELD_UID || 'seo';
78

89
describe('Global Fields API Tests', () => {
910
describe('Global Field Basic Operations', () => {

test/api/live-preview.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dotenv.config();
88
const apiKey = process.env.API_KEY as string;
99
const deliveryToken = process.env.DELIVERY_TOKEN as string;
1010
const environment = process.env.ENVIRONMENT as string;
11-
const branch = process.env.BRANCH as string;
11+
const branch = process.env.BRANCH_UID as string;
1212
// Using new standardized env variable names
1313
// Use MEDIUM_ENTRY_UID for article content type (MEDIUM_CT)
1414
const entryUid = (process.env.MEDIUM_ENTRY_UID || process.env.COMPLEX_ENTRY_UID || '') as string;

test/api/locale-fallback-chain.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ describe('Locale Fallback Chain Tests', () => {
239239
expect(result).toBeDefined();
240240

241241
// Check global fields for locale-specific content
242-
const globalFields = ['seo', 'hero_banner', 'related_content', 'featured_content'];
242+
// Field names that exist in content types (seo, search are common global fields)
243+
const globalFields = ['seo', 'search', 'content_block', 'referenced_data'];
243244
const globalFieldAnalysis: Record<string, any> = {};
244245

245246
globalFields.forEach(field => {

test/api/metadata-branch-operations.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('Metadata and Branch Operations API Tests', () => {
5151
});
5252

5353
it('should include metadata in single asset fetch', async () => {
54-
const assetUid = process.env.ASSET_UID || 'sample_asset';
54+
const assetUid = process.env.IMAGE_ASSET_UID || 'sample_asset';
5555
const result = await stack.asset()
5656
.includeMetadata()
5757
.find();
@@ -92,7 +92,8 @@ describe('Metadata and Branch Operations API Tests', () => {
9292

9393
describe('Global Field Operations', () => {
9494
it('should fetch global field successfully', async () => {
95-
const globalFieldUid = process.env.GLOBAL_FIELD_UID || 'sample_global_field';
95+
// Use GLOBAL_FIELD_UID from env, fallback to 'seo' which exists in the test stack
96+
const globalFieldUid = process.env.SIMPLE_GLOBAL_FIELD_UID || process.env.GLOBAL_FIELD_UID || 'seo';
9697

9798
try {
9899
const result = await stack.globalField(globalFieldUid).fetch();

0 commit comments

Comments
 (0)