@@ -2,50 +2,51 @@ const testData = require("./numeric-keys.ct");
22
33import NullDocumentationGenerator from "../../../src/generateTS/docgen/nulldoc" ;
44import tsgenFactory from "../../../src/generateTS/factory" ;
5+ import { BasicLogger } from "../../../src/logger" ;
56
6- // Mock cliux to capture output
7- jest . mock ( "@contentstack/cli-utilities" , ( ) => ( {
8- cliux : {
9- print : jest . fn ( ) ,
10- success : jest . fn ( ) ,
11- error : jest . fn ( ) ,
12- table : jest . fn ( ) ,
13- } ,
14- } ) ) ;
7+ // Mock console methods to capture output
8+ const originalConsoleLog = console . log ;
9+ const originalConsoleWarn = console . warn ;
10+ const originalConsoleError = console . error ;
11+ const originalConsoleTable = console . table ;
1512
16- import { cliux } from "@contentstack/cli-utilities" ;
17-
18- let printOutput : string [ ] = [ ] ;
19- let successOutput : string [ ] = [ ] ;
13+ let logOutput : string [ ] = [ ] ;
14+ let warnOutput : string [ ] = [ ] ;
15+ let errorOutput : string [ ] = [ ] ;
2016let tableOutput : any [ ] = [ ] ;
2117
2218beforeEach ( ( ) => {
23- printOutput = [ ] ;
24- successOutput = [ ] ;
19+ logOutput = [ ] ;
20+ warnOutput = [ ] ;
21+ errorOutput = [ ] ;
2522 tableOutput = [ ] ;
26- ( cliux . print as jest . Mock ) . mockImplementation (
27- ( message : string , options ?: any ) => {
28- printOutput . push ( message ) ;
29- }
30- ) ;
31- ( cliux . success as jest . Mock ) . mockImplementation ( ( message : string ) => {
32- successOutput . push ( message ) ;
23+
24+ console . log = jest . fn ( ( ...args : any [ ] ) => {
25+ logOutput . push ( args . join ( " " ) ) ;
26+ } ) ;
27+ console . warn = jest . fn ( ( ...args : any [ ] ) => {
28+ warnOutput . push ( args . join ( " " ) ) ;
29+ } ) ;
30+ console . error = jest . fn ( ( ...args : any [ ] ) => {
31+ errorOutput . push ( args . join ( " " ) ) ;
32+ } ) ;
33+ console . table = jest . fn ( ( data : any ) => {
34+ tableOutput . push ( data ) ;
3335 } ) ;
34- ( cliux . table as jest . Mock ) . mockImplementation (
35- ( headers : any , data : any , flags ?: any , options ?: any ) => {
36- tableOutput . push ( { headers, data, flags, options } ) ;
37- }
38- ) ;
3936} ) ;
4037
4138afterEach ( ( ) => {
42- jest . clearAllMocks ( ) ;
39+ console . log = originalConsoleLog ;
40+ console . warn = originalConsoleWarn ;
41+ console . error = originalConsoleError ;
42+ console . table = originalConsoleTable ;
4343} ) ;
4444
4545describe ( "numeric key handling" , ( ) => {
4646 test ( "should skip fields with numeric UIDs and continue generation" , ( ) => {
4747 const tsgen = tsgenFactory ( {
4848 docgen : new NullDocumentationGenerator ( ) ,
49+ logger : new BasicLogger ( ) ,
4950 } ) ;
5051 const result = tsgen ( testData . contentTypeWithNumericKeys ) ;
5152
@@ -62,12 +63,14 @@ describe("numeric key handling", () => {
6263 test ( "should log warnings for skipped fields" , ( ) => {
6364 const tsgen = tsgenFactory ( {
6465 docgen : new NullDocumentationGenerator ( ) ,
66+ logger : new BasicLogger ( ) ,
6567 } ) ;
6668 tsgen ( testData . contentTypeWithNumericKeys ) ;
6769
6870 // Should have 2 warning messages for skipped fields
69- const warningMessages = printOutput . filter ( ( msg ) =>
70- msg . includes ( "Skipped field" )
71+ // Note: BasicLogger uses console.log with ANSI colors, so we check logOutput instead of warnOutput
72+ const warningMessages = logOutput . filter (
73+ ( msg ) => msg && msg . includes ( "Skipped field" )
7174 ) ;
7275 expect ( warningMessages ) . toHaveLength ( 2 ) ;
7376 expect ( warningMessages [ 0 ] ) . toContain ( 'Skipped field "123field"' ) ;
@@ -83,6 +86,7 @@ describe("numeric key handling", () => {
8386 test ( "should include skipped fields in metadata" , ( ) => {
8487 const tsgen = tsgenFactory ( {
8588 docgen : new NullDocumentationGenerator ( ) ,
89+ logger : new BasicLogger ( ) ,
8690 } ) ;
8791 const result = tsgen ( testData . contentTypeWithNumericKeys ) ;
8892
@@ -103,33 +107,32 @@ describe("numeric key handling", () => {
103107 test ( "should log summary at the end" , ( ) => {
104108 const tsgen = tsgenFactory ( {
105109 docgen : new NullDocumentationGenerator ( ) ,
110+ logger : new BasicLogger ( ) ,
106111 } ) ;
107112 tsgen ( testData . contentTypeWithNumericKeys ) ;
108113
109114 expect (
110- printOutput . some ( ( log ) => log . includes ( "Summary of Skipped Items" ) )
115+ logOutput . some ( ( log ) => log && log . includes ( "Summary of Skipped Items" ) )
111116 ) . toBe ( true ) ;
112117 expect (
113- printOutput . some ( ( log ) => log . includes ( "Total skipped items: 2" ) )
118+ logOutput . some ( ( log ) => log && log . includes ( "Total skipped items: 2" ) )
114119 ) . toBe ( true ) ;
115120 // Check that table was called with correct data
116121 expect ( tableOutput ) . toHaveLength ( 1 ) ; // One combined table
117- expect ( tableOutput [ 0 ] . headers ) . toHaveLength ( 4 ) ;
118- expect ( tableOutput [ 0 ] . headers [ 0 ] . value ) . toBe ( "Type" ) ;
119- expect ( tableOutput [ 0 ] . headers [ 1 ] . value ) . toBe ( "Key Name" ) ;
120- expect ( tableOutput [ 0 ] . headers [ 2 ] . value ) . toBe ( "Schema Path" ) ;
121- expect ( tableOutput [ 0 ] . headers [ 3 ] . value ) . toBe ( "Reason" ) ;
122- expect ( tableOutput [ 0 ] . data ) . toHaveLength ( 2 ) ; // 2 skipped fields
122+ expect ( tableOutput [ 0 ] ) . toHaveLength ( 2 ) ; // 2 skipped fields
123123 expect (
124- successOutput . some ( ( log ) =>
125- log . includes ( "Generation completed successfully with partial schema" )
124+ logOutput . some (
125+ ( log ) =>
126+ log &&
127+ log . includes ( "Generation completed successfully with partial schema" )
126128 )
127129 ) . toBe ( true ) ;
128130 } ) ;
129131
130132 test ( "should handle nested numeric keys in groups" , ( ) => {
131133 const tsgen = tsgenFactory ( {
132134 docgen : new NullDocumentationGenerator ( ) ,
135+ logger : new BasicLogger ( ) ,
133136 } ) ;
134137 const result = tsgen ( testData . contentTypeWithNestedNumericKeys ) ;
135138
@@ -144,6 +147,7 @@ describe("numeric key handling", () => {
144147 test ( "should handle numeric keys in modular blocks" , ( ) => {
145148 const tsgen = tsgenFactory ( {
146149 docgen : new NullDocumentationGenerator ( ) ,
150+ logger : new BasicLogger ( ) ,
147151 } ) ;
148152 const result = tsgen ( testData . contentTypeWithNumericBlockKeys ) ;
149153
@@ -158,6 +162,7 @@ describe("numeric key handling", () => {
158162 test ( "should handle mixed valid and invalid keys" , ( ) => {
159163 const tsgen = tsgenFactory ( {
160164 docgen : new NullDocumentationGenerator ( ) ,
165+ logger : new BasicLogger ( ) ,
161166 } ) ;
162167 const result = tsgen ( testData . contentTypeWithMixedKeys ) ;
163168
0 commit comments